import color from "picocolors";
import { initKeypress, onKey, stopKeypress } from "../../keyboard.js";
import { createStepUI } from "../../utils/stepui.js";
import { generateSteps } from "./config.js";

// 框架 + 组件配置 UI
export function createScaffoldUI(projectType) {
  return createStepUI({
    title: `${projectType} - 项目配置`,
    getSteps: () => generateSteps(projectType, null),
    onStepChange: framework => generateSteps(projectType, framework),
  });
}

// 解析配置结果
export function formatResults(steps, results) {
  const summary = [];

  results.forEach((val, i) => {
    if (Array.isArray(val) && val.length > 0) {
      summary.push(`${steps[i].name}: ${val.join(", ")}`);
    } else if (val && val !== "none") {
      summary.push(`${steps[i].name}: ${val}`);
    }
  });

  return summary.length ? summary : ["未选择任何组件"];
}

// 等待按键
export async function waitKey(message = "按任意键返回") {
  console.log(color.dim(`\n${message}`));

  return new Promise(resolve => {
    initKeypress();
    onKey(() => {
      stopKeypress();
      resolve();
    });
  });
}
