import { i as inquirer } from './index6.mjs';
import { execSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import 'node:readline';
import '../shared/ccjk.BAGoDD49.mjs';
import 'stream';
import 'node:tty';
import 'node:process';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'node:util';
import 'tty';
import 'fs';
import 'child_process';
import 'node:crypto';
import 'buffer';
import 'string_decoder';

function getCurrentVersion() {
  try {
    const packagePath = join(__dirname, "../../package.json");
    const pkg = JSON.parse(readFileSync(packagePath, "utf-8"));
    return pkg.version;
  } catch {
    return "0.0.0";
  }
}
async function getLatestVersion() {
  try {
    const result = execSync("npm view @cometx/ccjk version", {
      encoding: "utf-8",
      timeout: 3e3,
      stdio: ["pipe", "pipe", "ignore"]
      // 忽略 stderr
    });
    return result.trim();
  } catch {
    return getCurrentVersion();
  }
}
function compareVersions(v1, v2) {
  const parts1 = v1.split(".").map(Number);
  const parts2 = v2.split(".").map(Number);
  for (let i = 0; i < 3; i++) {
    const p1 = parts1[i] || 0;
    const p2 = parts2[i] || 0;
    if (p1 > p2)
      return 1;
    if (p1 < p2)
      return -1;
  }
  return 0;
}
function detectUpdateType(current, latest) {
  const c = current.split(".").map(Number);
  const l = latest.split(".").map(Number);
  if (l[0] > c[0])
    return "major";
  if (l[1] > c[1])
    return "minor";
  if (l[2] > c[2])
    return "patch";
  return null;
}
async function checkForUpdates() {
  const current = getCurrentVersion();
  const latest = await getLatestVersion();
  const hasUpdate = compareVersions(latest, current) > 0;
  const updateType = hasUpdate ? detectUpdateType(current, latest) : null;
  return {
    current,
    latest,
    hasUpdate,
    updateType
  };
}
join(homedir(), ".claude", ".upgrade-state.json");
async function performUpgrade() {
  try {
    console.log("\u{1F680} Upgrading CCJK...");
    let packageManager = "npm";
    try {
      execSync("pnpm --version", { stdio: "ignore" });
      packageManager = "pnpm";
    } catch {
      try {
        execSync("yarn --version", { stdio: "ignore" });
        packageManager = "yarn";
      } catch {
      }
    }
    const command = packageManager === "yarn" ? "yarn global add @cometx/ccjk@latest" : `${packageManager} add -g @cometx/ccjk@latest`;
    console.log(`   Running: ${command}`);
    execSync(command, { stdio: "inherit" });
    console.log("\n\u2705 Upgrade completed!");
    return true;
  } catch (error) {
    console.error("\u274C Upgrade failed:", error);
    return false;
  }
}

async function upgrade() {
  console.log("\u{1F50D} Checking for updates...\n");
  const versionInfo = await checkForUpdates();
  if (!versionInfo.hasUpdate) {
    console.log("\u2705 You are already on the latest version!");
    console.log(`   Current version: v${versionInfo.current}
`);
    return;
  }
  console.log("\u{1F4E6} Update available:");
  console.log(`   Current: v${versionInfo.current}`);
  console.log(`   Latest:  v${versionInfo.latest} (${versionInfo.updateType} update)
`);
  const { confirm } = await inquirer.prompt([
    {
      type: "confirm",
      name: "confirm",
      message: "Do you want to upgrade now?",
      default: true
    }
  ]);
  if (!confirm) {
    console.log("\n\u23ED\uFE0F  Upgrade cancelled.\n");
    return;
  }
  await performUpgrade();
}

export { upgrade };
