"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MicroTemplateService = void 0;
const util = require("util");
const os = require("os");
const constants = require("../../constants");
const yok_1 = require("../yok");
const _ = require("lodash");
class MicroTemplateService {
    constructor($injector) {
        this.$injector = $injector;
        // Injector's dynamicCallRegex doesn't have 'g' option, which we need here.
        // Use ( ) in order to use $1 to get whole expression later
        this.dynamicCallRegex = new RegExp(util.format("(%s)", this.$injector.dynamicCallRegex.source), "g");
    }
    parseContent(data, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const localVariables = this.getLocalVariables(options);
            const compiledTemplate = _.template(data.replace(this.dynamicCallRegex, 'this.$injector.getDynamicCallData("$1")'));
            // When debugging parsing, uncomment the line below:
            // console.log(compiledTemplate.source);
            return yield compiledTemplate.apply(this, [localVariables]);
        });
    }
    isPlatform(...args) {
        const platform = os.platform().toLowerCase();
        return _.some(args, (arg) => arg.toLowerCase() === platform);
    }
    getLocalVariables(options) {
        const isHtml = options.isHtml;
        // in html help we want to show all help. Only CONSOLE specific help(wrapped in if(isConsole) ) must be omitted
        const localVariables = {
            constants,
        };
        localVariables["isLinux"] = isHtml || this.isPlatform("linux");
        localVariables["isWindows"] = isHtml || this.isPlatform("win32");
        localVariables["isMacOS"] = isHtml || this.isPlatform("darwin");
        localVariables["isConsole"] = !isHtml;
        localVariables["isHtml"] = isHtml;
        localVariables["isJekyll"] = false;
        return localVariables;
    }
}
exports.MicroTemplateService = MicroTemplateService;
yok_1.injector.register("microTemplateService", MicroTemplateService);
//# sourceMappingURL=micro-templating-service.js.map