"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const index = require("../../../_vendor/usehooks-ts/dist-index.cjs");
const commandHistoryStore = require("../../../stores/command-history-store.cjs");
function useInputState(options) {
  const { value, onChange, onSendMessage } = options;
  const [internalInputValue, setInternalInputValue] = React.useState({
    text: "",
    mentions: []
  });
  const [isComposing, setIsComposing] = React.useState(false);
  const isNavigatingHistory = React.useRef(false);
  const contextInputRef = React.useRef(null);
  const inputValue = value ?? internalInputValue;
  const setInputValue = onChange ?? setInternalInputValue;
  const focusInput = index.useEventCallback(() => {
    setTimeout(() => {
      contextInputRef.current?.focus();
    }, 0);
  });
  const clearInput = index.useEventCallback(() => {
    setInputValue({ text: "", mentions: [] });
  });
  const handleCompositionStart = index.useEventCallback(() => {
    setIsComposing(true);
  });
  const handleCompositionEnd = index.useEventCallback(() => {
    setIsComposing(false);
  });
  const handleKeyDown = index.useEventCallback((e) => {
    const isIMEComposing = e.nativeEvent.isComposing || e.keyCode === 229 || isComposing;
    if (e.key === "Enter") {
      if (e.shiftKey) {
        return;
      }
      if (!isIMEComposing) {
        e.preventDefault();
        e.stopPropagation();
        isNavigatingHistory.current = false;
        onSendMessage?.();
        return;
      }
      return;
    }
    if (e.key === "ArrowUp" && !e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey) {
      if (isIMEComposing) return;
      e.preventDefault();
      e.stopPropagation();
      const historyCommand = commandHistoryStore.commandHistoryActions.navigateUp(inputValue.text);
      if (historyCommand !== null) {
        isNavigatingHistory.current = true;
        setInputValue({ text: historyCommand, mentions: [] });
        setTimeout(() => {
          isNavigatingHistory.current = false;
        }, 50);
      }
      return;
    }
    if (e.key === "ArrowDown" && !e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey) {
      if (isIMEComposing) return;
      e.preventDefault();
      e.stopPropagation();
      const historyCommand = commandHistoryStore.commandHistoryActions.navigateDown();
      if (historyCommand !== null) {
        isNavigatingHistory.current = true;
        setInputValue({ text: historyCommand, mentions: [] });
        setTimeout(() => {
          isNavigatingHistory.current = false;
        }, 50);
      } else {
        isNavigatingHistory.current = false;
      }
      return;
    }
    if (e.key !== "ArrowUp" && e.key !== "ArrowDown" && e.key !== "ArrowLeft" && e.key !== "ArrowRight") {
      isNavigatingHistory.current = false;
      if (e.key.length === 1 || e.key === "Backspace" || e.key === "Delete") {
        commandHistoryStore.commandHistoryActions.resetNavigation();
      }
    }
  });
  const handleInputChange = index.useEventCallback((newValue) => {
    const isUserTyping = !isNavigatingHistory.current && newValue.text !== inputValue.text;
    setInputValue(newValue);
    if (isUserTyping) {
      commandHistoryStore.commandHistoryActions.resetNavigation();
    }
  });
  return {
    // 状态
    inputValue,
    contextInputRef,
    isComposing,
    // 方法
    setInputValue,
    clearInput,
    focusInput,
    handleKeyDown,
    handleInputChange,
    handleCompositionStart,
    handleCompositionEnd
  };
}
exports.useInputState = useInputState;
//# sourceMappingURL=use-input-state.cjs.map
