import { r as reactExports, j as jsxRuntimeExports } from "../_libs/react.mjs";
import { k as TooltipProvider, f as formatTokens, c as cn, l as Tooltip, m as TooltipTrigger, n as TooltipContent } from "./ProxyViewerContainer--3K3o3Sm.mjs";
import "./router-DVixpJO-.mjs";
import "../_libs/modelcontextprotocol__server.mjs";
import "../_libs/jszip.mjs";
import { K as Info } from "../_libs/lucide-react.mjs";
import "../_libs/swr.mjs";
import "../_libs/use-sync-external-store.mjs";
import "../_libs/dequal.mjs";
import "../_libs/clsx.mjs";
import "../_libs/tailwind-merge.mjs";
import "../_libs/class-variance-authority.mjs";
import "../_libs/radix-ui__react-dialog.mjs";
import "../_libs/radix-ui__primitive.mjs";
import "../_libs/radix-ui__react-compose-refs.mjs";
import "../_libs/radix-ui__react-context.mjs";
import "../_libs/radix-ui__react-id.mjs";
import "../_libs/@radix-ui/react-use-layout-effect+[...].mjs";
import "../_libs/@radix-ui/react-use-controllable-state+[...].mjs";
import "../_libs/@radix-ui/react-dismissable-layer+[...].mjs";
import "../_libs/radix-ui__react-primitive.mjs";
import "../_libs/react-dom.mjs";
import "util";
import "async_hooks";
import "stream";
import "crypto";
import "../_libs/radix-ui__react-slot.mjs";
import "../_libs/@radix-ui/react-use-callback-ref+[...].mjs";
import "../_libs/@radix-ui/react-use-escape-keydown+[...].mjs";
import "../_libs/radix-ui__react-focus-scope.mjs";
import "../_libs/radix-ui__react-portal.mjs";
import "../_libs/radix-ui__react-presence.mjs";
import "../_libs/radix-ui__react-focus-guards.mjs";
import "../_libs/react-remove-scroll.mjs";
import "tslib";
import "../_libs/react-remove-scroll-bar.mjs";
import "../_libs/react-style-singleton.mjs";
import "../_libs/get-nonce.mjs";
import "../_libs/use-sidecar.mjs";
import "../_libs/use-callback-ref.mjs";
import "../_libs/aria-hidden.mjs";
import "../_libs/diff.mjs";
import "../_libs/tanstack__react-virtual.mjs";
import "../_libs/tanstack__virtual-core.mjs";
import "../_libs/radix-ui__react-select.mjs";
import "../_libs/radix-ui__number.mjs";
import "../_libs/radix-ui__react-collection.mjs";
import "../_libs/radix-ui__react-direction.mjs";
import "../_libs/radix-ui__react-popper.mjs";
import "../_libs/floating-ui__react-dom.mjs";
import "../_libs/floating-ui__dom.mjs";
import "../_libs/floating-ui__core.mjs";
import "../_libs/floating-ui__utils.mjs";
import "../_libs/radix-ui__react-arrow.mjs";
import "../_libs/radix-ui__react-use-size.mjs";
import "../_libs/radix-ui__react-use-previous.mjs";
import "../_libs/@radix-ui/react-visually-hidden+[...].mjs";
import "../_libs/zod.mjs";
import "../_libs/radix-ui__react-tooltip.mjs";
import "../_libs/radix-ui__react-tabs.mjs";
import "../_libs/radix-ui__react-roving-focus.mjs";
import "../_libs/tanstack__react-router.mjs";
import "../_libs/tiny-warning.mjs";
import "../_libs/tanstack__router-core.mjs";
import "../_libs/cookie-es.mjs";
import "../_libs/tanstack__history.mjs";
import "../_libs/tiny-invariant.mjs";
import "../_libs/seroval.mjs";
import "../_libs/seroval-plugins.mjs";
import "node:stream/web";
import "node:stream";
import "../_libs/isbot.mjs";
import "node:fs";
import "node:fs/promises";
import "node:buffer";
import "node:path";
import "../_libs/conf.mjs";
import "node:util";
import "node:process";
import "node:crypto";
import "node:assert";
import "../_libs/dot-prop.mjs";
import "../_libs/env-paths.mjs";
import "node:os";
import "../_libs/atomically.mjs";
import "../_libs/stubborn-fs.mjs";
import "../_libs/stubborn-utils.mjs";
import "../_libs/when-exit.mjs";
import "../_libs/ajv.mjs";
import "../_libs/fast-deep-equal.mjs";
import "../_libs/json-schema-traverse.mjs";
import "../_libs/fast-uri.mjs";
import "../_libs/ajv-formats.mjs";
import "../_libs/debounce-fn.mjs";
import "../_libs/mimic-function.mjs";
import "../_libs/semver.mjs";
import "../_libs/uint8array-extras.mjs";
import "node:child_process";
import "node:worker_threads";
import "../_libs/readable-stream.mjs";
import "events";
import "node:string_decoder";
import "../_libs/process-nextick-args.mjs";
import "../_libs/isarray.mjs";
import "../_libs/safe-buffer.mjs";
import "buffer";
import "../_libs/core-util-is.mjs";
import "../_libs/inherits.mjs";
import "../_libs/util-deprecate.mjs";
import "../_libs/lie.mjs";
import "../_libs/immediate.mjs";
import "../_libs/setimmediate.mjs";
import "../_libs/pako.mjs";
const ROLE_COLOR_CLASSES = {
  system: "bg-sky-500/70",
  user: "bg-emerald-500/70",
  assistant: "bg-violet-500/70",
  tool: "bg-amber-500/70",
  tools: "bg-slate-500/70"
};
const ROLE_FOCUS_RING = {
  system: "focus-visible:ring-sky-300",
  user: "focus-visible:ring-emerald-300",
  assistant: "focus-visible:ring-violet-300",
  tool: "focus-visible:ring-amber-300",
  tools: "focus-visible:ring-slate-300"
};
const MAX_VISIBLE_SEGMENTS = 12;
const MIN_SEGMENT_PERCENT = 1;
const TOOLTIP_PREVIEW_LIMIT = 80;
const LABEL_TRUNCATE_LIMIT = 24;
function truncateLabel(label) {
  if (label.length <= LABEL_TRUNCATE_LIMIT) return label;
  return `${label.slice(0, LABEL_TRUNCATE_LIMIT - 1)}…`;
}
function truncatePreview(text) {
  const singleLine = text.replace(/\s+/g, " ").trim();
  if (singleLine.length <= TOOLTIP_PREVIEW_LIMIT) return singleLine;
  return `${singleLine.slice(0, TOOLTIP_PREVIEW_LIMIT)}…`;
}
const SegmentBar = reactExports.memo(function SegmentBar2({
  segments,
  totalTokens,
  onActivate
}) {
  const total = reactExports.useMemo(() => {
    if (totalTokens > 0) return totalTokens;
    return segments.reduce((sum, s) => sum + s.size, 0);
  }, [segments, totalTokens]);
  const visibleSegments = segments.slice(0, MAX_VISIBLE_SEGMENTS);
  const overflowSegments = segments.slice(MAX_VISIBLE_SEGMENTS);
  const overflowSize = overflowSegments.reduce((sum, s) => sum + s.size, 0);
  const overflowCount = overflowSegments.length;
  const overflowStartIndex = MAX_VISIBLE_SEGMENTS;
  const overflowEndIndex = overflowStartIndex + overflowCount - 1;
  const hasOverflow = overflowCount > 0;
  const visibleTotal = reactExports.useMemo(
    () => visibleSegments.reduce((sum, s) => sum + s.size, 0),
    [visibleSegments]
  );
  const ariaLabel = reactExports.useMemo(
    () => `Request anatomy: ~${formatTokens(total)} tokens across ${segments.length} segment${segments.length === 1 ? "" : "s"}`,
    [segments.length, total]
  );
  if (segments.length === 0 || total <= 0) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { role: "img", "aria-label": ariaLabel, className: "h-6" });
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(TooltipProvider, { delayDuration: 150, children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { role: "img", "aria-label": ariaLabel, className: "flex flex-col gap-1.5", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(
      "div",
      {
        className: "relative flex h-6 w-full overflow-hidden rounded border border-border/40 bg-muted/30",
        "data-testid": "anatomy-segment-bar",
        children: [
          visibleSegments.map((segment, index) => {
            const rawPercent = total > 0 ? segment.size / total * 100 : 0;
            const percent = Math.max(MIN_SEGMENT_PERCENT, rawPercent);
            return /* @__PURE__ */ jsxRuntimeExports.jsxs(Tooltip, { children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                "button",
                {
                  type: "button",
                  role: "button",
                  tabIndex: 0,
                  onClick: () => onActivate?.(segment),
                  onKeyDown: (e) => {
                    if (e.key === "Enter" || e.key === " ") {
                      e.preventDefault();
                      onActivate?.(segment);
                    }
                  },
                  "data-anatomy-path": segment.path,
                  "aria-label": `${segment.label}, ~${formatTokens(segment.size)} tokens`,
                  className: cn(
                    "h-full border-r border-background/80 last:border-r-0",
                    "opacity-90 hover:opacity-100 focus:opacity-100",
                    "focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-1 focus-visible:ring-offset-background",
                    ROLE_COLOR_CLASSES[segment.role],
                    ROLE_FOCUS_RING[segment.role]
                  ),
                  style: { width: `${percent}%` }
                }
              ) }),
              /* @__PURE__ */ jsxRuntimeExports.jsxs(TooltipContent, { side: "bottom", className: "max-w-sm text-xs p-2 space-y-0.5", children: [
                /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "font-semibold", children: [
                  segment.label,
                  " · ~",
                  formatTokens(segment.size),
                  " tokens"
                ] }),
                /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "text-muted-foreground", children: [
                  segment.characters.toLocaleString(),
                  " chars"
                ] }),
                segment.text.length > 0 && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-muted-foreground/90 break-words whitespace-pre-wrap", children: truncatePreview(segment.text) })
              ] })
            ] }, `${segment.path}-${index}`);
          }),
          hasOverflow && /* @__PURE__ */ jsxRuntimeExports.jsxs(Tooltip, { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
              "div",
              {
                role: "img",
                "aria-label": `${overflowCount} additional segments`,
                className: "flex h-full items-center justify-center bg-muted-foreground/30 px-1.5 text-[10px] font-mono text-background",
                style: {
                  width: `${Math.max(MIN_SEGMENT_PERCENT, overflowSize / total * 100)}%`
                },
                children: [
                  "… +",
                  overflowCount
                ]
              }
            ) }),
            /* @__PURE__ */ jsxRuntimeExports.jsxs(TooltipContent, { side: "bottom", className: "text-xs", children: [
              overflowCount,
              " more segment",
              overflowCount === 1 ? "" : "s",
              " (indices",
              " ",
              overflowStartIndex,
              "–",
              overflowEndIndex,
              ")"
            ] })
          ] })
        ]
      }
    ),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex w-full gap-1 text-[10px] text-muted-foreground", children: [
      visibleSegments.map((segment, index) => {
        const rawPercent = total > 0 ? segment.size / total * 100 : 0;
        const percent = Math.max(MIN_SEGMENT_PERCENT, rawPercent);
        return /* @__PURE__ */ jsxRuntimeExports.jsxs(
          "div",
          {
            className: "flex flex-col gap-0.5 truncate",
            style: { width: `${percent}%` },
            title: `${segment.label} · ~${formatTokens(segment.size)} tokens`,
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "truncate font-mono text-foreground/80", children: truncateLabel(segment.label) }),
              /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "truncate font-mono text-muted-foreground/70", children: [
                "~",
                formatTokens(segment.size)
              ] })
            ]
          },
          `label-${segment.path}-${index}`
        );
      }),
      hasOverflow && /* @__PURE__ */ jsxRuntimeExports.jsxs(
        "div",
        {
          className: "flex flex-col gap-0.5 truncate text-muted-foreground",
          style: { width: `${Math.max(MIN_SEGMENT_PERCENT, overflowSize / total * 100)}%` },
          children: [
            /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "truncate font-mono text-foreground/60", children: [
              "… +",
              overflowCount
            ] }),
            /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "truncate font-mono text-muted-foreground/60", children: [
              "~",
              formatTokens(overflowSize)
            ] })
          ]
        }
      )
    ] }),
    visibleTotal < total * 0.1 && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "h-0" })
  ] }) });
});
const DIVERGENCE_AMBER_THRESHOLD = 0.25;
function RequestAnatomy({
  parsed,
  inputTokens,
  onSegmentActivate,
  segments: providedSegments
}) {
  const segments = reactExports.useMemo(() => providedSegments ?? null, [providedSegments]);
  const total = reactExports.useMemo(() => (segments ?? []).reduce((sum, s) => sum + s.size, 0), [segments]);
  const showDivergenceWarning = reactExports.useMemo(() => {
    if (segments === null || segments === void 0) return false;
    if (inputTokens === null) return false;
    if (total === 0) return false;
    const ratio = Math.abs(inputTokens - total) / Math.max(inputTokens, total);
    return ratio >= DIVERGENCE_AMBER_THRESHOLD;
  }, [inputTokens, segments, total]);
  const summaryColorClass = reactExports.useMemo(() => {
    if (inputTokens === null) return "text-muted-foreground";
    if (showDivergenceWarning) return "text-amber-400";
    return "text-muted-foreground";
  }, [inputTokens, showDivergenceWarning]);
  if (segments === null || segments === void 0) {
    return null;
  }
  if (segments.length === 0) {
    return null;
  }
  if (parsed === null && providedSegments === void 0) {
    return null;
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(TooltipProvider, { delayDuration: 150, children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "px-4 py-3 space-y-2", "data-testid": "anatomy-root", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-2 text-xs", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "font-mono tabular-nums text-foreground", children: [
        "~",
        formatTokens(total),
        " tokens"
      ] }),
      inputTokens !== null && /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: cn("font-mono tabular-nums", summaryColorClass), children: [
        "(server: ",
        formatTokens(inputTokens),
        ")"
      ] }),
      showDivergenceWarning && /* @__PURE__ */ jsxRuntimeExports.jsxs(Tooltip, { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
          "button",
          {
            type: "button",
            className: "inline-flex items-center text-amber-400 hover:text-amber-300",
            "aria-label": "Token estimate diverges from server",
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(Info, { className: "size-3.5" })
          }
        ) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(TooltipContent, { className: "max-w-xs text-xs", children: "Bar uses a token estimate heuristic (~4 ASCII chars / token, ~1 CJK / emoji char per token). The server's reported value is the source of truth for cost." })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(SegmentBar, { segments, totalTokens: total, onActivate: onSegmentActivate })
  ] }) });
}
export {
  RequestAnatomy
};
