"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.webSpeech = void 0;
const APINotImplementedError_1 = require("../../../../exception/APINotImplementedError");
function webSpeech(window, opt) {
    const SpeechRecognition = ({ lang, grammars, continuous, interimResults, maxAlternatives, }) => {
        const SpeechRecognition_ = 
        // @ts-ignore
        window.SpeechRecognition || window.webkitSpeechRecognition;
        if (!SpeechRecognition_) {
            throw new APINotImplementedError_1.APINotSupportedError('Speech');
        }
        const recognition = new SpeechRecognition_();
        if (grammars) {
            recognition.grammars = grammars;
        }
        recognition.lang = lang;
        recognition.interimResults = interimResults;
        recognition.maxAlternatives = maxAlternatives;
        recognition.continuous = continuous;
        const speechRecognition = {
            start: function () {
                recognition.start();
            },
            stop: function () {
                recognition.stop();
            },
            addListener: function (event, listener) {
                let _listener = listener;
                if (event === 'error') {
                    _listener = ({ error }) => {
                        if (error === 'aborted' || error === 'no-speech') {
                            return;
                        }
                        listener(error);
                    };
                }
                else if (event === 'result') {
                    _listener = (event) => {
                        const results = event.results;
                        listener(results);
                    };
                }
                recognition.addEventListener(event, _listener);
                return () => {
                    recognition.removeEventListener(event, _listener);
                };
            },
        };
        return speechRecognition;
    };
    const SpeechGrammarList = (opt) => {
        const SpeechGrammarList_ = 
        // @ts-ignore
        window.SpeechGrammarList || window.webkitSpeechGrammarList;
        if (!SpeechGrammarList_) {
            throw new APINotImplementedError_1.APINotSupportedError('Speech Grammar');
        }
        const speechRecognitionList = new SpeechGrammarList_();
        return speechRecognitionList;
    };
    const SpeechSynthesis = (opt) => {
        if (!window.speechSynthesis) {
            throw new APINotImplementedError_1.APINotSupportedError('Speech Synthesis');
        }
        const { speechSynthesis } = window;
        return {
            getVoices: () => {
                return speechSynthesis.getVoices();
            },
            speak(utterance) {
                speechSynthesis.speak(utterance);
            },
        };
    };
    const SpeechSynthesisUtterance = ({ text, voice, }) => {
        // @ts-ignore
        if (!window.SpeechSynthesisUtterance) {
            throw new APINotImplementedError_1.APINotSupportedError('Speech Synthesis');
        }
        // @ts-ignore
        const utterance = new window.SpeechSynthesisUtterance(text);
        utterance.voice = voice;
        return utterance;
    };
    const speech = {
        SpeechRecognition,
        SpeechGrammarList,
        SpeechSynthesis,
        SpeechSynthesisUtterance,
    };
    return speech;
}
exports.webSpeech = webSpeech;
//# sourceMappingURL=speech.js.map