"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useTokenCounter_exports = {};
__export(useTokenCounter_exports, {
  useTokenCounter: () => useTokenCounter
});
module.exports = __toCommonJS(useTokenCounter_exports);
var import_react = require("react");
function estimateTokens(charCount) {
  return Math.ceil(charCount / 4);
}
function formatTokenCount(count, includesEstimate) {
  if (count < 1e3) return "";
  const prefix = includesEstimate ? "~" : "";
  const kValue = (count / 1e3).toFixed(1);
  return `${prefix}${kValue}K tokens`;
}
function useTokenCounter({ agent, isActive }) {
  const [lastInputTokens, setLastInputTokens] = (0, import_react.useState)(0);
  const [cumulativeOutputTokens, setCumulativeOutputTokens] = (0, import_react.useState)(0);
  const [currentSegmentEstimate, setCurrentSegmentEstimate] = (0, import_react.useState)(0);
  const currentCharCountRef = (0, import_react.useRef)(0);
  (0, import_react.useEffect)(() => {
    if (!isActive) {
      setLastInputTokens(0);
      setCumulativeOutputTokens(0);
      setCurrentSegmentEstimate(0);
      currentCharCountRef.current = 0;
      return;
    }
    const controller = new AbortController();
    const { signal } = controller;
    currentCharCountRef.current = 0;
    setLastInputTokens(0);
    setCumulativeOutputTokens(0);
    setCurrentSegmentEstimate(0);
    agent.on(
      "llm:chunk",
      (payload) => {
        if (payload.chunkType === "text") {
          currentCharCountRef.current += payload.content.length;
          const estimate = estimateTokens(currentCharCountRef.current);
          if (estimate >= 1e3) {
            setCurrentSegmentEstimate(estimate);
          }
        }
      },
      { signal }
    );
    agent.on(
      "llm:response",
      (payload) => {
        const usage = payload.tokenUsage;
        const rawInputTokens = usage.inputTokens ?? 0;
        const cacheWriteTokens = usage.cacheWriteTokens ?? 0;
        const inputTokens = Math.max(0, rawInputTokens - cacheWriteTokens);
        if (inputTokens > 0) {
          setLastInputTokens(inputTokens);
        }
        const outputTokens = usage.outputTokens ?? 0;
        if (outputTokens > 0) {
          setCumulativeOutputTokens((prev) => prev + outputTokens);
        }
        currentCharCountRef.current = 0;
        setCurrentSegmentEstimate(0);
      },
      { signal }
    );
    return () => {
      controller.abort();
    };
  }, [agent, isActive]);
  const totalActualTokens = lastInputTokens + cumulativeOutputTokens;
  const displayCount = totalActualTokens + currentSegmentEstimate;
  const includesEstimate = currentSegmentEstimate > 0;
  return {
    totalActualTokens,
    currentSegmentEstimate,
    displayCount,
    includesEstimate,
    formatted: formatTokenCount(displayCount, includesEstimate)
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useTokenCounter
});
