"use strict";
/********************************************************************************
 * Copyright (C) 2015-2018 Red Hat, Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GrammarsReader = void 0;
var inversify_1 = require("inversify");
var path = require("path");
var fs = require("fs");
var GrammarsReader = /** @class */ (function () {
    function GrammarsReader() {
    }
    GrammarsReader.prototype.readGrammars = function (rawGrammars, pluginPath) {
        var e_1, _a;
        var result = new Array();
        try {
            for (var rawGrammars_1 = __values(rawGrammars), rawGrammars_1_1 = rawGrammars_1.next(); !rawGrammars_1_1.done; rawGrammars_1_1 = rawGrammars_1.next()) {
                var rawGrammar = rawGrammars_1_1.value;
                var grammar = this.readGrammar(rawGrammar, pluginPath);
                if (grammar) {
                    result.push(grammar);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (rawGrammars_1_1 && !rawGrammars_1_1.done && (_a = rawGrammars_1.return)) _a.call(rawGrammars_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return result;
    };
    GrammarsReader.prototype.readGrammar = function (rawGrammar, pluginPath) {
        // TODO: validate inputs
        var grammar;
        if (rawGrammar.path.endsWith('json')) {
            grammar = require(path.resolve(pluginPath, rawGrammar.path));
        }
        else {
            grammar = fs.readFileSync(path.resolve(pluginPath, rawGrammar.path), 'utf8');
        }
        return {
            language: rawGrammar.language,
            scope: rawGrammar.scopeName,
            format: rawGrammar.path.endsWith('json') ? 'json' : 'plist',
            grammar: grammar,
            grammarLocation: rawGrammar.path,
            injectTo: rawGrammar.injectTo,
            embeddedLanguages: rawGrammar.embeddedLanguages,
            tokenTypes: rawGrammar.tokenTypes
        };
    };
    GrammarsReader = __decorate([
        inversify_1.injectable()
    ], GrammarsReader);
    return GrammarsReader;
}());
exports.GrammarsReader = GrammarsReader;
//# sourceMappingURL=grammars-reader.js.map