import { o as resolveConfigPath } from "./paths-VdmjGGyc.js";
import { r as theme } from "./theme-C5EwWDWE.js";
import process from "node:process";
import fs from "node:fs";

//#region src/cli/first-run.ts
/**
* @module cli/first-run
*
* First-run detection and the obvious "you're not set up yet" welcome
* screen shown when someone runs bare `koi` on a machine that has never
* been logged in or configured. Routes them straight into login, the
* setup wizard, or the local app.
*
* This is deliberately conservative: it only takes over the terminal
* when attached to a real human TTY. Scripts, CI, docker e2e, and piped
* input keep the legacy `tui` fallback so existing automation is
* unaffected.
*/
/** True when a SkyKoi config file already exists on disk. */
function hasLocalConfig() {
	try {
		return fs.existsSync(resolveConfigPath());
	} catch {
		return false;
	}
}
/**
* A "fresh" machine: no saved login AND no config file. These users have
* nothing wired up, so bare `koi` should make setup obvious instead of
* dropping silently into the local TUI.
*/
function isFreshInstall(isLoggedIn) {
	return !isLoggedIn && !hasLocalConfig();
}
/**
* Only render the interactive welcome when attached to a human TTY and
* not running under CI / piped input. Set `SKYKOI_NO_FIRST_RUN=1` to opt
* out (e.g. tests that exercise the bare-`koi` path without prompts).
*/
function canPromptInteractively() {
	if (process.env.SKYKOI_NO_FIRST_RUN === "1") return false;
	if (process.env.CI && process.env.CI !== "false") return false;
	return Boolean(process.stdin.isTTY && process.stdout.isTTY);
}
/**
* Render the obvious first-run welcome and return the command the user
* picked, or `null` if they cancelled (Ctrl-C / Esc).
*/
async function runFirstRunWelcome() {
	const { intro, outro, select, isCancel, cancel } = await import("@clack/prompts");
	const { note } = await import("./note-D68Wd3Zr.js").then((n) => n.n);
	intro(theme.heading("  SkyKoi  "));
	note([
		"Looks like this is your first run — nothing is set up yet.",
		"",
		`${theme.accent("Log in")} to talk to the koi on your SkyKoi account, or`,
		`${theme.accent("set up a local koi")} to run everything on this machine.`
	].join("\n"), "Welcome");
	const choice = await select({
		message: "How do you want to get started?",
		options: [
			{
				value: "login",
				label: "Log in to SkyKoi",
				hint: "recommended · connects to your account"
			},
			{
				value: "admin",
				label: "Admin / team access",
				hint: "local · uses your env's Anthropic key, no website"
			},
			{
				value: "onboard",
				label: "Set up a local koi",
				hint: "full setup wizard · runs on this machine"
			},
			{
				value: "tui",
				label: "Just open the app for now",
				hint: "set up later with `koi onboard`"
			}
		]
	});
	if (isCancel(choice)) {
		cancel(`No problem — run ${theme.command("koi")} again any time, or ${theme.command("koi login")} / ${theme.command("koi onboard")} directly.`);
		return null;
	}
	outro(choice === "login" ? theme.muted("Opening login…") : choice === "admin" ? theme.muted("Unlocking admin access…") : choice === "onboard" ? theme.muted("Starting setup…") : theme.muted("Opening SkyKoi…"));
	return choice;
}

//#endregion
export { canPromptInteractively, isFreshInstall, runFirstRunWelcome };