"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 LogLevelSelector_exports = {};
__export(LogLevelSelector_exports, {
  default: () => LogLevelSelector_default
});
module.exports = __toCommonJS(LogLevelSelector_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_BaseSelector = require("../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 = (0, import_react.forwardRef)(
  function LogLevelSelector2({ isVisible, onSelect, onClose, agent, sessionId }, ref) {
    const baseSelectorRef = (0, import_react.useRef)(null);
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    const [levels, setLevels] = (0, import_react.useState)([]);
    const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
    const [logFilePath, setLogFilePath] = (0, import_react.useState)(null);
    (0, import_react.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__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
        option.icon,
        " "
      ] }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "cyan" : "gray", bold: isSelected, children: option.level }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: isSelected ? "white" : "gray", children: [
        " - ",
        option.description
      ] }),
      option.isCurrent && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: isSelected ? "cyan" : "gray", bold: isSelected, children: [
        " ",
        "\u2190 Current"
      ] })
    ] });
    const handleSelect = (option) => {
      onSelect(option.level);
    };
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
      logFilePath && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
        "\u{1F4C1} Log file: ",
        logFilePath
      ] }) }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_BaseSelector.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;
