"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var InkCLIRefactored_exports = {};
__export(InkCLIRefactored_exports, {
  InkCLIRefactored: () => InkCLIRefactored,
  startInkCliRefactored: () => startInkCliRefactored
});
module.exports = __toCommonJS(InkCLIRefactored_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_host = require("./host/index.js");
var import_bracketedPaste = require("./utils/bracketedPaste.js");
var import_contexts = require("./contexts/index.js");
var import_ErrorBoundary = require("./components/ErrorBoundary.js");
var import_modes = require("./components/modes/index.js");
var import_useStreaming = require("./hooks/useStreaming.js");
var import_messageFormatting = require("./utils/messageFormatting.js");
const USE_ALTERNATE_BUFFER = false;
function formatCost(c) {
  if (c < 0.01) return `$${c.toFixed(4)}`;
  if (c < 1) return `$${c.toFixed(3)}`;
  return `$${c.toFixed(2)}`;
}
function InkCLIInner({
  agent,
  initialSessionId,
  initialPrompt,
  startupInfo,
  soundService,
  configFilePath,
  initialBypassPermissions = false
}) {
  const [, setSelectionHintShown] = (0, import_react.useState)(false);
  const { streaming } = (0, import_useStreaming.useStreaming)();
  const handleSelectionAttempt = (0, import_react.useCallback)(() => {
    setSelectionHintShown(true);
  }, []);
  if (USE_ALTERNATE_BUFFER) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_contexts.SoundProvider, { soundService, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_contexts.ScrollProvider, { onSelectionAttempt: handleSelectionAttempt, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_modes.AlternateBufferCLI,
      {
        agent,
        initialSessionId,
        initialPrompt,
        startupInfo,
        onSelectionAttempt: handleSelectionAttempt,
        useStreaming: streaming,
        configFilePath,
        initialBypassPermissions
      }
    ) }) });
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_contexts.SoundProvider, { soundService, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_modes.StaticCLI,
    {
      agent,
      initialSessionId,
      initialPrompt,
      startupInfo,
      useStreaming: streaming,
      configFilePath,
      initialBypassPermissions
    }
  ) });
}
function InkCLIRefactored({
  agent,
  initialSessionId,
  initialPrompt,
  startupInfo,
  soundService,
  configFilePath,
  initialBypassPermissions = false
}) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ErrorBoundary.ErrorBoundary, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_contexts.KeypressProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_contexts.MouseProvider, { mouseEventsEnabled: USE_ALTERNATE_BUFFER, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    InkCLIInner,
    {
      agent,
      initialSessionId,
      initialPrompt,
      startupInfo,
      soundService,
      configFilePath,
      initialBypassPermissions
    }
  ) }) }) });
}
async function startInkCliRefactored(agent, initialSessionId, options = {}) {
  (0, import_host.registerGracefulShutdown)(() => agent, { inkMode: true });
  (0, import_bracketedPaste.enableBracketedPaste)();
  const baseStartupInfo = await (0, import_messageFormatting.getStartupInfo)(agent, initialSessionId);
  const startupInfo = {
    ...baseStartupInfo,
    updateInfo: options.updateInfo,
    needsAgentSync: options.needsAgentSync
  };
  let globalPreferencesExistFn = () => false;
  let loadGlobalPreferencesFn = null;
  let agentPreferencesExistFn = () => false;
  let loadAgentPreferencesFn = null;
  try {
    const agentManagement = await import("@dexto/agent-management");
    globalPreferencesExistFn = agentManagement.globalPreferencesExist;
    loadGlobalPreferencesFn = agentManagement.loadGlobalPreferences;
    agentPreferencesExistFn = agentManagement.agentPreferencesExist;
    loadAgentPreferencesFn = agentManagement.loadAgentPreferences;
  } catch (error) {
    agent.logger.debug(
      `Preferences module could not be loaded: ${error instanceof Error ? error.message : String(error)}`
    );
  }
  let soundService = null;
  let soundConfig = {
    enabled: true,
    onStartup: false,
    startupSoundFile: void 0,
    onApprovalRequired: true,
    approvalSoundFile: void 0,
    onTaskComplete: true,
    completeSoundFile: void 0
  };
  try {
    const { SoundNotificationService: SoundNotificationServiceImpl } = await import("./utils/soundNotification.js");
    if (globalPreferencesExistFn() && loadGlobalPreferencesFn) {
      try {
        const preferences = await loadGlobalPreferencesFn();
        soundConfig = {
          enabled: preferences.sounds?.enabled ?? soundConfig.enabled,
          onStartup: preferences.sounds?.onStartup ?? soundConfig.onStartup,
          startupSoundFile: preferences.sounds?.startupSoundFile ?? soundConfig.startupSoundFile,
          onApprovalRequired: preferences.sounds?.onApprovalRequired ?? soundConfig.onApprovalRequired,
          approvalSoundFile: preferences.sounds?.approvalSoundFile ?? soundConfig.approvalSoundFile,
          onTaskComplete: preferences.sounds?.onTaskComplete ?? soundConfig.onTaskComplete,
          completeSoundFile: preferences.sounds?.completeSoundFile ?? soundConfig.completeSoundFile
        };
      } catch (error) {
        agent.logger.debug(
          `Sound preferences could not be loaded: ${error instanceof Error ? error.message : String(error)}`
        );
      }
    }
    soundService = new SoundNotificationServiceImpl(soundConfig);
    soundService.playStartupSound();
  } catch (error) {
    agent.logger.debug(
      `Sound initialization failed: ${error instanceof Error ? error.message : String(error)}`
    );
    soundService = null;
  }
  if (agentPreferencesExistFn(agent.config.agentId) && loadAgentPreferencesFn) {
    try {
      const preferences = await loadAgentPreferencesFn(agent.config.agentId);
      agent.setGlobalDisabledTools(preferences.tools?.disabled ?? []);
    } catch (error) {
      agent.logger.debug(
        `Agent tool preferences could not be loaded: ${error instanceof Error ? error.message : String(error)}`
      );
    }
  }
  const { registerExitHandler } = await import("./interactive-commands/exit-handler.js");
  const inkApp = (0, import_ink.render)(
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      InkCLIRefactored,
      {
        agent,
        initialSessionId,
        initialPrompt: options.initialPrompt,
        startupInfo,
        soundService,
        configFilePath: options.configFilePath ?? null,
        initialBypassPermissions: options.bypassPermissions ?? false
      }
    ),
    {
      exitOnCtrlC: false,
      alternateBuffer: USE_ALTERNATE_BUFFER,
      // Incremental rendering works better with VirtualizedList
      // Static pattern doesn't need it (and may work better without)
      incrementalRendering: USE_ALTERNATE_BUFFER
    }
  );
  registerExitHandler(() => inkApp.unmount());
  await inkApp.waitUntilExit();
  (0, import_bracketedPaste.disableBracketedPaste)();
  const chalk = (await import("chalk")).default;
  const { getExitStats, clearExitStats } = await import("./interactive-commands/exit-stats.js");
  const exitStats = getExitStats();
  if (exitStats) {
    process.stdout.write("\n".repeat(1));
    process.stdout.write(chalk.bold.cyan("\u{1F4CA} Session Summary") + "\n");
    process.stdout.write(chalk.dim("\u2500".repeat(50)) + "\n");
    if (exitStats.sessionId) {
      process.stdout.write(chalk.gray(`  Session ID:  ${exitStats.sessionId}`) + "\n");
    }
    if (exitStats.duration) {
      process.stdout.write(chalk.gray(`  Duration:    ${exitStats.duration}`) + "\n");
    }
    if (exitStats.messageCount.total > 0) {
      process.stdout.write(
        chalk.gray(
          `  Messages:    ${exitStats.messageCount.total} total (${exitStats.messageCount.user} user, ${exitStats.messageCount.assistant} assistant)`
        ) + "\n"
      );
    }
    if (exitStats.modelStats && exitStats.modelStats.length > 1) {
      process.stdout.write(chalk.gray("\n  Models Used:") + "\n");
      for (const modelStat of exitStats.modelStats) {
        const modelLabel = `${modelStat.model} (${modelStat.messageCount} msgs)`;
        process.stdout.write(chalk.gray(`    \u2022 ${modelLabel}`) + "\n");
        if (exitStats.tokenUsage) {
          const tokens = modelStat.tokenUsage;
          process.stdout.write(
            chalk.gray(
              `      Input tokens:       ${tokens.inputTokens.toLocaleString()}`
            ) + "\n"
          );
          process.stdout.write(
            chalk.gray(
              `      Output tokens:      ${tokens.outputTokens.toLocaleString()}`
            ) + "\n"
          );
          process.stdout.write(
            chalk.gray(
              `      Reasoning tokens:   ${tokens.reasoningTokens.toLocaleString()}`
            ) + "\n"
          );
          process.stdout.write(
            chalk.gray(
              `      Cache read tokens:  ${tokens.cacheReadTokens.toLocaleString()}`
            ) + "\n"
          );
          process.stdout.write(
            chalk.gray(
              `      Cache write tokens: ${tokens.cacheWriteTokens.toLocaleString()}`
            ) + "\n"
          );
          process.stdout.write(
            chalk.gray(
              `      Total tokens:       ${tokens.totalTokens.toLocaleString()}`
            ) + "\n"
          );
          if (modelStat.estimatedCost !== void 0) {
            process.stdout.write(
              chalk.gray(
                `      Cost:               ${formatCost(modelStat.estimatedCost)}`
              ) + "\n"
            );
          }
        }
      }
    }
    if (exitStats.usageNote) {
      process.stdout.write(chalk.gray("\n  Usage:") + "\n");
      process.stdout.write(chalk.gray(`    ${exitStats.usageNote}`) + "\n");
    }
    if (exitStats.tokenUsage) {
      const {
        inputTokens,
        outputTokens,
        reasoningTokens,
        cacheReadTokens,
        cacheWriteTokens,
        totalTokens
      } = exitStats.tokenUsage;
      const totalInputWithCache = inputTokens + cacheReadTokens;
      const cacheSavingsPercent = totalInputWithCache > 0 ? (cacheReadTokens / totalInputWithCache * 100).toFixed(1) : "0.0";
      const tokenSectionLabel = exitStats.modelStats && exitStats.modelStats.length > 1 ? "\n  Total Token Usage:" : "\n  Token Usage:";
      process.stdout.write(chalk.gray(tokenSectionLabel) + "\n");
      process.stdout.write(
        chalk.gray(`    Input tokens:       ${inputTokens.toLocaleString()}`) + "\n"
      );
      process.stdout.write(
        chalk.gray(`    Output tokens:      ${outputTokens.toLocaleString()}`) + "\n"
      );
      process.stdout.write(
        chalk.gray(`    Reasoning tokens:   ${reasoningTokens.toLocaleString()}`) + "\n"
      );
      const cacheReadLabel = cacheReadTokens > 0 ? `${cacheReadTokens.toLocaleString()} (\u{1F4B0} ${cacheSavingsPercent}% savings)` : cacheReadTokens.toLocaleString();
      process.stdout.write(chalk.gray(`    Cache read tokens:  ${cacheReadLabel}`) + "\n");
      process.stdout.write(
        chalk.gray(`    Cache write tokens: ${cacheWriteTokens.toLocaleString()}`) + "\n"
      );
      process.stdout.write(
        chalk.gray(`    Total tokens:       ${totalTokens.toLocaleString()}`) + "\n"
      );
    }
    if (exitStats.estimatedCost !== void 0) {
      process.stdout.write(
        chalk.green(`
  Estimated Cost: ${formatCost(exitStats.estimatedCost)}`) + "\n"
      );
    }
    clearExitStats();
  }
  process.stdout.write(chalk.dim("\u2500".repeat(50)) + "\n");
  process.stdout.write("\n" + chalk.rgb(255, 165, 0)("Exiting Dexto CLI. Goodbye!") + "\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  InkCLIRefactored,
  startInkCliRefactored
});
