"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 SessionRenameOverlay_exports = {};
__export(SessionRenameOverlay_exports, {
  default: () => SessionRenameOverlay_default
});
module.exports = __toCommonJS(SessionRenameOverlay_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
const SessionRenameOverlay = (0, import_react.forwardRef)(
  function SessionRenameOverlay2({ isVisible, currentTitle, onRename, onClose }, ref) {
    const [title, setTitle] = (0, import_react.useState)(currentTitle || "");
    const [error, setError] = (0, import_react.useState)(null);
    (0, import_react.useEffect)(() => {
      if (isVisible) {
        setTitle(currentTitle || "");
        setError(null);
      }
    }, [isVisible, currentTitle]);
    const handleSubmit = (0, import_react.useCallback)(() => {
      const trimmedTitle = title.trim();
      if (!trimmedTitle) {
        setError("Title cannot be empty");
        return;
      }
      onRename(trimmedTitle);
    }, [title, onRename]);
    (0, import_react.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__ */ (0, import_jsx_runtime.jsxs)(
      import_ink.Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: "cyan",
        paddingX: 1,
        marginTop: 1,
        children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { bold: true, color: "cyan", children: "Rename Session" }) }),
          currentTitle && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { marginBottom: 1, children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "Current: " }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "white", children: currentTitle })
          ] }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { flexDirection: "column", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { bold: true, children: "Enter new title:" }) }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { marginTop: 1, children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "> " }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: title }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "_" })
          ] }),
          error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "red", children: error }) }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "Enter to save \u2022 Esc to cancel" }) })
        ]
      }
    );
  }
);
var SessionRenameOverlay_default = SessionRenameOverlay;
