import { Q as theme, Rt as shortenHomePath, _ as defaultRuntime } from "./entry.js";
import "./auth-profiles-D11Xw15i.js";
import "./exec-CzwIUObA.js";
import { C as ensureAgentWorkspace, m as DEFAULT_AGENT_WORKSPACE_DIR } from "./agent-scope-C-Jte08L.js";
import "./model-selection-CMx1evaF.js";
import "./file-lock-DrmXX3vL.js";
import "./agent-paths-DryQawbH.js";
import "./manifest-registry-CVLmi8rz.js";
import { l as writeConfigFile, r as createConfigIO } from "./config-D7TcU4qN.js";
import "./shell-env-CTYaxUs_.js";
import "./client-BOd5o3Kp.js";
import "./call-DTshgdlW.js";
import "./message-channel-DXToSaWK.js";
import "./pairing-token-B-OTYROz.js";
import "./sessions-Dn6VXn4p.js";
import "./plugins-BsEhWvLn.js";
import "./redact-Dj3r1_tS.js";
import "./errors-BmdGLmRC.js";
import "./dock-DZ6SgvTh.js";
import { o as resolveSessionTranscriptsDir } from "./paths-DtiEyjsC.js";
import "./model-auth-Byr7Gic_.js";
import "./github-copilot-token-B5y4__YM.js";
import "./models-config-CiR_RUxw.js";
import "./model-catalog-CzKsiNZA.js";
import "./bindings-BRZBH7eX.js";
import { t as formatDocsLink } from "./links-Dnmd-RS-.js";
import { n as runCommandWithRuntime } from "./cli-utils-DXfA5936.js";
import "./progress-CI19Xh3z.js";
import { t as hasExplicitOptions } from "./command-options-BFvvecvU.js";
import "./prompt-style-CZGg35Py.js";
import "./note-BIAQpZxi.js";
import "./clack-prompter-BMQT37oC.js";
import "./daemon-runtime-AkgOtHR6.js";
import "./runtime-guard-LRRZ73eX.js";
import "./onboard-helpers-t7tI9KSA.js";
import "./systemd-CNnW7Chw.js";
import "./service-DYpU1ajf.js";
import "./health-Dkwuq8ta.js";
import "./onboarding-CrCGoZ4H.js";
import "./shared-DHJR5_SP.js";
import "./auth-token-YTHu2ftn.js";
import { n as logConfigUpdated, t as formatConfigPath } from "./logging-Ev3fNJEG.js";
import "./openai-model-default-Cx6qbnBw.js";
import "./model-picker-DvP8ViSf.js";
import "./systemd-linger-CctFkWNE.js";
import "./onboard-custom-CfKvdcJ5.js";
import { t as onboardCommand } from "./onboard-BSxqEQfo.js";
import JSON5 from "json5";
import fs from "node:fs/promises";

//#region src/commands/setup.ts
async function readConfigFileRaw(configPath) {
	try {
		const raw = await fs.readFile(configPath, "utf-8");
		const parsed = JSON5.parse(raw);
		if (parsed && typeof parsed === "object") return {
			exists: true,
			parsed
		};
		return {
			exists: true,
			parsed: {}
		};
	} catch {
		return {
			exists: false,
			parsed: {}
		};
	}
}
async function setupCommand(opts, runtime = defaultRuntime) {
	const desiredWorkspace = typeof opts?.workspace === "string" && opts.workspace.trim() ? opts.workspace.trim() : void 0;
	const configPath = createConfigIO().configPath;
	const existingRaw = await readConfigFileRaw(configPath);
	const cfg = existingRaw.parsed;
	const defaults = cfg.agents?.defaults ?? {};
	const workspace = desiredWorkspace ?? defaults.workspace ?? DEFAULT_AGENT_WORKSPACE_DIR;
	const next = {
		...cfg,
		agents: {
			...cfg.agents,
			defaults: {
				...defaults,
				workspace
			}
		}
	};
	if (!existingRaw.exists || defaults.workspace !== workspace) {
		await writeConfigFile(next);
		if (!existingRaw.exists) runtime.log(`Wrote ${formatConfigPath(configPath)}`);
		else logConfigUpdated(runtime, {
			path: configPath,
			suffix: "(set agents.defaults.workspace)"
		});
	} else runtime.log(`Config OK: ${formatConfigPath(configPath)}`);
	const ws = await ensureAgentWorkspace({
		dir: workspace,
		ensureBootstrapFiles: !next.agents?.defaults?.skipBootstrap
	});
	runtime.log(`Workspace OK: ${shortenHomePath(ws.dir)}`);
	const sessionsDir = resolveSessionTranscriptsDir();
	await fs.mkdir(sessionsDir, { recursive: true });
	runtime.log(`Sessions OK: ${shortenHomePath(sessionsDir)}`);
}

//#endregion
//#region src/cli/program/register.setup.ts
function registerSetupCommand(program) {
	program.command("setup").description("Initialize ~/.symi/symi.json and the agent workspace").addHelpText("after", () => `\n${theme.muted("Docs:")} ${formatDocsLink("/cli/setup", "docs.symi.ai/cli/setup")}\n`).option("--workspace <dir>", "Agent workspace directory (default: ~/.symi/workspace; stored as agents.defaults.workspace)").option("--wizard", "Run the interactive onboarding wizard", false).option("--non-interactive", "Run the wizard without prompts", false).option("--mode <mode>", "Wizard mode: local|remote").option("--remote-url <url>", "Remote Gateway WebSocket URL").option("--remote-token <token>", "Remote Gateway token (optional)").action(async (opts, command) => {
		await runCommandWithRuntime(defaultRuntime, async () => {
			const hasWizardFlags = hasExplicitOptions(command, [
				"wizard",
				"nonInteractive",
				"mode",
				"remoteUrl",
				"remoteToken"
			]);
			if (opts.wizard || hasWizardFlags) {
				await onboardCommand({
					workspace: opts.workspace,
					nonInteractive: Boolean(opts.nonInteractive),
					mode: opts.mode,
					remoteUrl: opts.remoteUrl,
					remoteToken: opts.remoteToken
				}, defaultRuntime);
				return;
			}
			await setupCommand({ workspace: opts.workspace }, defaultRuntime);
		});
	});
}

//#endregion
export { registerSetupCommand };