import "./paths-B4BZAPZh.js";
import { C as shortenHomePath } from "./utils-h12D6i3s.js";
import { n as isRich, r as theme, t as colorize } from "./theme-CvDUwCeH.js";
import "./registry-CEok6CBD.js";
import "./subsystem-CptcaHdG.js";
import "./exec-KZG5pigM.js";
import "./agent-scope-DuRxDIxP.js";
import "./model-selection-Ci1X7yz4.js";
import "./github-copilot-token-BZDGcFI5.js";
import { t as formatCliCommand } from "./command-format-DEKzLnLg.js";
import "./boolean-BsqeuxE6.js";
import "./env-Du80aeH-.js";
import { o as readConfigFileSnapshot } from "./config-DpEIy1Iq.js";
import "./manifest-registry-DP5prNZc.js";
import "./sessions-C_NwV0mt.js";
import "./dock-kPWc7D0x.js";
import "./message-channel-CP2gEtPf.js";
import "./normalize-DqxNnJHS.js";
import "./accounts-DOhECffI.js";
import "./accounts-C1lp0M2X.js";
import "./accounts-BEp1f-Fs.js";
import "./bindings-OUZ-uS5k.js";
import "./logging-D3KTM1pH.js";
import "./plugins-DXhAnLR9.js";
import "./paths-gjLMn4eA.js";
import "./prompt-style-7y8Wox11.js";
import { o as shouldMigrateStateFromPath } from "./argv-CjkmNIur.js";
import "./catalog-DJErxxzH.js";
import "./note-Ds2RlF8N.js";
import "./plugin-auto-enable-KDBYEzE0.js";
import { t as loadAndMaybeMigrateDoctorConfig } from "./doctor-config-flow-DqR7NaLl.js";

//#region src/cli/program/config-guard.ts
const ALLOWED_INVALID_COMMANDS = new Set([
	"doctor",
	"logs",
	"health",
	"help",
	"status"
]);
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("openclaw doctor --fix"))}`);
	if (!allowInvalid) params.runtime.exit(1);
}

//#endregion
export { ensureConfigReady };