"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MicrophoneButton = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
// Import PatternFly components
const react_core_1 = require("@patternfly/react-core");
// Import FontAwesome icons
const microphone_icon_1 = require("@patternfly/react-icons/dist/esm/icons/microphone-icon");
const MicrophoneButton = (_a) => {
    var { isListening, onIsListeningChange, onSpeechRecognition, className, tooltipProps, tooltipContent = { active: 'Stop listening', inactive: 'Use microphone' }, language = 'en-US', isCompact } = _a, props = __rest(_a, ["isListening", "onIsListeningChange", "onSpeechRecognition", "className", "tooltipProps", "tooltipContent", "language", "isCompact"]);
    // Microphone
    // --------------------------------------------------------------------------
    const [speechRecognition, setSpeechRecognition] = (0, react_1.useState)();
    // Listen for speech
    const startListening = (0, react_1.useCallback)(() => {
        if (speechRecognition) {
            speechRecognition.start();
            onIsListeningChange(true);
        }
    }, [onIsListeningChange, speechRecognition]);
    // Stop listening for speech
    const stopListening = (0, react_1.useCallback)(() => {
        if (speechRecognition && isListening) {
            speechRecognition.stop();
            onIsListeningChange(false);
        }
    }, [isListening, onIsListeningChange, speechRecognition]);
    // Detect speech recognition browser support
    (0, react_1.useEffect)(() => {
        if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) {
            // Initialize SpeechRecognition
            const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
            recognition.continuous = false;
            recognition.interimResults = false;
            recognition.lang = language;
            recognition.onresult = (event) => {
                const result = event.results[0][0].transcript;
                onSpeechRecognition(result);
                recognition.stop();
                // turn button off
                onIsListeningChange(false);
            };
            recognition.onerror = (event) => {
                // eslint-disable-next-line no-console
                console.error('Speech recognition error:', event.error);
                recognition.stop();
            };
            setSpeechRecognition(recognition);
        }
    }, [onSpeechRecognition, language, onIsListeningChange]);
    if (!speechRecognition) {
        return null;
    }
    return ((0, jsx_runtime_1.jsx)(react_core_1.Tooltip, Object.assign({ aria: "none", "aria-live": "polite", id: "pf-chatbot__tooltip--use-microphone", content: isListening ? tooltipContent.active : tooltipContent.inactive, position: (tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.position) || 'top', entryDelay: (tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.entryDelay) || 0, exitDelay: (tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.exitDelay) || 0, distance: (tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.distance) || 8, animationDuration: (tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.animationDuration) || 0 }, tooltipProps, { children: (0, jsx_runtime_1.jsx)(react_core_1.Button, Object.assign({ variant: "plain", className: `pf-chatbot__button--microphone ${isListening ? 'pf-chatbot__button--microphone--active' : ''} ${isCompact ? 'pf-m-compact' : ''} ${className !== null && className !== void 0 ? className : ''}`, "aria-label": props['aria-label'] || (isListening ? 'Stop listening' : 'Use microphone'), "aria-pressed": isListening, onClick: isListening ? stopListening : startListening, icon: (0, jsx_runtime_1.jsx)(react_core_1.Icon, { iconSize: isCompact ? 'lg' : 'xl', isInline: true, children: (0, jsx_runtime_1.jsx)(microphone_icon_1.MicrophoneIcon, {}) }), size: isCompact ? 'sm' : undefined }, props)) })));
};
exports.MicrophoneButton = MicrophoneButton;
exports.default = exports.MicrophoneButton;
