import "./paths-Cmq4jkLW.js";
import { B as theme, C as shortenHomePath } from "./utils-CcH8KYHm.js";
import "./pi-embedded-helpers-CUM4EVtq.js";
import "./reply-VKjYl5sI.js";
import "./registry-BvTskRju.js";
import { f as defaultRuntime } from "./subsystem-D5jeWpPI.js";
import "./exec-Drj_Y0q4.js";
import { C as ensureAgentWorkspace, m as DEFAULT_AGENT_WORKSPACE_DIR } from "./agent-scope-B830_s1e.js";
import "./model-selection-Cu-UzQaa.js";
import "./boolean-Wzu0-e0P.js";
import "./env-L9Bekcgw.js";
import { l as writeConfigFile, n as createConfigIO } from "./io-B0vHiKay.js";
import "./manifest-registry-CKXLdOam.js";
import "./config-CPJqIoc0.js";
import "./runner-DB15hets.js";
import "./model-catalog-DpWoHG9o.js";
import "./sandbox-l0BIS2AF.js";
import "./chrome-BHApvxyL.js";
import "./tailscale-C4Vg-KZ-.js";
import "./auth-DIeV2lKw.js";
import "./server-context-bg3lXzeH.js";
import "./frontmatter-CNh1FjpI.js";
import "./skills-CKZwNx6X.js";
import "./routes-CuHfLF5P.js";
import "./redact-D8ZUlwEM.js";
import "./errors-CaeqAr2b.js";
import "./paths-Bd5AWivy.js";
import "./ssrf-CqwYCyyw.js";
import "./image-ops-CgU_RtSG.js";
import "./store-vua1MTJe.js";
import "./ports-_-ibL5BF.js";
import "./trash-DyQphUG9.js";
import "./sessions-CUSpOb-h.js";
import "./message-channel-BZ3Pmru_.js";
import "./plugins-CK8s7WRs.js";
import "./accounts-CqvejUxk.js";
import "./accounts-DBps791j.js";
import "./accounts-D9-52N3f.js";
import "./bindings-Rfu8UC_f.js";
import "./logging-GKUQydLE.js";
import "./send-BcLigZI1.js";
import "./send-WJ_bUIhn.js";
import { o as resolveSessionTranscriptsDir } from "./paths-DRmXMWHJ.js";
import "./tool-images-BLUAnHRr.js";
import "./tool-display-D_iFHKHp.js";
import "./fetch-guard-DvcUsuQY.js";
import "./api-key-rotation-DZBE6VME.js";
import "./fetch-DNpno7P_.js";
import "./sqlite-2J_aUfpb.js";
import "./tokens-l-Si0ZDZ.js";
import "./with-timeout-RAYdcaDS.js";
import "./deliver-BZje9EiY.js";
import "./send-CS8No41i.js";
import "./reply-prefix-G6fMC6ID.js";
import "./memory-cli-Bt5IOprN.js";
import "./manager-DLaEm3Ck.js";
import "./retry-S9N3koCo.js";
import "./common-Ct4DbLC9.js";
import "./chunk-BZIV3cUY.js";
import "./markdown-tables-DJrsOaQF.js";
import "./ir-BbBSx66U.js";
import "./render-lL152pED.js";
import "./commands-registry-BqlDWjuG.js";
import "./client-DHtTLXhO.js";
import "./call-D2SDoKJk.js";
import "./send-1U4da-s7.js";
import "./outbound-attachment-GYBEI_mQ.js";
import "./fetch-foh1DT5r.js";
import "./delivery-queue-DwiVWmkG.js";
import "./channel-activity-B_aFm3Dm.js";
import "./tables-CckVE7p3.js";
import "./pairing-store-CzdfWU5W.js";
import { t as formatDocsLink } from "./links-DV6hYVY1.js";
import { n as runCommandWithRuntime } from "./cli-utils-BjM5oE0K.js";
import "./help-format-BpfPzEPd.js";
import "./progress-CWlDqwUr.js";
import "./resolve-route-B4jEqPtt.js";
import "./replies-B069McC7.js";
import "./pi-tools.policy-ki7ZRcz5.js";
import "./send-DGcH_0CW.js";
import "./onboard-helpers-Dtt6BNTy.js";
import "./prompt-style-fNcmRVVS.js";
import "./pairing-labels-CGli10Hb.js";
import "./session-cost-usage-CFTdb38x.js";
import "./nodes-screen-dtXdEZUN.js";
import "./project-context-Cdlm0139.js";
import "./control-service-CnE_Bj9V.js";
import "./stagger-DQK2wQxi.js";
import "./channel-selection-BGIcRAHU.js";
import "./runtime-guard-CISSb1XO.js";
import { t as hasExplicitOptions } from "./command-options-C1AsPlS_.js";
import "./note-DL3bb6Gk.js";
import "./clack-prompter-DXCMO1vU.js";
import "./daemon-runtime-CaeVeU9n.js";
import "./systemd-DG1iWQQd.js";
import "./service-CeQ0Lum9.js";
import "./health-C1tMKfCM.js";
import "./onboarding-BHbtH4LG.js";
import "./shared-DJIo2v-z.js";
import "./onboard-auth.credentials-CcUNd8i7.js";
import "./auth-token-DHTtZX8_.js";
import { n as logConfigUpdated, t as formatConfigPath } from "./logging-Czzgs1Mf.js";
import "./openai-model-default-tXOZlwFA.js";
import "./env-file-DUypwWSM.js";
import "./vllm-setup-C4-bM0FN.js";
import "./systemd-linger-BGAbLMdR.js";
import "./model-picker-B_geyJtH.js";
import "./onboard-custom-CNoruDe6.js";
import { t as onboardCommand } from "./onboard-tb9vPWi0.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 };