import { parseInitFlags, FlagParseError } from "./tui/flags.js";
const INIT_USAGE = [
  "Usage: wigolo init [options]",
  "",
  "Alias for: wigolo config --force-wizard",
  "Always launches the interactive setup wizard, then drops into the settings shell.",
  "",
  "Options:",
  "  --non-interactive, -y   Skip interactive prompts (uses plain text flow)",
  "  --agents=<csv>          Comma-separated agent ids (required with --non-interactive)",
  "  --skip-verify           Skip the post-install verify step",
  "  --plain                 Force plain (non-TUI) output",
  "  --help, -h              Show this message",
  ""
].join("\n");
async function runInit(args) {
  let flags;
  try {
    flags = parseInitFlags(args);
  } catch (err) {
    if (err instanceof FlagParseError) {
      process.stderr.write(`${err.message}
`);
      process.stderr.write(INIT_USAGE);
      return 2;
    }
    throw err;
  }
  if (flags.help) {
    process.stderr.write(INIT_USAGE);
    return 0;
  }
  if (flags.nonInteractive && flags.agents.length === 0) {
    process.stderr.write("--non-interactive requires --agents=<csv>\n");
    process.stderr.write(INIT_USAGE);
    return 2;
  }
  const isTTY = Boolean(process.stdout.isTTY);
  const isCI = process.env.CI === "true" || process.env.CI === "1" || process.env.GITHUB_ACTIONS === "true";
  const useInk = !flags.plain && !flags.nonInteractive && isTTY && !isCI;
  if (useInk) {
    const { runConfig } = await import("./config.js");
    const configArgs = ["--force-wizard"];
    return runConfig(configArgs);
  }
  return runInitPlain(flags);
}
async function runInitPlain(flags) {
  const { renderBanner } = await import("./tui/banner.js");
  const { getPackageVersion } = await import("./tui/version.js");
  const { runSystemCheck } = await import("./tui/system-check.js");
  const { ok, fail, warn, info } = await import("./tui/format.js");
  const { default: chalk } = await import("chalk");
  const { runWarmup } = await import("./warmup.js");
  const { detectAgents } = await import("./tui/agents.js");
  const { applyConfigs } = await import("./tui/config-writer.js");
  const { runVerify } = await import("./tui/verify.js");
  const { autoReporter } = await import("./tui/reporter-auto.js");
  const { getConfig } = await import("../config.js");
  const { saveInitConfig } = await import("./tui/utils/config-writer.js");
  function out(line = "") {
    process.stdout.write(`${line}
`);
  }
  const version = getPackageVersion();
  process.stdout.write(renderBanner(version));
  const sysResult = await runSystemCheck();
  out(chalk.bold("  Checking your system..."));
  if (sysResult.node.ok) {
    out(`  ${ok(`Node.js ${sysResult.node.version}`)}`);
  } else {
    out(`  ${fail(`Node.js ${sysResult.node.version ?? "(unknown)"}`)}`);
    if (sysResult.node.message) out(`    ${chalk.gray(sysResult.node.message)}`);
  }
  if (sysResult.python.ok) {
    out(`  ${ok(`Python ${sysResult.python.version} (${sysResult.python.binary})`)}`);
  } else {
    out(`  ${fail("Python 3 not found")}`);
    if (sysResult.python.message) out(`    ${chalk.gray(sysResult.python.message)}`);
    out(`    ${chalk.gray("Install: https://python.org/downloads or `brew install python3`")}`);
  }
  if (sysResult.docker.ok) {
    out(`  ${ok(`Docker ${sysResult.docker.version ?? ""} ${chalk.gray("(optional)")}`)}`.trim());
  } else {
    out(`  ${warn(`Docker not found ${chalk.gray("(optional)")}`)}`);
  }
  if (sysResult.disk.ok) {
    out(`  ${ok(`Disk: ${sysResult.disk.freeMb} MB free`)}`);
  } else {
    out(`  ${warn(`Disk: ${sysResult.disk.message ?? "low free space"}`)}`);
  }
  if (sysResult.hardFailure) {
    out();
    out(chalk.red.bold("  Setup cannot continue until the issues above are resolved."));
    return 1;
  }
  out();
  out(`  ${info("System check passed.")}`);
  out();
  const reporter = autoReporter({ plain: flags.plain, command: "init" });
  try {
    await runWarmup(["--all"], reporter);
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    process.stderr.write(`Warmup failed: ${message}
`);
    return 1;
  }
  let detected;
  try {
    detected = detectAgents({});
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    process.stderr.write(`Agent detection failed: ${message}
`);
    return 1;
  }
  let selected;
  if (flags.nonInteractive) {
    selected = [...flags.agents];
  } else {
    const { selectAgents, NotTtyError } = await import("./tui/select-agents.js");
    try {
      selected = await selectAgents(detected);
    } catch (err) {
      if (err instanceof NotTtyError) {
        process.stderr.write("init requires an interactive terminal.\n");
        process.stderr.write("Use --non-interactive --agents=<comma-list> in scripts or CI.\n");
        return 2;
      }
      const message = err instanceof Error ? err.message : String(err);
      process.stderr.write(`Selection failed: ${message}
`);
      return 1;
    }
  }
  if (selected.length === 0) {
    process.stderr.write("No agents selected \u2014 nothing to do.\n");
    return 0;
  }
  const config = getConfig();
  try {
    await applyConfigs(detected, selected, {});
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    process.stderr.write(`Writing configs failed: ${message}
`);
    return 1;
  }
  {
    const { getAgentHandler } = await import("./agents/registry.js");
    const { detectFirecrawlSkills } = await import("./agents/utils.js");
    const { homedir } = await import("node:os");
    const { join: pathJoin } = await import("node:path");
    if (selected.includes("claude-code")) {
      const firecrawl = detectFirecrawlSkills(pathJoin(homedir(), ".claude", "skills"));
      if (firecrawl.length > 0) {
        out();
        out(`  ${info(`Detected firecrawl skills (${firecrawl.join(", ")}).`)}`);
        out(`    ${chalk.gray("Wigolo will be preferred for local/cached/transparent searches.")}`);
        out(`    ${chalk.gray("See ~/.claude/skills/wigolo-search/SKILL.md for the boundaries.")}`);
      }
    }
    for (const id of selected) {
      const handler = getAgentHandler(id);
      if (!handler) continue;
      out(`  Configuring ${handler.displayName}...`);
      try {
        await handler.installInstructions();
        out(`  ${ok("Global instructions updated")}`);
      } catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        out(`  ${warn(`Instructions skipped: ${message}`)}`);
      }
      if (handler.supportsSkills && handler.installSkills) {
        try {
          await handler.installSkills();
          out(`  ${ok("8 skills installed")}`);
        } catch (err) {
          const message = err instanceof Error ? err.message : String(err);
          out(`  ${warn(`Skills skipped: ${message}`)}`);
        }
      }
      if (handler.supportsCommands && handler.installCommand) {
        try {
          await handler.installCommand();
          out(`  ${ok("Command installed")}`);
        } catch (err) {
          const message = err instanceof Error ? err.message : String(err);
          out(`  ${warn(`Command skipped: ${message}`)}`);
        }
      }
    }
  }
  saveInitConfig(config.dataDir, {
    configuredAgents: selected,
    lastInit: (/* @__PURE__ */ new Date()).toISOString()
  });
  if (!flags.nonInteractive) {
    try {
      const { promptExtras } = await import("./tui/extras-prompt.js");
      await promptExtras(config.dataDir);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      process.stderr.write(`Optional setup skipped: ${message}
`);
    }
  }
  if (!flags.skipVerify) {
    try {
      const verifyResult = await runVerify(config.dataDir, reporter);
      if (!verifyResult.allPassed) {
        reporter.note("Some checks failed. The CLI will still continue.");
      }
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      process.stderr.write(`Verify failed: ${message}
`);
    }
  }
  return 0;
}
export {
  runInit
};
//# sourceMappingURL=init.js.map