import { c as normalizeOptionalString } from "./string-coerce-DKw2K5wM.js";
import { d as resolveGatewayProfileSuffix } from "./constants-CeTJi0TJ.js";
import path from "node:path";
//#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.NODMIX_STATE_DIR);
	if (override) return resolveUserPathWithHome(override, override.startsWith("~") ? resolveHomeDir(env) : void 0);
	const home = resolveHomeDir(env);
	const suffix = resolveGatewayProfileSuffix(env.NODMIX_PROFILE);
	return path.join(home, `.nodmix${suffix}`);
}
function resolveGatewayTaskScriptPath(env) {
	const override = normalizeOptionalString(env.NODMIX_TASK_SCRIPT);
	if (override) return override;
	const scriptName = normalizeOptionalString(env.NODMIX_TASK_SCRIPT_NAME) || "gateway.cmd";
	if (/[/\\]|\.\./.test(scriptName)) throw new Error(`NODMIX_TASK_SCRIPT_NAME must be a file name only, not a path: ${scriptName}`);
	return path.join(resolveGatewayStateDir(env), scriptName);
}
//#endregion
export { resolveGatewayTaskScriptPath as n, resolveHomeDir as r, resolveGatewayStateDir as t };
