import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import path from "node:path";
import { Box, Text } from "ink";
import { getModelDisplayName, getReasoningProfile } from "@dexto/llm";
import { parseCodexBaseURL } from "@dexto/core";
import { getLLMProviderDisplayName } from "../utils/llm-provider-display.js";
import {
  getChatGPTRateLimitHint,
  shouldShowChatGPTRateLimitHint
} from "../utils/chatgpt-rate-limit.js";
import { supportsContextStats } from "../agent-backend.js";
function Footer({
  agent,
  sessionId,
  modelName,
  cwd,
  branchName,
  autoApproveEdits,
  bypassPermissions,
  planModeActive,
  isShellMode,
  chatgptRateLimitStatus
}) {
  const displayPath = cwd ? path.basename(cwd) || cwd : "";
  const displayModelName = getModelDisplayName(modelName);
  const [contextLeft, setContextLeft] = useState(null);
  const [, setLlmTick] = useState(0);
  const llmConfig = sessionId ? agent.getCurrentLLMConfig(sessionId) : null;
  const provider = llmConfig?.provider ?? null;
  const providerLabel = provider ? getLLMProviderDisplayName(provider, llmConfig?.baseURL) : null;
  const reasoningProfile = provider && llmConfig ? getReasoningProfile(provider, llmConfig.model) : null;
  const reasoningVariant = llmConfig?.reasoning?.variant ?? reasoningProfile?.defaultVariant ?? void 0;
  const showReasoningVariant = reasoningProfile?.capable === true && typeof reasoningVariant === "string";
  const isChatGPTLogin = provider === "openai-compatible" && parseCodexBaseURL(llmConfig?.baseURL)?.authMode === "chatgpt";
  const showChatGPTRateLimitHint = isChatGPTLogin && shouldShowChatGPTRateLimitHint(chatgptRateLimitStatus);
  const chatGPTRateLimitHint = showChatGPTRateLimitHint && chatgptRateLimitStatus ? getChatGPTRateLimitHint(chatgptRateLimitStatus) : null;
  const chatGPTRateLimitColor = chatgptRateLimitStatus?.exceeded ? "redBright" : (chatgptRateLimitStatus?.usedPercent ?? 0) >= 90 ? "yellowBright" : "yellow";
  useEffect(() => {
    if (!supportsContextStats(agent)) {
      setContextLeft(null);
      return;
    }
    if (!sessionId) {
      setContextLeft(null);
      return;
    }
    let cancelled = false;
    let refreshId = 0;
    const refreshContext = async () => {
      const requestId = ++refreshId;
      try {
        const stats = await agent.getContextStats(sessionId);
        if (cancelled || requestId !== refreshId) return;
        const percentLeft = Math.max(0, Math.min(100, 100 - stats.usagePercent));
        setContextLeft({
          percentLeft
        });
      } catch {
        if (!cancelled) {
          setContextLeft(null);
        }
      }
    };
    refreshContext();
    const controller = new AbortController();
    const { signal } = controller;
    const sessionEvents = [
      "llm:response",
      "llm:switched",
      "context:compacted",
      "context:pruned",
      "context:cleared",
      "message:dequeued",
      "session:reset"
    ];
    const handleEvent = (payload) => {
      if (payload.sessionId && payload.sessionId !== sessionId) return;
      refreshContext();
    };
    const handleLlmSwitched = (payload) => {
      if (payload.sessionIds && !payload.sessionIds.includes(sessionId)) return;
      refreshContext();
      setLlmTick((prev) => prev + 1);
    };
    for (const eventName of sessionEvents) {
      if (eventName === "llm:switched") {
        agent.on(eventName, handleLlmSwitched, { signal });
      } else {
        agent.on(eventName, handleEvent, { signal });
      }
    }
    return () => {
      cancelled = true;
      controller.abort();
    };
  }, [agent, sessionId]);
  const pathColor = isShellMode ? "yellow" : "blue";
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 1, children: [
    /* @__PURE__ */ jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [
      /* @__PURE__ */ jsxs(Box, { children: [
        /* @__PURE__ */ jsx(Text, { color: pathColor, children: displayPath }),
        branchName && /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
          " (",
          branchName,
          ")"
        ] })
      ] }),
      /* @__PURE__ */ jsxs(Box, { children: [
        /* @__PURE__ */ jsx(Text, { color: "cyan", children: displayModelName }),
        providerLabel && /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
          " (",
          providerLabel,
          ")"
        ] }),
        showReasoningVariant && /* @__PURE__ */ jsxs(Fragment, { children: [
          /* @__PURE__ */ jsx(Text, { color: "gray", children: " \xB7 r:" }),
          /* @__PURE__ */ jsx(Text, { color: "magentaBright", children: reasoningVariant })
        ] })
      ] })
    ] }),
    contextLeft && /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
      contextLeft.percentLeft,
      "% context left"
    ] }) }),
    chatGPTRateLimitHint && /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Text, { color: chatGPTRateLimitColor, children: chatGPTRateLimitHint }) }),
    isShellMode && /* @__PURE__ */ jsxs(Box, { children: [
      /* @__PURE__ */ jsx(Text, { color: "yellow", bold: true, children: "!" }),
      /* @__PURE__ */ jsx(Text, { color: "gray", children: " for shell mode" })
    ] }),
    planModeActive && !isShellMode && /* @__PURE__ */ jsxs(Box, { children: [
      /* @__PURE__ */ jsx(Text, { color: "magentaBright", children: "plan mode" }),
      /* @__PURE__ */ jsx(Text, { color: "gray", children: " (shift + tab to cycle)" })
    ] }),
    autoApproveEdits && !planModeActive && !isShellMode && /* @__PURE__ */ jsxs(Box, { children: [
      /* @__PURE__ */ jsx(Text, { color: "yellowBright", children: "accept edits" }),
      /* @__PURE__ */ jsx(Text, { color: "gray", children: " (shift + tab to cycle)" })
    ] }),
    bypassPermissions && !planModeActive && !autoApproveEdits && !isShellMode && /* @__PURE__ */ jsxs(Box, { children: [
      /* @__PURE__ */ jsx(Text, { color: "redBright", bold: true, children: "bypass permissions" }),
      /* @__PURE__ */ jsx(Text, { color: "gray", children: " (shift + tab to cycle)" })
    ] })
  ] });
}
export {
  Footer
};
