import { useState, useRef } from "react";
import { useEventCallback } from "../../../_vendor/usehooks-ts/dist-index.js";
import { commandHistoryActions } from "../../../stores/command-history-store.js";
function useInputState(options) {
  const { value, onChange, onSendMessage } = options;
  const [internalInputValue, setInternalInputValue] = useState({
    text: "",
    mentions: []
  });
  const [isComposing, setIsComposing] = useState(false);
  const isNavigatingHistory = useRef(false);
  const contextInputRef = useRef(null);
  const inputValue = value ?? internalInputValue;
  const setInputValue = onChange ?? setInternalInputValue;
  const focusInput = useEventCallback(() => {
    setTimeout(() => {
      contextInputRef.current?.focus();
    }, 0);
  });
  const clearInput = useEventCallback(() => {
    setInputValue({ text: "", mentions: [] });
  });
  const handleCompositionStart = useEventCallback(() => {
    setIsComposing(true);
  });
  const handleCompositionEnd = useEventCallback(() => {
    setIsComposing(false);
  });
  const handleKeyDown = 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 = 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 = 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") {
        commandHistoryActions.resetNavigation();
      }
    }
  });
  const handleInputChange = useEventCallback((newValue) => {
    const isUserTyping = !isNavigatingHistory.current && newValue.text !== inputValue.text;
    setInputValue(newValue);
    if (isUserTyping) {
      commandHistoryActions.resetNavigation();
    }
  });
  return {
    // 状态
    inputValue,
    contextInputRef,
    isComposing,
    // 方法
    setInputValue,
    clearInput,
    focusInput,
    handleKeyDown,
    handleInputChange,
    handleCompositionStart,
    handleCompositionEnd
  };
}
export {
  useInputState
};
//# sourceMappingURL=use-input-state.js.map
