import { n as defaultRuntime } from "./runtime-CgZZEqw7.js";
import { r as theme } from "./theme-AvNURkOg.js";
import { m as resolveDefaultAgentId } from "./agent-scope-BA9YeMqS.js";
import { t as danger } from "./globals-D7B2Z7Cq.js";
import { l as readConfigFileSnapshot } from "./io-B_JNqEVl.js";
import { l as SOUL_PRESETS } from "./zod-schema.agent-runtime-CZAExHGM.js";
import { r as replaceConfigFile } from "./config-BR67jvKt.js";
import { n as setSoulInConfig, t as removeSoulFromConfig } from "./soul-config-helpers-D_ht473j.js";
import { fileURLToPath } from "node:url";
import { existsSync, readFileSync } from "node:fs";
import { dirname, join } from "node:path";
//#region src/cli/soul-cli.ts
const __dirname = dirname(fileURLToPath(import.meta.url));
const PRESETS_DIR = existsSync(join(__dirname, "soul-presets")) ? join(__dirname, "soul-presets") : join(__dirname, "..", "soul-presets");
const SOUL_PRESET_NAMES = {
	intj: "建筑师 (Architect)",
	intp: "逻辑学家 (Logician)",
	entj: "指挥官 (Commander)",
	entp: "辩论家 (Debater)",
	infj: "提倡者 (Advocate)",
	infp: "调停者 (Mediator)",
	enfj: "主人公 (Protagonist)",
	enfp: "竞选者 (Campaigner)",
	istj: "物流师 (Logistician)",
	isfj: "守卫者 (Defender)",
	estj: "总经理 (Executive)",
	esfj: "执政官 (Consul)",
	istp: "鉴赏家 (Virtuoso)",
	isfp: "探险家 (Adventurer)",
	estp: "企业家 (Entrepreneur)",
	esfp: "表演者 (Entertainer)"
};
function resolvePresetKey(input) {
	const lower = input.toLowerCase().trim();
	if (SOUL_PRESETS.includes(lower)) return lower;
	return null;
}
function loadPresetContent(preset) {
	return readFileSync(join(PRESETS_DIR, `${preset}.md`), "utf-8");
}
function resolveCurrentPreset(config, agentId) {
	const entry = config.agents?.list?.find((e) => e.id.toLowerCase() === agentId.toLowerCase());
	if (entry?.soul?.preset) return entry.soul.preset;
	if (config.agents?.defaults?.soul?.preset) return config.agents.defaults.soul.preset;
	return config.soul?.preset;
}
async function resolveEffectiveAgentId(sourceConfig, agentId) {
	if (agentId) return agentId;
	return resolveDefaultAgentId(sourceConfig);
}
async function runSoulList(agentId) {
	const snapshot = await readConfigFileSnapshot();
	const effectiveAgentId = await resolveEffectiveAgentId(snapshot.sourceConfig, agentId);
	const currentPreset = resolveCurrentPreset(snapshot.sourceConfig, effectiveAgentId);
	for (const key of SOUL_PRESETS) {
		const name = SOUL_PRESET_NAMES[key];
		const isCurrent = key === currentPreset;
		const prefix = isCurrent ? theme.accent("→") : " ";
		const suffix = isCurrent ? ` ${theme.muted("(current)")}` : "";
		defaultRuntime.log(`  ${prefix} ${theme.heading(key.toUpperCase().padEnd(4))}  ${name}${suffix}`);
	}
	defaultRuntime.log("");
	defaultRuntime.log(`Use ${theme.command(`kaijibot soul set <type> --agent ${effectiveAgentId}`)} to select a soul preset.`);
	defaultRuntime.log(`Use ${theme.command(`kaijibot soul unset --agent ${effectiveAgentId}`)} to revert to the default SOUL.md.`);
}
async function runSoulGet(agentId) {
	const snapshot = await readConfigFileSnapshot();
	const effectiveAgentId = await resolveEffectiveAgentId(snapshot.sourceConfig, agentId);
	const currentPreset = resolveCurrentPreset(snapshot.sourceConfig, effectiveAgentId);
	if (!currentPreset) {
		defaultRuntime.log("No soul preset is currently active. Using default SOUL.md.");
		return;
	}
	const name = SOUL_PRESET_NAMES[currentPreset];
	defaultRuntime.log(`Current soul preset: ${theme.heading(currentPreset.toUpperCase())} — ${theme.success(name)} [agent: ${effectiveAgentId}]`);
	defaultRuntime.log("");
	defaultRuntime.log("--- Preview ---");
	defaultRuntime.log(loadPresetContent(currentPreset));
}
async function runSoulSet(presetInput, agentId) {
	const preset = resolvePresetKey(presetInput);
	if (!preset) {
		defaultRuntime.error(danger(`Unknown soul preset: "${presetInput}". Valid presets: ${SOUL_PRESETS.join(", ")}`));
		defaultRuntime.exit(1);
		return;
	}
	const sourceConfig = { ...(await readConfigFileSnapshot()).sourceConfig };
	const effectiveAgentId = await resolveEffectiveAgentId(sourceConfig, agentId);
	setSoulInConfig(sourceConfig, effectiveAgentId, preset);
	await replaceConfigFile({ nextConfig: sourceConfig });
	const name = SOUL_PRESET_NAMES[preset];
	defaultRuntime.log(`Soul preset set to ${theme.heading(preset.toUpperCase())} — ${theme.success(name)} [agent: ${effectiveAgentId}]`);
	defaultRuntime.log("");
	defaultRuntime.log("Change takes effect on the next message (hot-reload).");
}
async function runSoulUnset(agentId) {
	const sourceConfig = { ...(await readConfigFileSnapshot()).sourceConfig };
	const effectiveAgentId = await resolveEffectiveAgentId(sourceConfig, agentId);
	if (!resolveCurrentPreset(sourceConfig, effectiveAgentId)) {
		defaultRuntime.log("No soul preset is currently active.");
		return;
	}
	removeSoulFromConfig(sourceConfig, effectiveAgentId);
	await replaceConfigFile({ nextConfig: sourceConfig });
	defaultRuntime.log(`Soul preset removed [agent: ${effectiveAgentId}]. Reverted to default SOUL.md.`);
	defaultRuntime.log("");
	defaultRuntime.log("Change takes effect on the next message (hot-reload).");
}
function registerSoulCli(program) {
	const soul = program.command("soul").description("Manage soul presets (MBTI-based personality profiles)").option("--agent <id>", "Target a specific agent (default: resolved from config)").action(async (opts) => {
		await runSoulList(opts.agent);
	});
	soul.command("list").description("List all available soul presets").option("--agent <id>", "Target a specific agent").action(async (opts) => {
		await runSoulList(opts.agent);
	});
	soul.command("get").description("Show the currently active soul preset").option("--agent <id>", "Target a specific agent").action(async (opts) => {
		await runSoulGet(opts.agent);
	});
	soul.command("set").description("Set the active soul preset (e.g., kaijibot soul set intj)").argument("<type>", "MBTI type (e.g., intj, entp, infj)").option("--agent <id>", "Target a specific agent").action(async (type, opts) => {
		await runSoulSet(type, opts.agent);
	});
	soul.command("unset").description("Remove the soul preset and revert to default SOUL.md").option("--agent <id>", "Target a specific agent").action(async (opts) => {
		await runSoulUnset(opts.agent);
	});
}
//#endregion
export { registerSoulCli };
