import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, useMemo } from "react";
import { Box, Text, useInput } from "ink";
function EditableMultiLineInput({
  value,
  onChange,
  onSubmit,
  placeholder,
  isProcessing = false,
  onToggleSingleLine
}) {
  const [cursorPos, setCursorPos] = useState(value.length);
  useEffect(() => {
    if (cursorPos > value.length) {
      setCursorPos(value.length);
    }
  }, [value, cursorPos]);
  const { lines, currentLine, currentCol, lineStartIndices } = 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]);
  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__ */ jsxs(Text, { children: [
        /* @__PURE__ */ jsx(Text, { color: "cyan", children: prefix }),
        /* @__PURE__ */ jsx(Text, { children: line || " " })
      ] }, lineIdx);
    }
    const before = line.slice(0, cursorCol);
    const atCursor = line.charAt(cursorCol) || " ";
    const after = line.slice(cursorCol + 1);
    return /* @__PURE__ */ jsxs(Text, { children: [
      /* @__PURE__ */ jsx(Text, { color: "cyan", children: prefix }),
      /* @__PURE__ */ jsx(Text, { children: before }),
      /* @__PURE__ */ jsx(Text, { backgroundColor: "green", color: "black", children: atCursor }),
      /* @__PURE__ */ jsx(Text, { children: after })
    ] }, lineIdx);
  };
  if (!value && placeholder) {
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
      /* @__PURE__ */ jsxs(Text, { children: [
        /* @__PURE__ */ jsx(Text, { color: "cyan", children: "\xBB " }),
        /* @__PURE__ */ jsx(Text, { backgroundColor: "green", color: "black", children: " " }),
        /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
          " ",
          placeholder
        ] })
      ] }),
      /* @__PURE__ */ jsx(Text, { color: "gray", children: "Multi-line mode \u2022 Cmd/Ctrl+Enter to submit \u2022 Shift+Enter for single-line" })
    ] });
  }
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
    lines.map((line, idx) => renderLine(line, idx)),
    /* @__PURE__ */ jsx(Text, { color: "gray", children: "Multi-line mode \u2022 Cmd/Ctrl+Enter to submit \u2022 Shift+Enter for single-line" })
  ] });
}
export {
  EditableMultiLineInput as default
};
