import { existsSync, readFileSync } from 'node:fs';
import a from './index5.mjs';
import { ensureI18nInitialized, i18n } from './index2.mjs';
import { n as normalizeClaudeFamilySettings, r as resolveClaudeFamilySettingsTarget } from '../shared/ccjk.DDL-4C-k.mjs';
import { writeJsonConfig } from './json-config.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:process';
import 'node:url';
import '../shared/ccjk.bQ7Dh1g4.mjs';
import './constants.mjs';
import 'node:os';
import './ccjk-config.mjs';
import '../shared/ccjk.BBtCGd_g.mjs';
import './index3.mjs';
import './fs-operations.mjs';
import 'node:crypto';
import 'node:fs/promises';
import '../shared/ccjk.RyizuzOI.mjs';

const ENV_KEY = "CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS";
function t(key, opts) {
  return i18n.t(`agent-teams:${key}`, opts);
}
function readSettings() {
  const target = resolveClaudeFamilySettingsTarget();
  if (!existsSync(target.settingsFile))
    return {};
  try {
    return JSON.parse(readFileSync(target.settingsFile, "utf-8"));
  } catch {
    return {};
  }
}
function isAgentTeamsEnabled() {
  const settings = readSettings();
  return settings?.env?.[ENV_KEY] === "1";
}
function setAgentTeams(enabled) {
  const settings = readSettings();
  if (!settings.env)
    settings.env = {};
  if (enabled) {
    settings.env[ENV_KEY] = "1";
  } else {
    delete settings.env[ENV_KEY];
  }
  normalizeClaudeFamilySettings(settings);
  writeJsonConfig(resolveClaudeFamilySettingsTarget().settingsFile, settings);
}
function getTeammateMode() {
  const settings = readSettings();
  return settings?.teammateMode || "auto";
}
function setTeammateMode(mode) {
  const settings = readSettings();
  settings.teammateMode = mode;
  normalizeClaudeFamilySettings(settings);
  writeJsonConfig(resolveClaudeFamilySettingsTarget().settingsFile, settings);
}
async function agentTeamsCommand(options) {
  ensureI18nInitialized();
  if (options.status) {
    printStatus();
    return;
  }
  if (options.on !== void 0 || options.off !== void 0) {
    const enable = options.on === true;
    setAgentTeams(enable);
    console.log(a.green(t(enable ? "enabled" : "disabled")));
    return;
  }
  if (options.mode) {
    const valid = ["auto", "in-process", "tmux"];
    if (!valid.includes(options.mode)) {
      console.log(a.red(t("invalidMode")));
      return;
    }
    setTeammateMode(options.mode);
    console.log(a.green(t("modeSet", { mode: options.mode })));
    return;
  }
  const current = isAgentTeamsEnabled();
  const { default: inquirer } = await import('./index6.mjs').then(function (n) { return n.c; });
  const { action } = await inquirer.prompt([{
    type: "list",
    name: "action",
    message: t("settings"),
    choices: [
      {
        name: current ? t("toggleDisable") : t("toggleEnable"),
        value: "toggle"
      },
      {
        name: t("setMode"),
        value: "mode"
      },
      {
        name: t("viewStatus"),
        value: "status"
      },
      {
        name: t("back"),
        value: "back"
      }
    ]
  }]);
  if (action === "toggle") {
    setAgentTeams(!current);
    console.log(a.green(t(!current ? "enabled" : "disabled")));
  } else if (action === "mode") {
    const currentMode = getTeammateMode();
    const { mode } = await inquirer.prompt([{
      type: "list",
      name: "mode",
      message: t("selectMode"),
      choices: [
        { name: `auto ${currentMode === "auto" ? "(current)" : ""}`, value: "auto" },
        { name: `in-process ${currentMode === "in-process" ? "(current)" : ""}`, value: "in-process" },
        { name: `tmux ${currentMode === "tmux" ? "(current)" : ""}`, value: "tmux" }
      ]
    }]);
    setTeammateMode(mode);
    console.log(a.green(t("modeSet", { mode })));
  } else if (action === "status") {
    printStatus();
  }
}
function printStatus() {
  const enabled = isAgentTeamsEnabled();
  const mode = getTeammateMode();
  console.log();
  console.log(a.bold(t("statusTitle")));
  console.log(a.gray("\u2500".repeat(40)));
  console.log(`  ${t("statusLabel")}:  ${enabled ? a.green("\u2705 Enabled") : a.dim("\u2B1C Disabled")}`);
  console.log(`  ${t("modeLabel")}:    ${a.cyan(mode)}`);
  console.log();
  if (!enabled) {
    console.log(a.dim(`  ${t("enableHint")}`));
  } else {
    console.log(a.dim(`  ${t("usageHint")}`));
  }
  console.log();
}

export { agentTeamsCommand, getTeammateMode, isAgentTeamsEnabled, setAgentTeams, setTeammateMode };
