import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, forwardRef, useRef, useImperativeHandle } from "react";
import { Box, Text } from "ink";
import { BaseSelector } from "../base/BaseSelector.js";
const LOG_LEVELS = [
  { level: "error", description: "Errors only", icon: "!" },
  { level: "warn", description: "Warnings and above", icon: "\u26A0\uFE0F" },
  { level: "info", description: "Info and above (default)", icon: "\u2139\uFE0F" },
  { level: "debug", description: "Debug information", icon: "\u{1F50D}" },
  { level: "silly", description: "Everything (most verbose)", icon: "\u{1F52C}" }
];
const LogLevelSelector = forwardRef(
  function LogLevelSelector2({ isVisible, onSelect, onClose, agent, sessionId }, ref) {
    const baseSelectorRef = useRef(null);
    useImperativeHandle(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    const [levels, setLevels] = useState([]);
    const [selectedIndex, setSelectedIndex] = useState(0);
    const [logFilePath, setLogFilePath] = useState(null);
    useEffect(() => {
      let isCancelled = false;
      const run = async () => {
        if (!isVisible) {
          setLogFilePath(null);
          return;
        }
        const currentLevel = agent.logger.getLevel();
        const levelList = LOG_LEVELS.map((l) => ({
          ...l,
          isCurrent: l.level === currentLevel
        }));
        setLevels(levelList);
        const session = sessionId ? await agent.getSession(sessionId) : void 0;
        if (!isCancelled) {
          setLogFilePath(session?.logger.getLogFilePath() ?? null);
        }
        const currentIndex = levelList.findIndex((l) => l.isCurrent);
        if (currentIndex >= 0) {
          setSelectedIndex(currentIndex);
        }
      };
      void run();
      return () => {
        isCancelled = true;
      };
    }, [isVisible, agent, sessionId]);
    const formatItem = (option, isSelected) => /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsxs(Text, { children: [
        option.icon,
        " "
      ] }),
      /* @__PURE__ */ jsx(Text, { color: isSelected ? "cyan" : "gray", bold: isSelected, children: option.level }),
      /* @__PURE__ */ jsxs(Text, { color: isSelected ? "white" : "gray", children: [
        " - ",
        option.description
      ] }),
      option.isCurrent && /* @__PURE__ */ jsxs(Text, { color: isSelected ? "cyan" : "gray", bold: isSelected, children: [
        " ",
        "\u2190 Current"
      ] })
    ] });
    const handleSelect = (option) => {
      onSelect(option.level);
    };
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
      logFilePath && /* @__PURE__ */ jsx(Box, { marginBottom: 1, children: /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
        "\u{1F4C1} Log file: ",
        logFilePath
      ] }) }),
      /* @__PURE__ */ jsx(
        BaseSelector,
        {
          ref: baseSelectorRef,
          items: levels,
          isVisible,
          isLoading: false,
          selectedIndex,
          onSelectIndex: setSelectedIndex,
          onSelect: handleSelect,
          onClose,
          formatItem,
          title: "Select Log Level",
          borderColor: "yellowBright",
          emptyMessage: "No log levels available"
        }
      )
    ] });
  }
);
var LogLevelSelector_default = LogLevelSelector;
export {
  LogLevelSelector_default as default
};
