"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaItemInfoGeneratorTest = void 0;
const ProjectInfoItem_1 = require("./ProjectInfoItem");
const IInfoItemData_1 = require("./IInfoItemData");
const Database_1 = require("../minecraft/Database");
const CartoApp_1 = require("../app/CartoApp");
const axios_1 = require("axios");
const Utilities_1 = require("../core/Utilities");
const MinecraftDefinitions_1 = require("../minecraft/MinecraftDefinitions");
const ProjectItemUtilities_1 = require("../app/ProjectItemUtilities");
const json_schema_1 = require("json-schema");
const JsonSchemaErrorBase = 100;
const NotCurrentFormatVersionBase = 1100;
var SchemaItemInfoGeneratorTest;
(function (SchemaItemInfoGeneratorTest) {
    SchemaItemInfoGeneratorTest[SchemaItemInfoGeneratorTest["couldNotParseJson"] = 1] = "couldNotParseJson";
})(SchemaItemInfoGeneratorTest = exports.SchemaItemInfoGeneratorTest || (exports.SchemaItemInfoGeneratorTest = {}));
class SchemaItemInfoGenerator {
    id = "JSON";
    title = "JSON Structure Validation";
    canAlwaysProcess = true;
    _schemaContentByPath = {};
    constructor() {
        this.loadSchema = this.loadSchema.bind(this);
    }
    getTopicData(topicId) {
        return {
            title: topicId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async loadSchema(uri) {
        const res = await axios_1.default.get(Utilities_1.default.ensureEndsWithSlash(CartoApp_1.default.contentRoot) + uri);
        return res.data;
    }
    async generate(projectItem, contentIndex) {
        const items = [];
        if (projectItem.primaryFile &&
            projectItem.primaryFile.content &&
            typeof projectItem.primaryFile.content === "string") {
            const schemaPath = projectItem.getSchemaPath();
            if (schemaPath) {
                let verIsCurrent = await MinecraftDefinitions_1.default.formatVersionIsCurrent(projectItem);
                if (verIsCurrent) {
                    let schemaContents = this._schemaContentByPath[schemaPath];
                    if (!schemaContents) {
                        schemaContents = await Database_1.default.getSchema(schemaPath);
                        if (schemaContents) {
                            this._schemaContentByPath[schemaPath] = schemaContents;
                        }
                    }
                    if (schemaContents) {
                        let content = projectItem.primaryFile.content;
                        let contentObj = undefined;
                        content = Utilities_1.default.fixJsonContent(content);
                        try {
                            contentObj = JSON.parse(content);
                            const results = (0, json_schema_1.validate)(contentObj, schemaContents);
                            for (const err of results.errors) {
                                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.warning, this.id, JsonSchemaErrorBase + projectItem.itemType, `JSON structure error`, projectItem, `(${err.property}) ${err.message}`));
                            }
                        }
                        catch (e) {
                            let errorMess = e;
                            if (e.message) {
                                errorMess = e.message;
                            }
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, SchemaItemInfoGeneratorTest.couldNotParseJson, "Could not parse JSON - " + errorMess, projectItem));
                        }
                        if (contentObj) {
                        }
                    }
                }
                else {
                    let fvStr = "";
                    const fv = await MinecraftDefinitions_1.default.getFormatVersion(projectItem);
                    if (fv) {
                        fvStr = " (is at " + fv.join(".") + ")";
                    }
                    items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, NotCurrentFormatVersionBase + projectItem.itemType, ProjectItemUtilities_1.default.getDescriptionForType(projectItem.itemType) +
                        " is not at a current format version" +
                        fvStr, projectItem));
                }
            }
        }
        return items;
    }
}
exports.default = SchemaItemInfoGenerator;

//# sourceMappingURL=../maps/info/SchemaItemInfoGenerator.js.map
