"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const index = require("../../../_vendor/usehooks-ts/dist-index.cjs");
const designSystem = require("@choiceform/design-system");
const useSpeechRecognition = require("./use-speech-recognition.cjs");
function useVoiceInput(options) {
  const { inputValue, language = "zh-CN", onChange, onInputChange, onSetInputValue } = options;
  const speechBaseTextRef = React.useRef("");
  const isCJK = /^(zh|ja|ko)/i.test(language);
  const handleVoiceResult = index.useEventCallback((text, isFinal) => {
    const baseText = speechBaseTextRef.current || "";
    if (isFinal) {
      const newText = baseText + (baseText && !isCJK ? "" : "") + text;
      const newValue = { text: newText, mentions: inputValue.mentions || [] };
      onChange?.(newValue);
      onSetInputValue?.(newValue);
      onInputChange?.(newValue);
      speechBaseTextRef.current = newText;
    } else {
      const previewText = baseText + (baseText && !isCJK ? "" : "") + text;
      const previewValue = { text: previewText, mentions: inputValue.mentions || [] };
      onSetInputValue?.(previewValue);
      onInputChange?.(previewValue);
    }
  });
  const webSpeech = useSpeechRecognition.useSpeechRecognition({
    lang: language,
    continuous: true,
    interimResults: true,
    onResult: handleVoiceResult,
    onError: (message) => {
      designSystem.notifications({ text: `Voice input error: ${message}` });
    }
  });
  const {
    isSupported: isVoiceSupported,
    isListening: isVoiceListening,
    start: startVoice,
    stop: stopVoice
  } = webSpeech;
  const [isLongPressing, setIsLongPressing] = React.useState(false);
  const longPressTimerRef = React.useRef(null);
  const LONG_PRESS_DELAY = 300;
  const handleVoiceMouseDown = index.useEventCallback(() => {
    if (longPressTimerRef.current) {
      clearTimeout(longPressTimerRef.current);
    }
    longPressTimerRef.current = setTimeout(() => {
      setIsLongPressing(true);
      if (!isVoiceListening) {
        speechBaseTextRef.current = inputValue.text || "";
        startVoice();
      }
    }, LONG_PRESS_DELAY);
  });
  const handleVoiceMouseUp = index.useEventCallback(() => {
    if (longPressTimerRef.current) {
      clearTimeout(longPressTimerRef.current);
      longPressTimerRef.current = null;
    }
    if (isLongPressing) {
      setIsLongPressing(false);
      if (isVoiceListening) {
        stopVoice();
      }
    }
  });
  const handleVoiceClick = index.useEventCallback(() => {
    if (!isLongPressing) {
      if (isVoiceListening) {
        stopVoice();
      } else {
        speechBaseTextRef.current = inputValue.text || "";
        startVoice();
      }
    }
  });
  React.useEffect(() => {
    return () => {
      if (longPressTimerRef.current) {
        clearTimeout(longPressTimerRef.current);
      }
    };
  }, []);
  const useControlKeyVoiceInput = (isInputFocused) => {
    React.useEffect(() => {
      const handleKeyDown = (e) => {
        if (!isInputFocused || !e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return;
        if (e.key !== "Control") return;
        e.preventDefault();
        if (!isVoiceListening) {
          speechBaseTextRef.current = inputValue.text || "";
          startVoice();
        }
      };
      const handleKeyUp = (e) => {
        if (!isInputFocused || e.key !== "Control") return;
        e.preventDefault();
        if (isVoiceListening) {
          stopVoice();
        }
      };
      window.addEventListener("keydown", handleKeyDown);
      window.addEventListener("keyup", handleKeyUp);
      return () => {
        window.removeEventListener("keydown", handleKeyDown);
        window.removeEventListener("keyup", handleKeyUp);
      };
    }, [isVoiceListening, startVoice, stopVoice, isInputFocused]);
  };
  return {
    // Voice recognition state
    isVoiceSupported,
    isVoiceListening,
    // Voice button handlers
    handleVoiceMouseDown,
    handleVoiceMouseUp,
    handleVoiceClick,
    // Control key functionality
    useControlKeyVoiceInput
  };
}
exports.useVoiceInput = useVoiceInput;
//# sourceMappingURL=use-voice-input.cjs.map
