import { i as normalizeLowercaseStringOrEmpty, s as normalizeOptionalString } from "./string-coerce-CIT1-v_a.js";
import path from "node:path";
//#region src/daemon/constants.ts
const GATEWAY_LAUNCH_AGENT_LABEL = "ai.kaijibot.gateway";
const GATEWAY_SYSTEMD_SERVICE_NAME = "kaijibot-gateway";
const GATEWAY_WINDOWS_TASK_NAME = "KaijiBot Gateway";
const GATEWAY_WATCHDOG_TASK_NAME = "KaijiBot Gateway Watchdog";
const GATEWAY_SERVICE_MARKER = "kaijibot";
const GATEWAY_SERVICE_KIND = "gateway";
const NODE_LAUNCH_AGENT_LABEL = "ai.kaijibot.node";
const NODE_SYSTEMD_SERVICE_NAME = "kaijibot-node";
const NODE_WINDOWS_TASK_NAME = "KaijiBot Node";
const NODE_SERVICE_MARKER = "kaijibot";
const NODE_SERVICE_KIND = "node";
const NODE_WINDOWS_TASK_SCRIPT_NAME = "node.cmd";
const LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES = ["clawdbot-gateway"];
function normalizeGatewayProfile(profile) {
	const trimmed = profile?.trim();
	if (!trimmed || normalizeLowercaseStringOrEmpty(trimmed) === "default") return null;
	return trimmed;
}
function resolveGatewayProfileSuffix(profile) {
	const normalized = normalizeGatewayProfile(profile);
	return normalized ? `-${normalized}` : "";
}
function resolveGatewayLaunchAgentLabel(profile) {
	const normalized = normalizeGatewayProfile(profile);
	if (!normalized) return GATEWAY_LAUNCH_AGENT_LABEL;
	return `ai.kaijibot.${normalized}`;
}
function resolveLegacyGatewayLaunchAgentLabels(profile) {
	return [];
}
function resolveGatewaySystemdServiceName(profile) {
	const suffix = resolveGatewayProfileSuffix(profile);
	if (!suffix) return GATEWAY_SYSTEMD_SERVICE_NAME;
	return `kaijibot-gateway${suffix}`;
}
function resolveGatewayWindowsTaskName(profile) {
	const normalized = normalizeGatewayProfile(profile);
	if (!normalized) return GATEWAY_WINDOWS_TASK_NAME;
	return `KaijiBot Gateway (${normalized})`;
}
function resolveGatewayWatchdogTaskName(profile) {
	const normalized = normalizeGatewayProfile(profile);
	if (!normalized) return GATEWAY_WATCHDOG_TASK_NAME;
	return `KaijiBot Gateway Watchdog (${normalized})`;
}
function formatGatewayServiceDescription(params) {
	const profile = normalizeGatewayProfile(params?.profile);
	const version = params?.version?.trim();
	const parts = [];
	if (profile) parts.push(`profile: ${profile}`);
	if (version) parts.push(`v${version}`);
	if (parts.length === 0) return "KaijiBot Gateway";
	return `KaijiBot Gateway (${parts.join(", ")})`;
}
function resolveGatewayServiceDescription(params) {
	return params.description ?? formatGatewayServiceDescription({
		profile: params.env.KAIJIBOT_PROFILE,
		version: params.environment?.KAIJIBOT_SERVICE_VERSION ?? params.env.KAIJIBOT_SERVICE_VERSION
	});
}
function resolveNodeLaunchAgentLabel() {
	return NODE_LAUNCH_AGENT_LABEL;
}
function resolveNodeSystemdServiceName() {
	return NODE_SYSTEMD_SERVICE_NAME;
}
function resolveNodeWindowsTaskName() {
	return NODE_WINDOWS_TASK_NAME;
}
function formatNodeServiceDescription(params) {
	const version = params?.version?.trim();
	if (!version) return "KaijiBot Node Host";
	return `KaijiBot Node Host (v${version})`;
}
//#endregion
//#region src/daemon/paths.ts
const windowsAbsolutePath = /^[a-zA-Z]:[\\/]/;
const windowsUncPath = /^\\\\/;
function resolveHomeDir(env) {
	const home = normalizeOptionalString(env.HOME) || normalizeOptionalString(env.USERPROFILE);
	if (!home) throw new Error("Missing HOME");
	return home;
}
function resolveUserPathWithHome(input, home) {
	const trimmed = input.trim();
	if (!trimmed) return trimmed;
	if (trimmed.startsWith("~")) {
		if (!home) throw new Error("Missing HOME");
		const expanded = trimmed.replace(/^~(?=$|[\\/])/, home);
		return path.resolve(expanded);
	}
	if (windowsAbsolutePath.test(trimmed) || windowsUncPath.test(trimmed)) return trimmed;
	return path.resolve(trimmed);
}
function resolveGatewayStateDir(env) {
	const override = normalizeOptionalString(env.KAIJIBOT_STATE_DIR);
	if (override) return resolveUserPathWithHome(override, override.startsWith("~") ? resolveHomeDir(env) : void 0);
	const home = resolveHomeDir(env);
	const suffix = resolveGatewayProfileSuffix(env.KAIJIBOT_PROFILE);
	return path.join(home, `.kaijibot${suffix}`);
}
//#endregion
export { resolveNodeLaunchAgentLabel as _, GATEWAY_SERVICE_MARKER as a, NODE_SERVICE_MARKER as c, resolveGatewayLaunchAgentLabel as d, resolveGatewayServiceDescription as f, resolveLegacyGatewayLaunchAgentLabels as g, resolveGatewayWindowsTaskName as h, GATEWAY_SERVICE_KIND as i, NODE_WINDOWS_TASK_SCRIPT_NAME as l, resolveGatewayWatchdogTaskName as m, resolveHomeDir as n, LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES as o, resolveGatewaySystemdServiceName as p, GATEWAY_LAUNCH_AGENT_LABEL as r, NODE_SERVICE_KIND as s, resolveGatewayStateDir as t, formatNodeServiceDescription as u, resolveNodeSystemdServiceName as v, resolveNodeWindowsTaskName as y };
