"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormSchemaItemInfoGeneratorTest = void 0;
const ProjectInfoItem_1 = require("./ProjectInfoItem");
const CartoApp_1 = require("../app/CartoApp");
const axios_1 = require("axios");
const Utilities_1 = require("../core/Utilities");
const Database_1 = require("../minecraft/Database");
const IInfoItemData_1 = require("./IInfoItemData");
const DataFormValidator_1 = require("../dataform/DataFormValidator");
const StorageUtilities_1 = require("../storage/StorageUtilities");
var FormSchemaItemInfoGeneratorTest;
(function (FormSchemaItemInfoGeneratorTest) {
    FormSchemaItemInfoGeneratorTest[FormSchemaItemInfoGeneratorTest["couldNotParseJson"] = 401] = "couldNotParseJson";
    FormSchemaItemInfoGeneratorTest[FormSchemaItemInfoGeneratorTest["couldNotFindForm"] = 402] = "couldNotFindForm";
})(FormSchemaItemInfoGeneratorTest = exports.FormSchemaItemInfoGeneratorTest || (exports.FormSchemaItemInfoGeneratorTest = {}));
class FormSchemaItemInfoGenerator {
    id = "JSONF";
    title = "JSON Structure Validation (via Minecraft docs)";
    canAlwaysProcess = true;
    _schemaContentByPath = {};
    uuidRegex = new RegExp("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");
    constructor() {
        this.testUuid = this.testUuid.bind(this);
        this.testUri = this.testUri.bind(this);
    }
    getTopicData(topicId) {
        return {
            title: topicId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async loadSchema(uri) {
        console.log("Retrieving " + uri);
        const res = await axios_1.default.get(Utilities_1.default.ensureEndsWithSlash(CartoApp_1.default.contentRoot) + uri);
        console.log("Loading error: " + JSON.stringify(res.data));
        return res.data;
    }
    async generate(projectItem, contentIndex) {
        const items = [];
        if (projectItem.primaryFile &&
            projectItem.primaryFile.content &&
            typeof projectItem.primaryFile.content === "string") {
            const formPath = projectItem.getFormPath();
            if (formPath) {
                const form = await Database_1.default.ensureFormLoadedByPath(formPath);
                if (form) {
                    const data = StorageUtilities_1.default.getJsonObject(projectItem.primaryFile);
                    if (!data) {
                        items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, FormSchemaItemInfoGeneratorTest.couldNotParseJson, "Could not parse JSON. " +
                            (projectItem.primaryFile.errorStateMessage ? projectItem.primaryFile.errorStateMessage : ""), projectItem, formPath));
                    }
                    else {
                        const results = await DataFormValidator_1.default.validate(data, form);
                        if (results) {
                            for (const result of results) {
                                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.warning, this.id, result.type, result.message, projectItem));
                            }
                        }
                    }
                }
                else {
                    items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.internalProcessingError, this.id, FormSchemaItemInfoGeneratorTest.couldNotFindForm, "Could not find expected form", undefined, formPath));
                }
            }
        }
        return items;
    }
    testUuid(uuidString) {
        return this.uuidRegex.test(uuidString);
    }
    testUnknownFormat(formatString) {
        return true;
    }
    testUri(uriString) {
        // could get much more sophisticated here...
        return uriString.indexOf("://") >= 0;
    }
}
exports.default = FormSchemaItemInfoGenerator;

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