"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateProcessor = void 0;
const tslib_1 = require("tslib");
const replace_in_file_1 = require("replace-in-file");
const fs = (0, tslib_1.__importStar)(require("fs"));
class TemplateProcessor {
    constructor(templateData, workspace) {
        this.templateData = templateData;
        this.workspace = workspace;
    }
    async processTemplateUpdate(values) {
        if (this.templateData.templateFields.length != values.length)
            throw new Error("Template values length must match field length");
        const results = await (0, replace_in_file_1.replaceInFile)({
            files: [
                this.workspace + "/*.xml",
                this.workspace + "/*.shtml"
            ],
            from: this.templateData.templateFields.map((field) => {
                if (field.value) {
                    let value = this.escapeRegExp(field.value);
                    return new RegExp(field.updatePrefix ?
                        "(" + this.escapeRegExp(field.updatePrefix) + ")" + value :
                        value, 'g');
                }
                return new RegExp("\\\$tst\\\{" + field.token + "\\\}", 'g');
            }),
            to: values.map((value, index) => {
                let template = this.templateData.templateFields[index];
                if (template.value && template.updatePrefix)
                    return "$1" + value;
                return value;
            })
        });
        let replacements = results.reduce((count, currentValue) => {
            return count + (currentValue.hasChanged ? 1 : 0);
        }, 0);
        if (!replacements) {
            console.log("Nothing was updated in the project");
            return;
        }
        for (let i = 0; i < values.length; i++) {
            this.templateData.templateFields[i].value = values[i];
        }
        fs.writeFileSync(this.workspace + "/tstemplate.json", JSON.stringify(this.templateData, null, "\t"));
    }
    escapeRegExp(string) {
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
    }
}
exports.TemplateProcessor = TemplateProcessor;
