"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 EditableMultiLineInput_exports = {};
__export(EditableMultiLineInput_exports, {
  default: () => EditableMultiLineInput
});
module.exports = __toCommonJS(EditableMultiLineInput_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
function EditableMultiLineInput({
  value,
  onChange,
  onSubmit,
  placeholder,
  isProcessing = false,
  onToggleSingleLine
}) {
  const [cursorPos, setCursorPos] = (0, import_react.useState)(value.length);
  (0, import_react.useEffect)(() => {
    if (cursorPos > value.length) {
      setCursorPos(value.length);
    }
  }, [value, cursorPos]);
  const { lines, currentLine, currentCol, lineStartIndices } = (0, import_react.useMemo)(() => {
    const lines2 = value.split("\n");
    const lineStartIndices2 = [];
    let pos = 0;
    for (const line of lines2) {
      lineStartIndices2.push(pos);
      pos += line.length + 1;
    }
    let currentLine2 = 0;
    for (let i = 0; i < lineStartIndices2.length; i++) {
      const lineEnd = i < lineStartIndices2.length - 1 ? lineStartIndices2[i + 1] - 1 : value.length;
      if (cursorPos <= lineEnd || i === lineStartIndices2.length - 1) {
        currentLine2 = i;
        break;
      }
    }
    const currentCol2 = cursorPos - (lineStartIndices2[currentLine2] ?? 0);
    return { lines: lines2, currentLine: currentLine2, currentCol: currentCol2, lineStartIndices: lineStartIndices2 };
  }, [value, cursorPos]);
  (0, import_ink.useInput)(
    (inputChar, key) => {
      if (isProcessing) return;
      if (key.return && (key.meta || key.ctrl)) {
        onSubmit(value);
        return;
      }
      if (key.return && key.shift) {
        onToggleSingleLine?.();
        return;
      }
      if (key.return) {
        const newValue = value.slice(0, cursorPos) + "\n" + value.slice(cursorPos);
        onChange(newValue);
        setCursorPos(cursorPos + 1);
        return;
      }
      if (key.backspace && cursorPos > 0) {
        onChange(value.slice(0, cursorPos - 1) + value.slice(cursorPos));
        setCursorPos(cursorPos - 1);
        return;
      }
      if (key.delete && cursorPos < value.length) {
        onChange(value.slice(0, cursorPos) + value.slice(cursorPos + 1));
        return;
      }
      if (key.leftArrow) {
        setCursorPos(Math.max(0, cursorPos - 1));
        return;
      }
      if (key.rightArrow) {
        setCursorPos(Math.min(value.length, cursorPos + 1));
        return;
      }
      if (key.upArrow && currentLine > 0) {
        const prevLineStart = lineStartIndices[currentLine - 1];
        const prevLineLen = lines[currentLine - 1].length;
        setCursorPos(prevLineStart + Math.min(currentCol, prevLineLen));
        return;
      }
      if (key.downArrow && currentLine < lines.length - 1) {
        const nextLineStart = lineStartIndices[currentLine + 1];
        const nextLineLen = lines[currentLine + 1].length;
        setCursorPos(nextLineStart + Math.min(currentCol, nextLineLen));
        return;
      }
      if (inputChar && !key.ctrl && !key.meta) {
        onChange(value.slice(0, cursorPos) + inputChar + value.slice(cursorPos));
        setCursorPos(cursorPos + inputChar.length);
      }
    },
    { isActive: true }
  );
  const renderLine = (line, lineIdx) => {
    const lineStart = lineStartIndices[lineIdx];
    const isCursorLine = lineIdx === currentLine;
    const cursorCol = isCursorLine ? cursorPos - lineStart : -1;
    const prefix = lineIdx === 0 ? "\xBB " : "  ";
    if (cursorCol < 0) {
      return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: prefix }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: line || " " })
      ] }, lineIdx);
    }
    const before = line.slice(0, cursorCol);
    const atCursor = line.charAt(cursorCol) || " ";
    const after = line.slice(cursorCol + 1);
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: prefix }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: before }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { backgroundColor: "green", color: "black", children: atCursor }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: after })
    ] }, lineIdx);
  };
  if (!value && placeholder) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "\xBB " }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { backgroundColor: "green", color: "black", children: " " }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
          " ",
          placeholder
        ] })
      ] }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "Multi-line mode \u2022 Cmd/Ctrl+Enter to submit \u2022 Shift+Enter for single-line" })
    ] });
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
    lines.map((line, idx) => renderLine(line, idx)),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "Multi-line mode \u2022 Cmd/Ctrl+Enter to submit \u2022 Shift+Enter for single-line" })
  ] });
}
