import "./paths-Cmq4jkLW.js";
import { B as theme, C as shortenHomePath, R as colorize, z as isRich } from "./utils-CcH8KYHm.js";
import "./registry-BvTskRju.js";
import "./subsystem-D5jeWpPI.js";
import "./exec-Drj_Y0q4.js";
import "./agent-scope-B830_s1e.js";
import "./model-selection-Cu-UzQaa.js";
import { t as formatCliCommand } from "./command-format-CrA15oQV.js";
import "./boolean-Wzu0-e0P.js";
import "./env-L9Bekcgw.js";
import { o as readConfigFileSnapshot } from "./io-B0vHiKay.js";
import "./manifest-registry-CKXLdOam.js";
import "./config-CPJqIoc0.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 "./paths-DRmXMWHJ.js";
import "./prompt-style-fNcmRVVS.js";
import { o as shouldMigrateStateFromPath } from "./argv-J8S_DhL2.js";
import "./catalog-IhKgYGCM.js";
import "./note-DL3bb6Gk.js";
import "./plugin-auto-enable-B7XiDEEM.js";
import { t as loadAndMaybeMigrateDoctorConfig } from "./doctor-config-flow-Bb_ouEdh.js";

//#region src/cli/program/config-guard.ts
const ALLOWED_INVALID_COMMANDS = new Set([
	"doctor",
	"logs",
	"health",
	"help",
	"status",
	"stop"
]);
const ALLOWED_INVALID_GATEWAY_SUBCOMMANDS = new Set([
	"status",
	"probe",
	"health",
	"discover",
	"call",
	"install",
	"uninstall",
	"start",
	"stop",
	"restart"
]);
let didRunDoctorConfigFlow = false;
let configSnapshotPromise = null;
function formatConfigIssues(issues) {
	return issues.map((issue) => `- ${issue.path || "<root>"}: ${issue.message}`);
}
async function getConfigSnapshot() {
	if (process.env.VITEST === "true") return readConfigFileSnapshot();
	configSnapshotPromise ??= readConfigFileSnapshot();
	return configSnapshotPromise;
}
async function ensureConfigReady(params) {
	const commandPath = params.commandPath ?? [];
	if (!didRunDoctorConfigFlow && shouldMigrateStateFromPath(commandPath)) {
		didRunDoctorConfigFlow = true;
		await loadAndMaybeMigrateDoctorConfig({
			options: { nonInteractive: true },
			confirm: async () => false
		});
	}
	const snapshot = await getConfigSnapshot();
	const commandName = commandPath[0];
	const subcommandName = commandPath[1];
	const allowInvalid = commandName ? ALLOWED_INVALID_COMMANDS.has(commandName) || commandName === "gateway" && subcommandName && ALLOWED_INVALID_GATEWAY_SUBCOMMANDS.has(subcommandName) : false;
	const issues = snapshot.exists && !snapshot.valid ? formatConfigIssues(snapshot.issues) : [];
	const legacyIssues = snapshot.legacyIssues.length > 0 ? snapshot.legacyIssues.map((issue) => `- ${issue.path}: ${issue.message}`) : [];
	if (!(snapshot.exists && !snapshot.valid)) return;
	const rich = isRich();
	const muted = (value) => colorize(rich, theme.muted, value);
	const error = (value) => colorize(rich, theme.error, value);
	const heading = (value) => colorize(rich, theme.heading, value);
	const commandText = (value) => colorize(rich, theme.command, value);
	params.runtime.error(heading("Config invalid"));
	params.runtime.error(`${muted("File:")} ${muted(shortenHomePath(snapshot.path))}`);
	if (issues.length > 0) {
		params.runtime.error(muted("Problem:"));
		params.runtime.error(issues.map((issue) => `  ${error(issue)}`).join("\n"));
	}
	if (legacyIssues.length > 0) {
		params.runtime.error(muted("Legacy config keys detected:"));
		params.runtime.error(legacyIssues.map((issue) => `  ${error(issue)}`).join("\n"));
	}
	params.runtime.error("");
	params.runtime.error(`${muted("Run:")} ${commandText(formatCliCommand("coderclaw doctor --fix"))}`);
	if (!allowInvalid) params.runtime.exit(1);
}

//#endregion
export { ensureConfigReady };