"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const IProjectItemData_1 = require("../../app/IProjectItemData");
const ImageUtilites_1 = require("../../storage/ImageUtilites");
const IInfoItemData_1 = require("../IInfoItemData");
const ProjectInfoItem_1 = require("../ProjectInfoItem");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const AsyncUtilities_1 = require("../../core/async/AsyncUtilities");
const Log_1 = require("../../core/Log");
const DefaultEduImageWidth = 480;
const DefaultEduImageHeight = 270;
const DefaultBedrockImageWidth = 800;
const DefaultBedrockImageHeight = 450;
var CheckWorldIconsGeneratorTest;
(function (CheckWorldIconsGeneratorTest) {
    CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["NoIconFound"] = 101] = "NoIconFound";
    CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["MultipleIconsFound"] = 102] = "MultipleIconsFound";
    CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["IconNotValidImage"] = 103] = "IconNotValidImage";
    CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["IconNotValidSize"] = 104] = "IconNotValidSize";
})(CheckWorldIconsGeneratorTest || (CheckWorldIconsGeneratorTest = {}));
/*
 * Generator for validating World Icons
 *
 * Will ensure:
 *  * exactly 1 icon per world folder (folders with a world manifest)
 *  * icon is a valid .jpeg
 *  * icon is the correct size
 */
class CheckWorldIconsGenerator {
    id = "CWI";
    title = "Check World Icon Generator";
    canAlwaysProcess = true;
    severity = IInfoItemData_1.InfoItemType.error;
    async generate(project) {
        const itemsCopy = project.getItemsCopy();
        const isEDUOffer = itemsCopy.find((item) => item.itemType === IProjectItemData_1.ProjectItemType.educationJson) !== undefined;
        const worldFolders = [];
        for (const projectItem of itemsCopy) {
            if (projectItem.itemType === IProjectItemData_1.ProjectItemType.worldTemplateManifestJson) {
                await projectItem.ensureStorage();
                const folder = projectItem.getFolder();
                if (folder) {
                    worldFolders.push(folder);
                }
            }
        }
        const results = [];
        for (const folder of worldFolders) {
            Log_1.default.assert(folder !== null, "Manifest file cannot exist without a folder");
            const resultForFolder = await this.validateWorldIconForFolder(folder, isEDUOffer);
            results.push(...resultForFolder);
        }
        return results;
    }
    async validateWorldIconForFolder(folder, isEDUOffer) {
        const files = await (0, AsyncUtilities_1.filterAsync)(folder.allFiles, ImageUtilites_1.isWorldIcon);
        if (files.length === 0) {
            const message = `No World Icon found for ${folder.getFolderRelativePath}.`;
            return [this.createResult(CheckWorldIconsGeneratorTest.NoIconFound, message)];
        }
        else if (files.length > 1) {
            const message = `Too many World Icons found for ${folder.getFolderRelativePath}. ${files.length} icons found, expected: 1`;
            return [this.createResult(CheckWorldIconsGeneratorTest.MultipleIconsFound, message)];
        }
        const file = files[0];
        const image = await (0, ImageUtilites_1.parseImageMetadata)(file);
        if (!image) {
            const message = `Image (${file.name}) is not valid.`;
            return [this.createResult(CheckWorldIconsGeneratorTest.IconNotValidImage, message)];
        }
        const isSizeCorrect = isEDUOffer ? isSizeCorrectForEDU : isSizeCorrectForBedrock;
        if (!isSizeCorrect({ width: image.ImageWidth, height: image.ImageHeight })) {
            return [this.createResult(CheckWorldIconsGeneratorTest.IconNotValidSize, `Image (${file.name}) is not valid.`)];
        }
        return [];
    }
    createResult(test, message) {
        return new ProjectInfoItem_1.default(this.severity, this.id, test, message);
    }
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(CheckWorldIconsGeneratorTest, topicId),
        };
    }
    summarize() { }
}
exports.default = CheckWorldIconsGenerator;
function isSizeCorrectForBedrock(size) {
    return size.width === DefaultBedrockImageWidth && size.height === DefaultBedrockImageHeight;
}
function isSizeCorrectForEDU(size) {
    const isEDUSize = size.width === DefaultEduImageWidth && size.height === DefaultEduImageHeight;
    return isEDUSize || isSizeCorrectForBedrock(size);
}

//# sourceMappingURL=../../maps/info/projectGenerators/CheckWorldIconsGenerator.js.map
