"use client";
import * as React from 'react';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import { jsx, jsxs } from 'react/jsx-runtime';

function cn(...inputs) {
  return twMerge(clsx(inputs));
}
function getSpeechRecognitionCtor() {
  if (typeof window === "undefined") return null;
  const w = window;
  return w.SpeechRecognition ?? w.webkitSpeechRecognition ?? null;
}
function SpeechRecognition({
  isListening,
  onListeningChange,
  onTranscript,
  onError,
  lang = "en-US",
  continuous = true,
  interimResults = true,
  startLabel = "Start dictation",
  stopLabel = "Stop dictation",
  notSupportedLabel = "Speech recognition not supported in this browser",
  disabled,
  className,
  ...rest
}) {
  const recognitionRef = React.useRef(null);
  const [isSupported, setIsSupported] = React.useState(true);
  const onTranscriptRef = React.useRef(onTranscript);
  const onListeningChangeRef = React.useRef(onListeningChange);
  const onErrorRef = React.useRef(onError);
  onTranscriptRef.current = onTranscript;
  onListeningChangeRef.current = onListeningChange;
  onErrorRef.current = onError;
  const mountedRef = React.useRef(true);
  React.useEffect(
    () => () => {
      mountedRef.current = false;
    },
    []
  );
  const rebuildingRef = React.useRef(false);
  const isListeningRef = React.useRef(isListening);
  isListeningRef.current = isListening;
  React.useEffect(() => {
    const Ctor = getSpeechRecognitionCtor();
    setIsSupported(Ctor !== null);
  }, []);
  React.useEffect(() => {
    if (!isListening) return;
    const Ctor = getSpeechRecognitionCtor();
    if (!Ctor) return;
    const instance = new Ctor();
    instance.continuous = continuous;
    instance.interimResults = interimResults;
    instance.lang = lang;
    instance.onresult = (event) => {
      if (!mountedRef.current) return;
      for (let i = event.resultIndex; i < event.results.length; i++) {
        const result = event.results[i];
        const transcript = result[0]?.transcript ?? "";
        if (transcript) onTranscriptRef.current(transcript, result.isFinal);
      }
    };
    instance.onerror = (event) => {
      if (!mountedRef.current) return;
      onErrorRef.current?.(event.error, event.message);
      if (event.error !== "aborted") onListeningChangeRef.current(false);
    };
    instance.onend = () => {
      if (!mountedRef.current) return;
      if (rebuildingRef.current) return;
      onListeningChangeRef.current(false);
    };
    recognitionRef.current = instance;
    try {
      instance.start();
    } catch (err) {
      onErrorRef.current?.("start-failed", err instanceof Error ? err.message : String(err));
      onListeningChangeRef.current(false);
    }
    return () => {
      rebuildingRef.current = isListeningRef.current;
      instance.onresult = null;
      instance.onerror = null;
      instance.onend = null;
      try {
        instance.abort();
      } catch {
      }
      recognitionRef.current = null;
      queueMicrotask(() => {
        rebuildingRef.current = false;
      });
    };
  }, [isListening, continuous, interimResults, lang]);
  const tooltip = !isSupported ? notSupportedLabel : isListening ? stopLabel : startLabel;
  const accessibleName = isSupported ? startLabel : notSupportedLabel;
  const isDisabled = disabled || !isSupported;
  return /* @__PURE__ */ jsx(
    "button",
    {
      type: "button",
      ...rest,
      disabled: isDisabled,
      "aria-label": accessibleName,
      "aria-pressed": isListening,
      title: tooltip,
      onClick: (event) => {
        rest.onClick?.(event);
        if (event.defaultPrevented || isDisabled) return;
        onListeningChange(!isListening);
      },
      className: cn(
        "inline-flex h-9 w-9 items-center justify-center rounded-md border bg-background",
        "text-foreground transition-all duration-[var(--duration-normal,200ms)] ease-out",
        "hover:bg-accent hover:text-accent-foreground",
        "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
        "disabled:cursor-not-allowed disabled:opacity-50",
        isListening && "animate-pulse border-destructive text-destructive",
        className
      ),
      children: /* @__PURE__ */ jsxs(
        "svg",
        {
          "aria-hidden": true,
          viewBox: "0 0 16 16",
          width: "14",
          height: "14",
          fill: "none",
          stroke: "currentColor",
          strokeWidth: "1.5",
          strokeLinecap: "round",
          strokeLinejoin: "round",
          children: [
            /* @__PURE__ */ jsx("rect", { x: "6", y: "2", width: "4", height: "8", rx: "2" }),
            /* @__PURE__ */ jsx("path", { d: "M3.5 7.5a4.5 4.5 0 0 0 9 0" }),
            /* @__PURE__ */ jsx("path", { d: "M8 12v2" }),
            /* @__PURE__ */ jsx("path", { d: "M5.5 14h5" })
          ]
        }
      )
    }
  );
}

export { SpeechRecognition };
//# sourceMappingURL=speech-recognition.js.map
//# sourceMappingURL=speech-recognition.js.map