"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.grammarsFrom = exports.JSGFStrFrom = exports.specNameGrammar = void 0;
const APINotImplementedError_1 = require("../exception/APINotImplementedError");
const specNameGrammar = (system) => {
    const { specs } = system;
    const token_set = new Set();
    for (let id in specs) {
        const spec = specs[id];
        const { name = '' } = spec;
        const name_tokens = name.split(' ');
        for (const name_token of name_tokens) {
            token_set.add(name_token);
        }
    }
    const tokens = Array.from(token_set).sort();
    return (0, exports.grammarsFrom)(system, tokens);
};
exports.specNameGrammar = specNameGrammar;
const JSGFStrFrom = (tokens) => {
    const rule = tokens.join(' | ');
    const grammar = `#JSGF V1.0; grammar tokens; public <token> = ${rule} ;`;
    return grammar;
};
exports.JSGFStrFrom = JSGFStrFrom;
const grammarsFrom = (system, tokens) => {
    const { api: { speech: { SpeechGrammarList }, }, } = system;
    if (!SpeechGrammarList) {
        throw new APINotImplementedError_1.APINotSupportedError('Speech Grammar List');
    }
    const grammarsStr = (0, exports.JSGFStrFrom)(tokens);
    const grammars = new SpeechGrammarList({});
    grammars.addFromString(grammarsStr, 1);
    return grammars;
};
exports.grammarsFrom = grammarsFrom;
//# sourceMappingURL=grammar.js.map