#!/usr/bin/env node
// @bun
import {
  __require
} from "./chunk-qp2qdcda.js";

// src/entrypoints/cli.tsx
var feature = (_name) => false;
if (typeof globalThis.MACRO === "undefined") {
  globalThis.MACRO = {
    VERSION: "2.1.888",
    BUILD_TIME: new Date().toISOString(),
    FEEDBACK_CHANNEL: "",
    ISSUES_EXPLAINER: "",
    NATIVE_PACKAGE_URL: "",
    PACKAGE_URL: "",
    VERSION_CHANGELOG: ""
  };
}
globalThis.BUILD_TARGET = "external";
globalThis.BUILD_ENV = "production";
globalThis.INTERFACE_TYPE = "stdio";
process.env.COREPACK_ENABLE_AUTO_PIN = "0";
if (process.env.CLAUDE_CODE_REMOTE === "true") {
  const existing = process.env.NODE_OPTIONS || "";
  process.env.NODE_OPTIONS = existing ? `${existing} --max-old-space-size=8192` : "--max-old-space-size=8192";
}
if (feature("ABLATION_BASELINE") && process.env.CLAUDE_CODE_ABLATION_BASELINE) {
  for (const k of [
    "CLAUDE_CODE_SIMPLE",
    "CLAUDE_CODE_DISABLE_THINKING",
    "DISABLE_INTERLEAVED_THINKING",
    "DISABLE_COMPACT",
    "DISABLE_AUTO_COMPACT",
    "CLAUDE_CODE_DISABLE_AUTO_MEMORY",
    "CLAUDE_CODE_DISABLE_BACKGROUND_TASKS"
  ]) {
    process.env[k] ??= "1";
  }
}
async function main() {
  const args = process.argv.slice(2);
  if (args.length === 1 && (args[0] === "--version" || args[0] === "-v" || args[0] === "-V")) {
    console.log(`${MACRO.VERSION} (\u5609\u9675\u6C5F-code)`);
    return;
  }
  {
    const { loadConfig, hasConfig, runSetupWizard, applyConfig } = await import("./chunk-n512q71j.js");
    if (args[0] === "--setup" || args[0] === "setup") {
      const config = await runSetupWizard();
      applyConfig(config);
      args.shift();
      if (args.length === 0) {}
    } else if (!hasConfig() && !process.env.ANTHROPIC_API_KEY && args[0] !== "-p" && args[0] !== "--print") {
      const config = await runSetupWizard();
      applyConfig(config);
    } else if (hasConfig()) {
      const config = loadConfig();
      if (config)
        applyConfig(config);
    }
  }
  const { profileCheckpoint } = await import("./chunk-bg3mt9bm.js");
  profileCheckpoint("cli_entry");
  if (feature("DUMP_SYSTEM_PROMPT") && args[0] === "--dump-system-prompt") {
    profileCheckpoint("cli_dump_system_prompt_path");
    const { enableConfigs } = await import("./chunk-7kq71tzc.js");
    enableConfigs();
    const { getMainLoopModel } = await import("./chunk-qecj4thj.js");
    const modelIdx = args.indexOf("--model");
    const model = modelIdx !== -1 && args[modelIdx + 1] || getMainLoopModel();
    const { getSystemPrompt } = await import("./chunk-8b4dxcj3.js");
    const prompt = await getSystemPrompt([], model);
    console.log(prompt.join(`
`));
    return;
  }
  if (process.argv[2] === "--claude-in-chrome-mcp") {
    profileCheckpoint("cli_claude_in_chrome_mcp_path");
    const { runClaudeInChromeMcpServer } = await import("./chunk-thcb5zjg.js");
    await runClaudeInChromeMcpServer();
    return;
  } else if (process.argv[2] === "--chrome-native-host") {
    profileCheckpoint("cli_chrome_native_host_path");
    const { runChromeNativeHost } = await import("./chunk-77g09znh.js");
    await runChromeNativeHost();
    return;
  } else if (feature("CHICAGO_MCP") && process.argv[2] === "--computer-use-mcp") {
    profileCheckpoint("cli_computer_use_mcp_path");
    const { runComputerUseMcpServer } = await import("./chunk-3mvtynx1.js");
    await runComputerUseMcpServer();
    return;
  }
  if (feature("DAEMON") && args[0] === "--daemon-worker") {
    const { runDaemonWorker } = await import("./chunk-s274nv8k.js");
    await runDaemonWorker(args[1]);
    return;
  }
  if (feature("BRIDGE_MODE") && (args[0] === "remote-control" || args[0] === "rc" || args[0] === "remote" || args[0] === "sync" || args[0] === "bridge")) {
    profileCheckpoint("cli_bridge_path");
    const { enableConfigs } = await import("./chunk-7kq71tzc.js");
    enableConfigs();
    const { getBridgeDisabledReason, checkBridgeMinVersion } = await import("./chunk-1bdmq3f5.js");
    const { BRIDGE_LOGIN_ERROR } = await import("./chunk-bsbmmfyt.js");
    const { bridgeMain } = await import("./chunk-89nrd694.js");
    const { exitWithError } = await import("./chunk-y1784krc.js");
    const { getClaudeAIOAuthTokens } = await import("./chunk-2vz5epmk.js");
    if (!getClaudeAIOAuthTokens()?.accessToken) {
      exitWithError(BRIDGE_LOGIN_ERROR);
    }
    const disabledReason = await getBridgeDisabledReason();
    if (disabledReason) {
      exitWithError(`Error: ${disabledReason}`);
    }
    const versionError = checkBridgeMinVersion();
    if (versionError) {
      exitWithError(versionError);
    }
    const { waitForPolicyLimitsToLoad, isPolicyAllowed } = await import("./chunk-m0egh5n2.js");
    await waitForPolicyLimitsToLoad();
    if (!isPolicyAllowed("allow_remote_control")) {
      exitWithError("Error: Remote Control is disabled by your organization's policy.");
    }
    await bridgeMain(args.slice(1));
    return;
  }
  if (feature("DAEMON") && args[0] === "daemon") {
    profileCheckpoint("cli_daemon_path");
    const { enableConfigs } = await import("./chunk-7kq71tzc.js");
    enableConfigs();
    const { initSinks } = await import("./chunk-68tfdman.js");
    initSinks();
    const { daemonMain } = await import("./chunk-v3gm2day.js");
    await daemonMain(args.slice(1));
    return;
  }
  if (feature("BG_SESSIONS") && (args[0] === "ps" || args[0] === "logs" || args[0] === "attach" || args[0] === "kill" || args.includes("--bg") || args.includes("--background"))) {
    profileCheckpoint("cli_bg_path");
    const { enableConfigs } = await import("./chunk-7kq71tzc.js");
    enableConfigs();
    const bg = await import("./chunk-1c8z1b5v.js");
    switch (args[0]) {
      case "ps":
        await bg.psHandler(args.slice(1));
        break;
      case "logs":
        await bg.logsHandler(args[1]);
        break;
      case "attach":
        await bg.attachHandler(args[1]);
        break;
      case "kill":
        await bg.killHandler(args[1]);
        break;
      default:
        await bg.handleBgFlag(args);
    }
    return;
  }
  if (feature("TEMPLATES") && (args[0] === "new" || args[0] === "list" || args[0] === "reply")) {
    profileCheckpoint("cli_templates_path");
    const { templatesMain } = await import("./chunk-xdw5dv00.js");
    await templatesMain(args);
    process.exit(0);
  }
  if (feature("BYOC_ENVIRONMENT_RUNNER") && args[0] === "environment-runner") {
    profileCheckpoint("cli_environment_runner_path");
    const { environmentRunnerMain } = await import("./chunk-hs8ph4p8.js");
    await environmentRunnerMain(args.slice(1));
    return;
  }
  if (feature("SELF_HOSTED_RUNNER") && args[0] === "self-hosted-runner") {
    profileCheckpoint("cli_self_hosted_runner_path");
    const { selfHostedRunnerMain } = await import("./chunk-q5by3da6.js");
    await selfHostedRunnerMain(args.slice(1));
    return;
  }
  const hasTmuxFlag = args.includes("--tmux") || args.includes("--tmux=classic");
  if (hasTmuxFlag && (args.includes("-w") || args.includes("--worktree") || args.some((a) => a.startsWith("--worktree=")))) {
    profileCheckpoint("cli_tmux_worktree_fast_path");
    const { enableConfigs } = await import("./chunk-7kq71tzc.js");
    enableConfigs();
    const { isWorktreeModeEnabled } = await import("./chunk-pwwa7s62.js");
    if (isWorktreeModeEnabled()) {
      const { execIntoTmuxWorktree } = await import("./chunk-x9vd3zjx.js");
      const result = await execIntoTmuxWorktree(args);
      if (result.handled) {
        return;
      }
      if (result.error) {
        const { exitWithError } = await import("./chunk-y1784krc.js");
        exitWithError(result.error);
      }
    }
  }
  if (args.length === 1 && (args[0] === "--update" || args[0] === "--upgrade")) {
    process.argv = [process.argv[0], process.argv[1], "update"];
  }
  if (args.includes("--bare")) {
    process.env.CLAUDE_CODE_SIMPLE = "1";
  }
  const { startCapturingEarlyInput } = await import("./chunk-ywxd4qw4.js");
  startCapturingEarlyInput();
  profileCheckpoint("cli_before_main_import");
  const { main: cliMain } = await import("./chunk-x01atcnf.js");
  profileCheckpoint("cli_after_main_import");
  await cliMain();
  profileCheckpoint("cli_after_main_complete");
}
main();
