import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, forwardRef, useRef, useImperativeHandle } from "react";
import { Text } from "ink";
import { BaseSelector } from "../base/BaseSelector.js";
import { isStreamingEnabled, setStreamingEnabled } from "../../state/streaming-state.js";
const StreamSelector = forwardRef(
  function StreamSelector2({ isVisible, onSelect, onClose }, ref) {
    const baseSelectorRef = useRef(null);
    useImperativeHandle(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    const [options, setOptions] = useState([]);
    const [selectedIndex, setSelectedIndex] = useState(0);
    useEffect(() => {
      if (!isVisible) return;
      const currentEnabled = isStreamingEnabled();
      const optionList = [
        {
          id: "enabled",
          label: "Enabled (Experimental)",
          description: "Show responses as they are generated",
          icon: "\u25B6\uFE0F",
          isCurrent: currentEnabled
        },
        {
          id: "disabled",
          label: "Disabled",
          description: "Show complete response when finished (default)",
          icon: "\u23F8\uFE0F",
          isCurrent: !currentEnabled
        }
      ];
      setOptions(optionList);
      setSelectedIndex(currentEnabled ? 0 : 1);
    }, [isVisible]);
    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.label }),
      /* @__PURE__ */ jsxs(Text, { color: isSelected ? "white" : "gray", children: [
        " - ",
        option.description
      ] }),
      option.isCurrent && /* @__PURE__ */ jsxs(Text, { color: "green", bold: true, children: [
        " ",
        "\u2713"
      ] })
    ] });
    const handleSelect = (option) => {
      const enabled = option.id === "enabled";
      setStreamingEnabled(enabled);
      onSelect(enabled);
    };
    return /* @__PURE__ */ jsx(
      BaseSelector,
      {
        ref: baseSelectorRef,
        items: options,
        isVisible,
        isLoading: false,
        selectedIndex,
        onSelectIndex: setSelectedIndex,
        onSelect: handleSelect,
        onClose,
        formatItem,
        title: "Streaming Mode",
        borderColor: "cyan",
        emptyMessage: "No options available"
      }
    );
  }
);
var StreamSelector_default = StreamSelector;
export {
  StreamSelector_default as default
};
