import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, forwardRef, useImperativeHandle, useCallback } from "react";
import { Box, Text } from "ink";
const SessionRenameOverlay = forwardRef(
  function SessionRenameOverlay2({ isVisible, currentTitle, onRename, onClose }, ref) {
    const [title, setTitle] = useState(currentTitle || "");
    const [error, setError] = useState(null);
    useEffect(() => {
      if (isVisible) {
        setTitle(currentTitle || "");
        setError(null);
      }
    }, [isVisible, currentTitle]);
    const handleSubmit = useCallback(() => {
      const trimmedTitle = title.trim();
      if (!trimmedTitle) {
        setError("Title cannot be empty");
        return;
      }
      onRename(trimmedTitle);
    }, [title, onRename]);
    useImperativeHandle(
      ref,
      () => ({
        handleInput: (input, key) => {
          if (!isVisible) return false;
          if (key.escape) {
            onClose();
            return true;
          }
          if (key.return) {
            handleSubmit();
            return true;
          }
          if (key.backspace || key.delete) {
            setTitle((prev) => prev.slice(0, -1));
            setError(null);
            return true;
          }
          if (input && !key.ctrl && !key.meta) {
            setTitle((prev) => prev + input);
            setError(null);
            return true;
          }
          return false;
        }
      }),
      [isVisible, onClose, handleSubmit]
    );
    if (!isVisible) return null;
    return /* @__PURE__ */ jsxs(
      Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: "cyan",
        paddingX: 1,
        marginTop: 1,
        children: [
          /* @__PURE__ */ jsx(Box, { marginBottom: 1, children: /* @__PURE__ */ jsx(Text, { bold: true, color: "cyan", children: "Rename Session" }) }),
          currentTitle && /* @__PURE__ */ jsxs(Box, { marginBottom: 1, children: [
            /* @__PURE__ */ jsx(Text, { color: "gray", children: "Current: " }),
            /* @__PURE__ */ jsx(Text, { color: "white", children: currentTitle })
          ] }),
          /* @__PURE__ */ jsx(Box, { flexDirection: "column", children: /* @__PURE__ */ jsx(Text, { bold: true, children: "Enter new title:" }) }),
          /* @__PURE__ */ jsxs(Box, { marginTop: 1, children: [
            /* @__PURE__ */ jsx(Text, { color: "cyan", children: "> " }),
            /* @__PURE__ */ jsx(Text, { children: title }),
            /* @__PURE__ */ jsx(Text, { color: "cyan", children: "_" })
          ] }),
          error && /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: "red", children: error }) }),
          /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: "gray", children: "Enter to save \u2022 Esc to cancel" }) })
        ]
      }
    );
  }
);
var SessionRenameOverlay_default = SessionRenameOverlay;
export {
  SessionRenameOverlay_default as default
};
