import { cliPrompt } from "./prompts.ts";
import type { TerminalBlock } from "./TerminalLayout.ts";

type prompt = [string, (v?: string) => boolean | undefined] | string;

export async function forceArgs(
  args: string[],
  prompts: prompt[],
  block?: TerminalBlock,
) {
  const newArgs: string[] = [];
  for (const [i, arg] of args.entries()) {
    if (typeof prompts[i] === "string") {
      let val = arg;
      while (!val) {
        val = await cliPrompt(prompts[i], block) || "";
      }
      newArgs.push(val);
    } else {
      const [promptText, validation] = prompts[i];
      let val = arg;
      while (!validation(val)) {
        val = await cliPrompt(promptText, block) || "";
      }
      newArgs.push(val);
    }
  }
  return newArgs;
}
