// Force strict mode and setup for ESM
"use strict";
import {
  getEffectiveSupportedModes
} from "./chunk-RMXPR6HS.js";
import {
  require_jsx_runtime
} from "./chunk-4FRUXVSE.js";
import {
  require_react
} from "./chunk-KYMBIKIW.js";
import {
  uiTelemetryService
} from "./chunk-4IUH45NF.js";
import {
  init_esbuild_shims
} from "./chunk-A4BMJM77.js";
import {
  __name,
  __toESM
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/ui/contexts/SessionContext.tsx
init_esbuild_shims();
var import_react = __toESM(require_react(), 1);
var import_jsx_runtime = __toESM(require_jsx_runtime(), 1);
var EMPTY_SKILL_METRICS = {
  totalCalls: 0,
  totalSuccess: 0,
  totalFail: 0,
  byName: {}
};
function areModelMetricsCoreEqual(a, b) {
  if (a.api.totalRequests !== b.api.totalRequests || a.api.totalErrors !== b.api.totalErrors || a.api.totalLatencyMs !== b.api.totalLatencyMs) {
    return false;
  }
  if (a.tokens.prompt !== b.tokens.prompt || a.tokens.candidates !== b.tokens.candidates || a.tokens.total !== b.tokens.total || a.tokens.cached !== b.tokens.cached || a.tokens.thoughts !== b.tokens.thoughts) {
    return false;
  }
  return true;
}
__name(areModelMetricsCoreEqual, "areModelMetricsCoreEqual");
function areModelMetricsEqual(a, b) {
  if (!areModelMetricsCoreEqual(a, b)) return false;
  const aKeys = Object.keys(a.bySource);
  const bKeys = Object.keys(b.bySource);
  if (aKeys.length !== bKeys.length) return false;
  for (const key of aKeys) {
    const aSource = a.bySource[key];
    const bSource = b.bySource[key];
    if (!bSource || !areModelMetricsCoreEqual(aSource, bSource)) {
      return false;
    }
  }
  return true;
}
__name(areModelMetricsEqual, "areModelMetricsEqual");
function areToolCallStatsEqual(a, b) {
  if (a.count !== b.count || a.success !== b.success || a.fail !== b.fail || a.durationMs !== b.durationMs) {
    return false;
  }
  if (a.decisions["accept" /* ACCEPT */] !== b.decisions["accept" /* ACCEPT */] || a.decisions["reject" /* REJECT */] !== b.decisions["reject" /* REJECT */] || a.decisions["modify" /* MODIFY */] !== b.decisions["modify" /* MODIFY */] || a.decisions["auto_accept" /* AUTO_ACCEPT */] !== b.decisions["auto_accept" /* AUTO_ACCEPT */]) {
    return false;
  }
  return true;
}
__name(areToolCallStatsEqual, "areToolCallStatsEqual");
function areSkillCallStatsEqual(a, b) {
  return a.count === b.count && a.success === b.success && a.fail === b.fail;
}
__name(areSkillCallStatsEqual, "areSkillCallStatsEqual");
function areSkillMetricsEqual(a, b) {
  if (a.totalCalls !== b.totalCalls || a.totalSuccess !== b.totalSuccess || a.totalFail !== b.totalFail) {
    return false;
  }
  const aKeys = Object.keys(a.byName);
  const bKeys = Object.keys(b.byName);
  if (aKeys.length !== bKeys.length) return false;
  for (const key of aKeys) {
    const skillA = a.byName[key];
    const skillB = b.byName[key];
    if (!skillB || !areSkillCallStatsEqual(skillA, skillB)) {
      return false;
    }
  }
  return true;
}
__name(areSkillMetricsEqual, "areSkillMetricsEqual");
function areMetricsEqual(a, b) {
  if (a === b) return true;
  if (!a || !b) return false;
  if (a.files.totalLinesAdded !== b.files.totalLinesAdded || a.files.totalLinesRemoved !== b.files.totalLinesRemoved) {
    return false;
  }
  const toolsA = a.tools;
  const toolsB = b.tools;
  if (toolsA.totalCalls !== toolsB.totalCalls || toolsA.totalSuccess !== toolsB.totalSuccess || toolsA.totalFail !== toolsB.totalFail || toolsA.totalDurationMs !== toolsB.totalDurationMs) {
    return false;
  }
  if (toolsA.totalDecisions["accept" /* ACCEPT */] !== toolsB.totalDecisions["accept" /* ACCEPT */] || toolsA.totalDecisions["reject" /* REJECT */] !== toolsB.totalDecisions["reject" /* REJECT */] || toolsA.totalDecisions["modify" /* MODIFY */] !== toolsB.totalDecisions["modify" /* MODIFY */] || toolsA.totalDecisions["auto_accept" /* AUTO_ACCEPT */] !== toolsB.totalDecisions["auto_accept" /* AUTO_ACCEPT */]) {
    return false;
  }
  const toolsByNameAKeys = Object.keys(toolsA.byName);
  const toolsByNameBKeys = Object.keys(toolsB.byName);
  if (toolsByNameAKeys.length !== toolsByNameBKeys.length) return false;
  for (const key of toolsByNameAKeys) {
    const toolA = toolsA.byName[key];
    const toolB = toolsB.byName[key];
    if (!toolB || !areToolCallStatsEqual(toolA, toolB)) {
      return false;
    }
  }
  if (!areSkillMetricsEqual(
    a.skills ?? EMPTY_SKILL_METRICS,
    b.skills ?? EMPTY_SKILL_METRICS
  )) {
    return false;
  }
  const modelsAKeys = Object.keys(a.models);
  const modelsBKeys = Object.keys(b.models);
  if (modelsAKeys.length !== modelsBKeys.length) return false;
  for (const key of modelsAKeys) {
    if (!b.models[key] || !areModelMetricsEqual(a.models[key], b.models[key])) {
      return false;
    }
  }
  return true;
}
__name(areMetricsEqual, "areMetricsEqual");
function cloneSessionMetrics(metrics) {
  return {
    models: Object.fromEntries(
      Object.entries(metrics.models).map(([name, model]) => [
        name,
        {
          api: { ...model.api },
          tokens: { ...model.tokens },
          bySource: Object.fromEntries(
            Object.entries(model.bySource).map(([source, sourceMetrics]) => [
              source,
              {
                api: { ...sourceMetrics.api },
                tokens: { ...sourceMetrics.tokens }
              }
            ])
          )
        }
      ])
    ),
    tools: {
      totalCalls: metrics.tools.totalCalls,
      totalSuccess: metrics.tools.totalSuccess,
      totalFail: metrics.tools.totalFail,
      totalDurationMs: metrics.tools.totalDurationMs,
      totalDecisions: { ...metrics.tools.totalDecisions },
      byName: Object.fromEntries(
        Object.entries(metrics.tools.byName).map(([name, stats]) => [
          name,
          {
            count: stats.count,
            success: stats.success,
            fail: stats.fail,
            durationMs: stats.durationMs,
            decisions: { ...stats.decisions }
          }
        ])
      )
    },
    files: {
      totalLinesAdded: metrics.files.totalLinesAdded,
      totalLinesRemoved: metrics.files.totalLinesRemoved
    },
    ...metrics.skills ? {
      skills: {
        totalCalls: metrics.skills.totalCalls,
        totalSuccess: metrics.skills.totalSuccess,
        totalFail: metrics.skills.totalFail,
        byName: Object.fromEntries(
          Object.entries(metrics.skills.byName).map(([name, stats]) => [
            name,
            {
              count: stats.count,
              success: stats.success,
              fail: stats.fail
            }
          ])
        )
      }
    } : {}
  };
}
__name(cloneSessionMetrics, "cloneSessionMetrics");
function getMetricsForDisplay(sessionId) {
  return cloneSessionMetrics(
    sessionId ? uiTelemetryService.getMetricsForSession(sessionId) : uiTelemetryService.getMetrics()
  );
}
__name(getMetricsForDisplay, "getMetricsForDisplay");
var SessionStatsContext = (0, import_react.createContext)(
  void 0
);
var createDefaultStats = /* @__PURE__ */ __name((sessionId = "") => ({
  sessionId,
  sessionStartTime: /* @__PURE__ */ new Date(),
  metrics: getMetricsForDisplay(sessionId),
  lastPromptTokenCount: 0,
  promptCount: 0
}), "createDefaultStats");
var SessionStatsProvider = /* @__PURE__ */ __name(({ sessionId, children }) => {
  const [stats, setStats] = (0, import_react.useState)(
    () => createDefaultStats(sessionId ?? "")
  );
  (0, import_react.useEffect)(() => {
    const handleUpdate = /* @__PURE__ */ __name(({
      metrics,
      lastPromptTokenCount
    }) => {
      setStats((prevState) => {
        const nextMetrics = prevState.sessionId ? getMetricsForDisplay(prevState.sessionId) : cloneSessionMetrics(metrics);
        if (prevState.lastPromptTokenCount === lastPromptTokenCount && areMetricsEqual(prevState.metrics, nextMetrics)) {
          return prevState;
        }
        return {
          ...prevState,
          metrics: nextMetrics,
          lastPromptTokenCount
        };
      });
    }, "handleUpdate");
    uiTelemetryService.on("update", handleUpdate);
    handleUpdate({
      metrics: uiTelemetryService.getMetrics(),
      lastPromptTokenCount: uiTelemetryService.getLastPromptTokenCount()
    });
    return () => {
      uiTelemetryService.off("update", handleUpdate);
    };
  }, []);
  const startNewSession = (0, import_react.useCallback)((sessionId2) => {
    setStats(() => ({
      ...createDefaultStats(sessionId2),
      lastPromptTokenCount: uiTelemetryService.getLastPromptTokenCount()
    }));
  }, []);
  const startNewPrompt = (0, import_react.useCallback)(() => {
    setStats((prevState) => ({
      ...prevState,
      promptCount: prevState.promptCount + 1
    }));
  }, []);
  const getPromptCount = (0, import_react.useCallback)(
    () => stats.promptCount,
    [stats.promptCount]
  );
  const seedPromptCount = (0, import_react.useCallback)((count) => {
    setStats((prevState) => ({
      ...prevState,
      promptCount: Math.max(prevState.promptCount, count)
    }));
  }, []);
  const value = (0, import_react.useMemo)(
    () => ({
      stats,
      startNewSession,
      startNewPrompt,
      getPromptCount,
      seedPromptCount
    }),
    [stats, startNewSession, startNewPrompt, getPromptCount, seedPromptCount]
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SessionStatsContext.Provider, { value, children });
}, "SessionStatsProvider");
var useSessionStats = /* @__PURE__ */ __name(() => {
  const context = (0, import_react.useContext)(SessionStatsContext);
  if (context === void 0) {
    throw new Error(
      "useSessionStats must be used within a SessionStatsProvider"
    );
  }
  return context;
}, "useSessionStats");

// packages/cli/src/services/commandMetadata.ts
init_esbuild_shims();
function getCommandSourceBadge(command) {
  switch (command.source) {
    case "bundled-skill":
      return "[Skill]";
    case "skill-dir-command":
      if (command.sourceDetail === "user") {
        return "[User]";
      }
      if (command.sourceDetail === "project") {
        return "[Project]";
      }
      return "[Custom]";
    case "plugin-command":
      return command.sourceDetail === "extension" ? "[Extension]" : "[Plugin]";
    case "mcp-prompt":
      return "[MCP]";
    case "builtin-command":
    default:
      return null;
  }
}
__name(getCommandSourceBadge, "getCommandSourceBadge");
function getCommandSourceGroup(command) {
  switch (command.source) {
    case "builtin-command":
      return { key: "built-in", title: "Built-in Commands", order: 0 };
    case "bundled-skill":
      return { key: "bundled-skill", title: "Bundled Skills", order: 1 };
    case "skill-dir-command":
      return { key: "custom", title: "Custom Commands", order: 2 };
    case "plugin-command":
      return { key: "plugin", title: "Plugin Commands", order: 3 };
    case "mcp-prompt":
      return { key: "mcp", title: "MCP Commands", order: 4 };
    default:
      return { key: "other", title: "Other Commands", order: 5 };
  }
}
__name(getCommandSourceGroup, "getCommandSourceGroup");
function formatSupportedModes(command) {
  const modes = getEffectiveSupportedModes(command);
  const hasInteractive = modes.includes("interactive");
  const hasNonInteractive = modes.includes("non_interactive");
  const hasAcp = modes.includes("acp");
  if (hasInteractive && hasNonInteractive && hasAcp) {
    return "[all]";
  }
  if (!hasInteractive && hasNonInteractive && hasAcp) {
    return "[headless]";
  }
  if (hasInteractive && !hasNonInteractive && !hasAcp) {
    return "[interactive]";
  }
  return modes.map((mode) => {
    switch (mode) {
      case "interactive":
        return "[i]";
      case "non_interactive":
        return "[ni]";
      case "acp":
        return "[acp]";
      default:
        return `[${mode}]`;
    }
  }).join(" ");
}
__name(formatSupportedModes, "formatSupportedModes");
function getCommandDisplayName(command, options = {}) {
  const prefix = options.prefix ?? "";
  const baseLabel = `${prefix}${command.name}`;
  if (options.matchedAlias) {
    return `${baseLabel} (alias: ${options.matchedAlias})`;
  }
  if (options.includeAliases === false) {
    return baseLabel;
  }
  const altNames = command.altNames?.filter(Boolean);
  if (!altNames || altNames.length === 0) {
    return baseLabel;
  }
  return `${baseLabel} (${altNames.join(", ")})`;
}
__name(getCommandDisplayName, "getCommandDisplayName");
function getCommandSubcommandNames(command) {
  return command.subCommands?.filter((subCommand) => !subCommand.hidden).map((subCommand) => subCommand.name) ?? [];
}
__name(getCommandSubcommandNames, "getCommandSubcommandNames");

// packages/cli/src/ui/constants.ts
init_esbuild_shims();
var EstimatedArtWidth = 59;
var BoxBorderWidth = 1;
var BOX_PADDING_X = 1;
var UI_WIDTH = EstimatedArtWidth + BOX_PADDING_X * 2 + BoxBorderWidth * 2;
var SHELL_COMMAND_NAME = "Shell Command";
var SHELL_NAME = "Shell";
var TOOL_STATUS = {
  SUCCESS: "\u2713",
  PENDING: "o",
  EXECUTING: "\u22B7",
  CONFIRMING: "?",
  CANCELED: "-",
  ERROR: "x"
};

export {
  SHELL_COMMAND_NAME,
  SHELL_NAME,
  TOOL_STATUS,
  SessionStatsProvider,
  useSessionStats,
  getCommandSourceBadge,
  getCommandSourceGroup,
  formatSupportedModes,
  getCommandDisplayName,
  getCommandSubcommandNames
};
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
