import { useRef, useState, useEffect } from "react";
import { useEventCallback } from "../../../_vendor/usehooks-ts/dist-index.js";
import { notifications } from "@choiceform/design-system";
import { useSpeechRecognition } from "./use-speech-recognition.js";
function useVoiceInput(options) {
  const { inputValue, language = "zh-CN", onChange, onInputChange, onSetInputValue } = options;
  const speechBaseTextRef = useRef("");
  const isCJK = /^(zh|ja|ko)/i.test(language);
  const handleVoiceResult = 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({
    lang: language,
    continuous: true,
    interimResults: true,
    onResult: handleVoiceResult,
    onError: (message) => {
      notifications({ text: `Voice input error: ${message}` });
    }
  });
  const {
    isSupported: isVoiceSupported,
    isListening: isVoiceListening,
    start: startVoice,
    stop: stopVoice
  } = webSpeech;
  const [isLongPressing, setIsLongPressing] = useState(false);
  const longPressTimerRef = useRef(null);
  const LONG_PRESS_DELAY = 300;
  const handleVoiceMouseDown = useEventCallback(() => {
    if (longPressTimerRef.current) {
      clearTimeout(longPressTimerRef.current);
    }
    longPressTimerRef.current = setTimeout(() => {
      setIsLongPressing(true);
      if (!isVoiceListening) {
        speechBaseTextRef.current = inputValue.text || "";
        startVoice();
      }
    }, LONG_PRESS_DELAY);
  });
  const handleVoiceMouseUp = useEventCallback(() => {
    if (longPressTimerRef.current) {
      clearTimeout(longPressTimerRef.current);
      longPressTimerRef.current = null;
    }
    if (isLongPressing) {
      setIsLongPressing(false);
      if (isVoiceListening) {
        stopVoice();
      }
    }
  });
  const handleVoiceClick = useEventCallback(() => {
    if (!isLongPressing) {
      if (isVoiceListening) {
        stopVoice();
      } else {
        speechBaseTextRef.current = inputValue.text || "";
        startVoice();
      }
    }
  });
  useEffect(() => {
    return () => {
      if (longPressTimerRef.current) {
        clearTimeout(longPressTimerRef.current);
      }
    };
  }, []);
  const useControlKeyVoiceInput = (isInputFocused) => {
    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
  };
}
export {
  useVoiceInput
};
//# sourceMappingURL=use-voice-input.js.map
