#!/usr/bin/env node

import { homedir } from "node:os";
import { basename, dirname, join, parse } from "node:path";

//#region src/config/paths.ts
const MAX_ACCOUNT_PATH_SEGMENT_LENGTH = 128;
const FALLBACK_ACCOUNT_HOST_KEY = "unauthenticated";
const FALLBACK_ACCOUNT_USER_KEY = "local";
const ATS_HOME_ENV_KEY = "ATS_HOME";
const ATS_ENV_PRESET_ENV_KEY = "ATS_ENV_PRESET";
const DEFAULT_ATS_ENV_PRESET = "prod";
const ATS_DEFAULT_HOME_DIR_BY_PRESET = {
	prod: ".ats",
	"dev-local": ".ats-dev",
	canary: ".ats-canary"
};
const ATS_ENV_PRESET_BY_ENTRY_NAME = {
	"agent-transport-system": "prod",
	ats: "prod",
	"ats-dev": "dev-local",
	"ats-canary": "canary"
};
function normalizeAtsProfileId(value) {
	const raw = String(value ?? "").trim();
	if (raw.length === 0) throw new Error("atsProfileId is required");
	const safe = raw.replace(/[^a-zA-Z0-9._-]+/g, "_").toLowerCase();
	if (!safe || safe === "." || safe === "..") throw new Error("atsProfileId is invalid");
	if (safe.length > MAX_ACCOUNT_PATH_SEGMENT_LENGTH) throw new Error("atsProfileId is too long");
	return safe;
}
function resolveAtsEnvPreset(input = {}) {
	const fromEnv = normalizeAtsEnvPreset((input.env ?? process.env)[ATS_ENV_PRESET_ENV_KEY]);
	if (fromEnv) return fromEnv;
	return resolveAtsEnvPresetFromArgv(input.argv ?? process.argv);
}
function resolveDefaultAtsHomeDirname(preset) {
	return ATS_DEFAULT_HOME_DIR_BY_PRESET[preset];
}
function resolveAtsRootDir(input = {}) {
	const env = input.env ?? process.env;
	const fromEnv = String(env[ATS_HOME_ENV_KEY] ?? "").trim();
	if (fromEnv) return fromEnv;
	const preset = resolveAtsEnvPreset(input);
	return join(resolveHomeDirectory(input), resolveDefaultAtsHomeDirname(preset));
}
function atsRootDir() {
	return resolveAtsRootDir();
}
function atsBackupsDir() {
	const root = atsRootDir();
	return join(dirname(root), `${basename(root)}-backups`);
}
function accountsDir() {
	return join(atsRootDir(), "accounts");
}
function deviceDir(input = {}) {
	return join(resolveAtsRootDir(input), "device");
}
function deviceSkillsDir(input = {}) {
	return join(deviceDir(input), "skills");
}
function devicePreferencesPath(input = {}) {
	return join(deviceDir(input), "preferences.json");
}
function deviceAgentRuntimePath(input = {}) {
	return join(deviceDir(input), "agent-runtime.json");
}
function deviceProvidersDir(input = {}) {
	return join(deviceDir(input), "providers");
}
function deviceProviderDir(providerId, input = {}) {
	return join(deviceProvidersDir(input), normalizePathSegment(providerId, "unknown"));
}
function deviceProviderProfilesDir(providerId, input = {}) {
	return join(deviceProviderDir(providerId, input), "profiles");
}
function deviceProviderProfileDir(providerId, profileId, input = {}) {
	return join(deviceProviderProfilesDir(providerId, input), normalizePathSegment(profileId, "unknown"));
}
function deviceOpenClawProfileDir(profileId, input = {}) {
	return deviceProviderProfileDir("openclaw", profileId, input);
}
function deviceOpenClawProfileWorkspaceDir(profileId, input = {}) {
	return join(deviceOpenClawProfileDir(profileId, input), "workspace");
}
function deviceOpenClawProfileBootstrapPath(profileId, input = {}) {
	return join(deviceOpenClawProfileDir(profileId, input), "bootstrap.json");
}
function deviceSkillsStorePath(input = {}) {
	return join(deviceSkillsDir(input), "store.json");
}
function deviceSkillsCustomTargetsPath(input = {}) {
	return join(deviceSkillsDir(input), "custom-targets.json");
}
function deviceSkillsInstallsPath(input = {}) {
	return join(deviceSkillsDir(input), "installs.json");
}
function legacyRuntimeConfigPath(input = {}) {
	return join(resolveAtsRootDir(input), "config.json");
}
function normalizeAccountHostKey(value) {
	return normalizePathSegment(value, FALLBACK_ACCOUNT_HOST_KEY);
}
function normalizeAccountUserKey(value) {
	return normalizePathSegment(value, FALLBACK_ACCOUNT_USER_KEY);
}
function normalizeAccountKey(input) {
	return {
		hostKey: normalizeAccountHostKey(input.hostKey),
		userKey: normalizeAccountUserKey(input.userKey)
	};
}
function accountDir(account) {
	const normalized = normalizeAccountKey(account);
	return join(accountsDir(), normalized.hostKey, normalized.userKey);
}
function accountProfilesDir(account) {
	return join(accountDir(account), "profiles");
}
function accountProfileDir(account, atsProfileId) {
	return join(accountProfilesDir(account), normalizeAtsProfileId(atsProfileId));
}
function accountProfileViewPath(account, atsProfileId) {
	return join(accountProfileDir(account, atsProfileId), "view.json");
}
function accountProfileLocalMetaPath(account, atsProfileId) {
	return join(accountProfileDir(account, atsProfileId), "local-meta.json");
}
function accountProfileWorkspacePath(account, atsProfileId) {
	return join(accountProfileDir(account, atsProfileId), "workspace");
}
function accountProfileSpaceCacheDir(account, atsProfileId) {
	return join(accountProfileDir(account, atsProfileId), "space-cache");
}
function legacyAccountProfileSpacesDir(account, atsProfileId) {
	return join(accountProfileDir(account, atsProfileId), "spaces");
}
function profileDir(atsProfileId) {
	return join(atsRootDir(), "profiles", atsProfileId);
}
function runtimeDir(input = {}) {
	return join(resolveAtsRootDir(input), "runtime");
}
function runtimeLogsDir() {
	return join(runtimeDir(), "logs");
}
function sessionsDir() {
	return join(runtimeDir(), "sessions");
}
function systemDir(input = {}) {
	return join(resolveAtsRootDir(input), "system");
}
function skillsDir() {
	return join(atsRootDir(), "skills");
}
function spacesDir(atsProfileId) {
	return join(profileDir(atsProfileId), "spaces");
}
function locksDir(atsProfileId) {
	return join(runtimeDir(), "locks", atsProfileId);
}
function profileViewPath(atsProfileId) {
	return join(profileDir(atsProfileId), "view.json");
}
function normalizePathSegment(value, fallback) {
	const normalized = String(value ?? "").trim().toLowerCase();
	if (!normalized) return fallback;
	const safe = normalized.replace(/[^a-z0-9._-]+/g, "_");
	if (!safe) return fallback;
	if (safe === "." || safe === "..") return fallback;
	return safe.slice(0, MAX_ACCOUNT_PATH_SEGMENT_LENGTH);
}
function resolveAtsEnvPresetFromArgv(argv) {
	return resolveKnownAtsEnvPresetFromArgv(argv) ?? DEFAULT_ATS_ENV_PRESET;
}
function resolveKnownAtsEnvPresetFromArgv(argv) {
	const entryName = resolveAtsCliEntryNameFromArgv(argv);
	if (!entryName) return null;
	return ATS_ENV_PRESET_BY_ENTRY_NAME[entryName] ?? null;
}
function resolveAtsCliEntryNameFromArgv(argv) {
	return resolveCliEntryName(argv[1]);
}
function resolveCliEntryName(value) {
	const raw = String(value ?? "").trim();
	if (!raw) return null;
	const normalizedName = parse(raw).name.trim().toLowerCase();
	if (!normalizedName) return null;
	if (!isAtsCliEntryName(normalizedName)) return null;
	return normalizedName;
}
function isAtsCliEntryName(value) {
	return value in ATS_ENV_PRESET_BY_ENTRY_NAME;
}
function normalizeAtsEnvPreset(value) {
	const normalized = String(value ?? "").trim().toLowerCase();
	if (normalized === "prod" || normalized === "dev-local" || normalized === "canary") return normalized;
	return null;
}
function resolveHomeDirectory(input) {
	if (input.homeDir) return input.homeDir;
	const env = input.env ?? process.env;
	const fromEnv = String(env.HOME ?? "").trim();
	if (fromEnv) return fromEnv;
	return homedir();
}

//#endregion
export { runtimeDir as A, normalizeAtsProfileId as C, resolveAtsEnvPreset as D, resolveAtsCliEntryNameFromArgv as E, systemDir as F, sessionsDir as M, skillsDir as N, resolveAtsRootDir as O, spacesDir as P, normalizeAccountKey as S, profileViewPath as T, deviceSkillsInstallsPath as _, accountProfileWorkspacePath as a, legacyRuntimeConfigPath as b, atsRootDir as c, deviceOpenClawProfileBootstrapPath as d, deviceOpenClawProfileDir as f, deviceSkillsDir as g, deviceSkillsCustomTargetsPath as h, accountProfileViewPath as i, runtimeLogsDir as j, resolveDefaultAtsHomeDirname as k, deviceAgentRuntimePath as l, devicePreferencesPath as m, accountProfileLocalMetaPath as n, accountsDir as o, deviceOpenClawProfileWorkspaceDir as p, accountProfileSpaceCacheDir as r, atsBackupsDir as s, ATS_HOME_ENV_KEY as t, deviceDir as u, deviceSkillsStorePath as v, profileDir as w, locksDir as x, legacyAccountProfileSpacesDir as y };
//# sourceMappingURL=paths-DFiopT5Y.js.map