import { j as theme } from "./registry-Sbac4a4z.js";
import "./paths-By0XjHBk.js";
import { v as shortenHomePath } from "./utils-CTPsqyE_.js";
import { f as defaultRuntime } from "./subsystem-CHbO_DkH.js";
import "./auth-profiles-Ce7R_25e.js";
import "./exec-DuYbft0z.js";
import { C as ensureAgentWorkspace, m as DEFAULT_AGENT_WORKSPACE_DIR } from "./agent-scope-DOrZjOnW.js";
import "./model-selection-7wdmO-Iw.js";
import "./file-lock-BVUYnlQv.js";
import "./agent-paths-BbZSGrUA.js";
import { l as writeConfigFile, r as createConfigIO } from "./config-B4jkreCN.js";
import "./boolean-CbZoNRMn.js";
import "./env-BRnPI9sO.js";
import "./shell-env-Dc3iU7HK.js";
import "./manifest-registry-D1MLZEjS.js";
import "./redact-B3O-RBO1.js";
import "./errors-bI7Tdx75.js";
import "./sessions-Dxf5Kjig.js";
import "./dock-hF45sewN.js";
import "./message-channel-DgjXWzf-.js";
import "./plugins-CF5skkHh.js";
import { o as resolveSessionTranscriptsDir } from "./paths-BtvQ7JvU.js";
import "./model-auth-1EAQvYRv.js";
import "./github-copilot-token-D-FdDfee.js";
import "./models-config-B5Xxy-c-.js";
import "./client-DH75FlQF.js";
import "./call-AZ9vNhST.js";
import "./pairing-token-Dcy37-4s.js";
import "./model-catalog-IWi6-nY9.js";
import "./bindings-DIhsYGa_.js";
import { t as formatDocsLink } from "./links-Bqr0FxXx.js";
import { n as runCommandWithRuntime } from "./cli-utils-CuyGL3rq.js";
import "./progress-swt2y17x.js";
import "./runtime-guard-CWA00ZXa.js";
import { t as hasExplicitOptions } from "./command-options-DDDwkB9t.js";
import "./prompt-style-olk5zI5k.js";
import "./note-D9FXI2qW.js";
import "./clack-prompter-BpcKjPIh.js";
import "./daemon-runtime-DlQla_dA.js";
import "./onboard-helpers-7IGQ8nAM.js";
import "./systemd-CUIM0n5P.js";
import "./service-e6MzlLCd.js";
import "./health-7UqIGG8U.js";
import "./onboarding-DxZAGVkD.js";
import "./shared-COtpr3Ad.js";
import "./auth-token-Cr28IDKY.js";
import { n as logConfigUpdated, t as formatConfigPath } from "./logging-DDnP1BTp.js";
import "./openai-model-default-BkuuK-Di.js";
import "./model-picker-Cf6UgQEh.js";
import "./systemd-linger-BzYk7A6M.js";
import "./onboard-custom-BcRYreNG.js";
import { t as onboardCommand } from "./onboard-DnwWawQO.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 };