'use strict';

var child_process = require('child_process');
require('fs');
require('path');
var util = require('util');

// src/platform/ai-services/internal/cli-management.ts
function stringifyUnknown(value) {
  if (value === void 0 || value === null) {
    return "";
  }
  if (typeof value === "string") {
    return value;
  }
  if (Buffer.isBuffer(value)) {
    return value.toString("utf8");
  }
  if (value instanceof Error) {
    return value.stack || value.message;
  }
  return util.inspect(value, { depth: 6, breakLength: 120 });
}
function getErrorSummary(error) {
  if (error instanceof Error) {
    return `${error.name}: ${error.message}`;
  }
  return stringifyUnknown(error) || "Unknown error";
}

// src/platform/types/app.ts
var AI_SERVICE = {
  CLAUDE: "claude",
  CODEX: "codex",
  GEMINI: "gemini"
};

// src/platform/ai-services/internal/semver.ts
var SEMVER_PATTERN = /(\d+\.\d+\.\d+)/;
function extractSemver(value) {
  const match = value.match(SEMVER_PATTERN);
  return match?.[1] ?? null;
}

// src/platform/ai-services/internal/cli-management.ts
var SERVICE_BINARY_NAMES = {
  [AI_SERVICE.CODEX]: "codex",
  [AI_SERVICE.GEMINI]: "gemini",
  [AI_SERVICE.CLAUDE]: "claude"
};
var SERVICE_INSTALL_PACKAGES = {
  [AI_SERVICE.CODEX]: "@openai/codex",
  [AI_SERVICE.GEMINI]: "@google/gemini-cli",
  [AI_SERVICE.CLAUDE]: "@anthropic-ai/claude-code"
};
function getBinaryName(service) {
  return SERVICE_BINARY_NAMES[service];
}
function getInstallPackageName(service) {
  return SERVICE_INSTALL_PACKAGES[service];
}
function isCliAvailable(service, trace) {
  const binaryName = getBinaryName(service);
  trace("cli-check:start", `${service} -> ${binaryName} --version`);
  const result = child_process.spawnSync(binaryName, ["--version"], {
    encoding: "utf8",
    maxBuffer: 1024 * 1024 * 2,
    timeout: 1e4
  });
  if (result.error || result.status !== 0) {
    trace("cli-check:missing", getErrorSummary(result.error || result.stderr || result.status));
    return false;
  }
  trace("cli-check:ok", `${service}`);
  return true;
}
function installCli(service, trace) {
  const packageName = getInstallPackageName(service);
  trace("cli-install:start", `${service} -> ${packageName}`);
  const result = child_process.spawnSync("npm", ["install", "-g", packageName], {
    encoding: "utf8",
    maxBuffer: 1024 * 1024 * 20,
    stdio: "inherit"
  });
  if (result.error || result.status !== 0) {
    trace("cli-install:failed", getErrorSummary(result.error || result.stderr || result.status));
    throw new Error(`${service} CLI \uC124\uCE58\uC5D0 \uC2E4\uD328\uD588\uC2B5\uB2C8\uB2E4. package=${packageName}`);
  }
  trace("cli-install:done", `${service}`);
}
function getInstalledVersion(service, trace) {
  const binaryName = getBinaryName(service);
  trace("cli-version:current:start", binaryName);
  const result = child_process.spawnSync(binaryName, ["--version"], {
    encoding: "utf8",
    maxBuffer: 1024 * 1024 * 2,
    timeout: 1e4
  });
  if (result.error || result.status !== 0) {
    trace("cli-version:current:failed", getErrorSummary(result.error || result.stderr || result.status));
    return null;
  }
  const output = (result.stdout || "").trim();
  const version = extractSemver(output);
  if (!version) {
    trace("cli-version:current:parse-failed", output);
    return null;
  }
  trace("cli-version:current:ok", version);
  return version;
}
function getLatestVersion(service, trace) {
  const packageName = getInstallPackageName(service);
  trace("cli-version:latest:start", packageName);
  const result = child_process.spawnSync("npm", ["view", packageName, "version"], {
    encoding: "utf8",
    maxBuffer: 1024 * 1024,
    timeout: 15e3
  });
  if (result.error || result.status !== 0) {
    trace("cli-version:latest:failed", getErrorSummary(result.error || result.stderr || result.status));
    return null;
  }
  const version = (result.stdout || "").trim();
  const normalizedVersion = extractSemver(version);
  if (!normalizedVersion) {
    trace("cli-version:latest:parse-failed", version);
    return null;
  }
  trace("cli-version:latest:ok", normalizedVersion);
  return normalizedVersion;
}
function upgradeServiceCli(service, trace, minimumVersion) {
  const packageName = getInstallPackageName(service);
  const packageSpec = minimumVersion ? `${packageName}@${minimumVersion}` : `${packageName}@latest`;
  trace("cli-upgrade:start", `${service} -> ${packageSpec}`);
  const result = child_process.spawnSync("npm", ["install", "-g", packageSpec], {
    encoding: "utf8",
    maxBuffer: 1024 * 1024 * 20,
    stdio: "inherit"
  });
  if (result.error || result.status !== 0) {
    trace("cli-upgrade:failed", getErrorSummary(result.error || result.stderr || result.status));
    return false;
  }
  trace("cli-upgrade:done", service);
  return true;
}
function ensureServiceCli(service, parsedArgs, trace) {
  if (isCliAvailable(service, trace)) {
    return true;
  }
  if (parsedArgs.testMode) {
    trace("cli-install:skip:test-mode", service);
    return false;
  }
  installCli(service, trace);
  return isCliAvailable(service, trace);
}

exports.ensureServiceCli = ensureServiceCli;
exports.getBinaryName = getBinaryName;
exports.getInstallPackageName = getInstallPackageName;
exports.getInstalledVersion = getInstalledVersion;
exports.getLatestVersion = getLatestVersion;
exports.installCli = installCli;
exports.isCliAvailable = isCliAvailable;
exports.upgradeServiceCli = upgradeServiceCli;
