"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidateXml = void 0;
const xsd = require("libxmljs2-xsd");
class ValidateXml {
    constructor(context) {
        this.context = context;
    }
    async runBuildStep() {
        this.context.updateStatus("Validate TSML...");
        let doc = this.context.getXmlDoc();
        let ns = doc.documentElement.getAttribute("xmlns:ts");
        const schemaVersion = ns.indexOf("2022/09") > -1 ? "2022-09" : "2020-06";
        const schemaBasePath = __dirname + "/../../schema/" + schemaVersion + "/";
        const schema = xsd.parseFile(schemaBasePath + "tokenscript.xsd", {
            baseUrl: schemaBasePath
        });
        const errors = schema.validateFile(this.context.getOutputXmlPath());
        if (errors) {
            const errMsg = "XML Validation Errors: \r\n" +
                errors.map((error) => {
                    return "- " + error.message.trim() + " (line " + error.line + ", column " + error.column + ")";
                }).join("\r\n");
            throw new Error(errMsg);
        }
    }
}
exports.ValidateXml = ValidateXml;
