import "./paths-CutK9mvd.js";
import "./registry-DScA7GM7.js";
import { S as shortenHomePath, z as theme } from "./utils-BXq128Ep.js";
import { f as defaultRuntime } from "./subsystem-C0jWWDsO.js";
import "./boolean-DtWR5bt3.js";
import "./auth-profiles-Ci0m1nDj.js";
import "./exec-BfxXkFYo.js";
import { C as ensureAgentWorkspace, m as DEFAULT_AGENT_WORKSPACE_DIR } from "./agent-scope-C9lu5zYP.js";
import "./frontmatter-B3s_6fwe.js";
import "./skills-CXwgvGur.js";
import "./manifest-registry-CLylU6AG.js";
import { c as writeConfigFile, t as createConfigIO } from "./io-CD_zuK0D.js";
import "./config-DiWsrVKe.js";
import "./client-DcGx78uR.js";
import "./call-DfwL1J3H.js";
import "./message-channel-f25bXyKM.js";
import "./subagent-registry-C2sM5Cyw.js";
import "./sessions-Mt8f23sr.js";
import "./tokens-D7kBZYPD.js";
import "./plugins-CiXA7mtz.js";
import "./accounts-DlyBFGv4.js";
import "./bindings-DjR4yXgA.js";
import "./logging-pqyrk15z.js";
import "./send-BSyWSMGa.js";
import "./send-Bv-Ugxhn.js";
import "./with-timeout-CMUEB0p-.js";
import "./deliver-CkqGoWHC.js";
import "./accounts-DNDn42nY.js";
import "./send-Dbw-1Nv6.js";
import "./image-ops-DX6lds-i.js";
import "./models-config-6QdRrV1c.js";
import "./pi-embedded-helpers-_sjZz5r6.js";
import "./sandbox-Cgi5GRjr.js";
import "./chrome-CCvAKFA-.js";
import "./tailscale-BrDWEIwI.js";
import "./auth-B80pI0Uh.js";
import "./server-context-CPsi2755.js";
import "./routes-MRFMs3Hh.js";
import "./redact-CFj039qF.js";
import "./errors-8VQXhB4u.js";
import "./paths-BKv1HwMq.js";
import "./ssrf-weuQnXiO.js";
import "./store-BgSeZwv0.js";
import "./ports-BTCCmBPN.js";
import "./trash-RPQQ3ep-.js";
import "./accounts-IQLo28rM.js";
import { o as resolveSessionTranscriptsDir } from "./paths-mQAx86lp.js";
import "./tool-images-BtNaJzDf.js";
import "./reply-prefix-CTkJoXqQ.js";
import "./memory-cli-DhQ3BMgb.js";
import "./manager-BUwPVwMU.js";
import "./gemini-auth-D_WaKG1u.js";
import "./sqlite-B6wrIncg.js";
import "./retry-eZfsfPJw.js";
import "./common-Cb-qblwg.js";
import "./chunk-CCkvwRI1.js";
import "./markdown-tables-BmPL6gxD.js";
import "./fetch-guard-DgkwAf8y.js";
import "./fetch-Cyj7bUtQ.js";
import "./ir-Zi6SqHuv.js";
import "./render-lL152pED.js";
import "./commands-registry-Bb7wps-U.js";
import "./runner-Dq5pUJw7.js";
import "./tool-display-C_2MZJqm.js";
import "./model-catalog-I10Mw32M.js";
import "./session-utils-rRgbDHdn.js";
import "./pi-tools.policy-Dl38gqLG.js";
import "./pairing-store-BFjZsQfU.js";
import "./fetch-FKiEPHsu.js";
import "./nodes-screen-aqjJyJKK.js";
import "./session-cost-usage-CFkSIl99.js";
import "./send-Dm5td_ud.js";
import "./outbound-attachment-DbnUFGg7.js";
import "./delivery-queue-CrM_98Zj.js";
import "./project-context-CDgzGX_i.js";
import "./control-service-Da4pvhRF.js";
import "./stagger-vDSk1wpM.js";
import "./channel-selection-C9HQldEr.js";
import "./send-DVTkNDIS.js";
import "./resolve-route-DfP8shJU.js";
import "./channel-activity-CjNqpxzA.js";
import "./tables-D0RFg7_p.js";
import { t as formatDocsLink } from "./links-Crq8FXFM.js";
import { n as runCommandWithRuntime } from "./cli-utils-m10N0YX2.js";
import "./help-format-xwRPMYQn.js";
import "./progress-Czj96fRf.js";
import "./replies-D7zeCuNw.js";
import "./onboard-helpers-BfUVfjJm.js";
import "./prompt-style-gyFdZw9z.js";
import "./pairing-labels-DcHuGxfy.js";
import { t as hasExplicitOptions } from "./command-options-C9glxEvR.js";
import "./note-DBwiNGlo.js";
import "./clack-prompter-H3S9tpcw.js";
import "./daemon-runtime-CZOtxU0-.js";
import "./runtime-guard-CV_MGdwf.js";
import "./systemd-DJnyEJey.js";
import "./service-B4bQU0ni.js";
import "./health-DSAmjFa7.js";
import "./onboarding-nAJRLgvc.js";
import "./shared-BRm-WFzj.js";
import "./onboard-auth.credentials-B9r-8gdL.js";
import "./auth-token-DWwV3TwE.js";
import { n as logConfigUpdated, t as formatConfigPath } from "./logging-D0STCWnR.js";
import "./openai-model-default-BMg8fxId.js";
import "./env-file-Dw1DtQK9.js";
import "./vllm-setup-Cr2hS61M.js";
import "./systemd-linger-DkqrShRC.js";
import "./model-picker-DZ8o94oV.js";
import "./onboard-custom-D3BtKuEY.js";
import { t as onboardCommand } from "./onboard-DjR6mcHR.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 ~/.coderclaw/coderclaw.json and the agent workspace").addHelpText("after", () => `\n${theme.muted("Docs:")} ${formatDocsLink("/cli/setup", "docs.coderclaw.ai/cli/setup")}\n`).option("--workspace <dir>", "Agent workspace directory (default: ~/.coderclaw/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 };