import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import a from './index5.mjs';
import { ensureI18nInitialized, i18n } from './index2.mjs';
import { w as wrapCommandWithSudo } from './platform.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:fs';
import 'node:process';
import 'node:url';
import '../shared/ccjk.bQ7Dh1g4.mjs';
import 'node:os';
import './main.mjs';
import 'module';
import 'node:path';
import 'node:stream';
import 'node:readline';

const execAsync = promisify(exec);
async function checkAgentBrowserInstalled() {
  try {
    await execAsync("agent-browser --version");
    return true;
  } catch {
    return false;
  }
}
async function getAgentBrowserStatus() {
  let isInstalled = false;
  let version = null;
  let hasBrowser = false;
  try {
    const { stdout } = await execAsync("agent-browser --version");
    isInstalled = true;
    const match = stdout.match(/(\d+\.\d+\.\d+)/);
    version = match ? match[1] : null;
  } catch {
    try {
      await execAsync("which agent-browser");
      isInstalled = true;
    } catch {
      isInstalled = false;
    }
  }
  hasBrowser = isInstalled;
  return {
    isInstalled,
    version,
    hasBrowser
  };
}
async function installAgentBrowser() {
  ensureI18nInitialized();
  const isInstalled = await checkAgentBrowserInstalled();
  if (isInstalled) {
    console.log(a.green(`\u2714 ${i18n.t("agentBrowser:alreadyInstalled", { version: "installed" })}`));
    return true;
  }
  console.log(a.green(`\u{1F4E6} ${i18n.t("agentBrowser:installing")}`));
  try {
    const installArgs = ["install", "-g", "agent-browser"];
    const { command, args, usedSudo } = wrapCommandWithSudo("npm", installArgs);
    if (usedSudo) {
      console.log(a.yellow(`\u2139 ${i18n.t("installation:usingSudo")}`));
    }
    await execAsync([command, ...args].join(" "), { timeout: 12e4 });
    console.log(a.green(`\u2714 ${i18n.t("agentBrowser:installSuccess")}`));
    console.log(a.gray(`\u2139 ${i18n.t("agentBrowser:browserAutoInstall")}`));
    return true;
  } catch (error) {
    console.error(a.red(`\u2716 ${i18n.t("agentBrowser:installFailed")}`));
    if (error.message) {
      console.error(a.gray(error.message));
    }
    return false;
  }
}
async function uninstallAgentBrowser() {
  ensureI18nInitialized();
  const status = await getAgentBrowserStatus();
  if (!status.isInstalled) {
    console.log(a.yellow(`\u2139 ${i18n.t("agentBrowser:notInstalled")}`));
    return true;
  }
  console.log(a.green(`\u{1F5D1}\uFE0F  ${i18n.t("agentBrowser:uninstalling")}`));
  try {
    const uninstallArgs = ["uninstall", "-g", "agent-browser"];
    const { command, args, usedSudo } = wrapCommandWithSudo("npm", uninstallArgs);
    if (usedSudo) {
      console.log(a.yellow(`\u2139 ${i18n.t("installation:usingSudo")}`));
    }
    await execAsync([command, ...args].join(" "), { timeout: 6e4 });
    console.log(a.green(`\u2714 ${i18n.t("agentBrowser:uninstallSuccess")}`));
    return true;
  } catch (error) {
    console.error(a.red(`\u2716 ${i18n.t("agentBrowser:uninstallFailed")}`));
    if (error.message) {
      console.error(a.gray(error.message));
    }
    return false;
  }
}

export { checkAgentBrowserInstalled, getAgentBrowserStatus, installAgentBrowser, uninstallAgentBrowser };
