#!/usr/bin/env node

// src/common/paths.ts
import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { homedir } from "os";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
var HOME = homedir();
var MODULE_DIR = dirname(fileURLToPath(import.meta.url));
var SOURCE_FONT_ASSETS_DIR = resolve(MODULE_DIR, "../../assets/fonts");
var DIST_FONT_ASSETS_DIR = resolve(MODULE_DIR, "../assets/fonts");
var DEFAULT_FONT_FAMILY = "sarasa-fixed-sc";
var DEFAULT_PROXY_PROFILE = "default";
function normalizeProxyProfileName(value) {
  const normalized = value?.trim() || DEFAULT_PROXY_PROFILE;
  if (normalized === "." || normalized === ".." || !/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/.test(normalized)) {
    throw new Error(
      `Invalid dev-anywhere profile "${normalized}". Use 1-64 letters, numbers, ".", "_" or "-".`
    );
  }
  return normalized;
}
function readProxyProfileNameFromArgv(argv) {
  const args = [...argv];
  while (args[0] === "--") args.shift();
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "claude" || arg === "codex") return void 0;
    if (arg === "--profile") {
      const next = args[i + 1];
      return next && !next.startsWith("-") ? next : void 0;
    }
    if (arg.startsWith("--profile=")) {
      return arg.slice("--profile=".length);
    }
  }
  return void 0;
}
function resolveProxyProfileName(argv = process.argv.slice(2), home = HOME) {
  return normalizeProxyProfileName(
    readProxyProfileNameFromArgv(argv) ?? readDefaultProfileFromConfig(home)
  );
}
function readDefaultProfileFromConfig(home) {
  try {
    const parsed = JSON.parse(readFileSync(`${home}/.dev-anywhere/config.json`, "utf-8"));
    return typeof parsed.defaultProfile === "string" ? parsed.defaultProfile : void 0;
  } catch {
    return void 0;
  }
}
function buildProxyProfilePaths(home, profileName) {
  const normalizedProfile = normalizeProxyProfileName(profileName);
  const appDir = `${home}/.dev-anywhere`;
  const isDefaultProfile = normalizedProfile === DEFAULT_PROXY_PROFILE;
  const profileDir = isDefaultProfile ? appDir : `${appDir}/profiles/${normalizedProfile}`;
  const runDir = isDefaultProfile ? `${appDir}/run` : `${profileDir}/run`;
  const stateDir = isDefaultProfile ? `${appDir}/state` : `${profileDir}/state`;
  const dataDir = isDefaultProfile ? `${appDir}/data` : `${profileDir}/data`;
  const logDir = isDefaultProfile ? `${appDir}/logs` : `${profileDir}/logs`;
  const relayDataDir = `${appDir}/relay-data`;
  return {
    profileName: normalizedProfile,
    isDefaultProfile,
    appDir,
    profileDir,
    configPath: `${appDir}/config.json`,
    runDir,
    sockPath: `${runDir}/dev-anywhere.sock`,
    pidPath: `${runDir}/dev-anywhere.pid`,
    stoppedPath: `${runDir}/stopped`,
    desiredRelayPath: `${runDir}/desired-relay`,
    stateDir,
    sessionsPath: `${stateDir}/sessions.json`,
    historyMetadataPath: `${stateDir}/history-metadata.json`,
    hookRegistryPath: `${stateDir}/hooks.json`,
    dataDir,
    proxyIdPath: isDefaultProfile ? `${appDir}/proxy-id` : `${profileDir}/proxy-id`,
    logDir,
    serviceLogPath: `${logDir}/service.log`,
    relayDataDir,
    fontDir: `${relayDataDir}/fonts`
  };
}
function defaultHookPortForProfile(profileName) {
  const normalizedProfile = normalizeProxyProfileName(profileName);
  if (normalizedProfile === DEFAULT_PROXY_PROFILE) return 17654;
  let hash = 0;
  for (const char of normalizedProfile) {
    hash = (hash * 31 + char.charCodeAt(0)) % 1e3;
  }
  return 17655 + hash;
}
var PROFILE_NAME = resolveProxyProfileName();
var PROFILE_PATHS = buildProxyProfilePaths(HOME, PROFILE_NAME);
function tildify(cwd) {
  return HOME ? cwd.replace(HOME, "~") : cwd;
}
var CONFIG_PATH = PROFILE_PATHS.configPath;
var RUN_DIR = PROFILE_PATHS.runDir;
var SOCK_PATH = PROFILE_PATHS.sockPath;
var PID_PATH = PROFILE_PATHS.pidPath;
var STOPPED_PATH = PROFILE_PATHS.stoppedPath;
var DESIRED_RELAY_PATH = PROFILE_PATHS.desiredRelayPath;
var STATE_DIR = PROFILE_PATHS.stateDir;
var SESSIONS_PATH = PROFILE_PATHS.sessionsPath;
var HISTORY_METADATA_PATH = PROFILE_PATHS.historyMetadataPath;
var HOOK_REGISTRY_PATH = PROFILE_PATHS.hookRegistryPath;
var DATA_DIR = PROFILE_PATHS.dataDir;
var PROXY_ID_PATH = PROFILE_PATHS.proxyIdPath;
var RELAY_DATA_DIR = PROFILE_PATHS.relayDataDir;
var FONT_DIR = PROFILE_PATHS.fontDir;
var LOG_DIR = PROFILE_PATHS.logDir;
var SERVICE_LOG_PATH = PROFILE_PATHS.serviceLogPath;
function sessionDir(sessionId) {
  return `${DATA_DIR}/${sessionId}`;
}
function sessionPaths(sessionId) {
  const dir = sessionDir(sessionId);
  return {
    dir,
    workerSock: `${dir}/worker.sock`
  };
}
function isInitialized() {
  return existsSync(CONFIG_PATH);
}
var DEFAULT_CONFIG = `{
  "defaultProfile": "default",
  "profiles": {
    "default": {
      "relay": "cloud"
    },
    "local": {
      "relay": "local"
    }
  },
  "relays": {
    "cloud": {
      "url": "wss://dev-anywhere.example.com",
      "proxyToken": ""
    },
    "local": {
      "url": "ws://localhost:3100"
    }
  }
}
`;
function copyFontFamilyIfMissing(targetFontsDir, source) {
  const family = source.family ?? DEFAULT_FONT_FAMILY;
  const sourceFamilyDir = `${source.dir}/${family}`;
  const targetFamilyDir = `${targetFontsDir}/${family}`;
  if (existsSync(targetFamilyDir) || !existsSync(sourceFamilyDir)) return false;
  mkdirSync(targetFontsDir, { recursive: true });
  cpSync(sourceFamilyDir, targetFamilyDir, { recursive: true });
  return true;
}
function installFontAssetsFromSources(targetFontsDir, sources) {
  for (const source of sources) {
    if (copyFontFamilyIfMissing(targetFontsDir, source)) return true;
  }
  return false;
}
function installFontAssets() {
  installFontAssetsFromSources(FONT_DIR, [
    { dir: SOURCE_FONT_ASSETS_DIR, family: DEFAULT_FONT_FAMILY },
    { dir: DIST_FONT_ASSETS_DIR, family: DEFAULT_FONT_FAMILY }
  ]);
}
function initWorkspace() {
  ensureProfileWorkspace();
  mkdirSync(RELAY_DATA_DIR, { recursive: true });
  installFontAssets();
  if (!existsSync(CONFIG_PATH)) {
    writeFileSync(CONFIG_PATH, DEFAULT_CONFIG);
  }
}
function ensureProfileWorkspace() {
  mkdirSync(RUN_DIR, { recursive: true });
  mkdirSync(STATE_DIR, { recursive: true });
  mkdirSync(DATA_DIR, { recursive: true });
  mkdirSync(LOG_DIR, { recursive: true });
}

export {
  DEFAULT_PROXY_PROFILE,
  defaultHookPortForProfile,
  PROFILE_NAME,
  tildify,
  CONFIG_PATH,
  SOCK_PATH,
  PID_PATH,
  STOPPED_PATH,
  DESIRED_RELAY_PATH,
  SESSIONS_PATH,
  HISTORY_METADATA_PATH,
  HOOK_REGISTRY_PATH,
  DATA_DIR,
  PROXY_ID_PATH,
  LOG_DIR,
  SERVICE_LOG_PATH,
  sessionPaths,
  isInitialized,
  initWorkspace,
  ensureProfileWorkspace
};
//# sourceMappingURL=chunk-7RSBFN2G.js.map