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

export const createScaffoldUI = (projectType) => {
  return createStepUI({
    title: `${projectType} - 项目配置`,
    getSteps: () => generateSteps(projectType, null),
    onStepChange: framework => generateSteps(projectType, framework),
  });
};

export const 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 const waitKey = async (message = "按任意键返回") => {
  console.log(color.dim(`\n${message}`));
  return new Promise(resolve => {
    initKeypress();
    onKey(() => {
      stopKeypress();
      resolve();
    });
  });
};
