"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ProjectInfoItem_1 = require("../ProjectInfoItem");
const Pack_1 = require("../../minecraft/Pack");
const IInfoItemData_1 = require("../IInfoItemData");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const ImageUtilites_1 = require("../../storage/ImageUtilites");
var CheckPacksIconsGeneratorTest;
(function (CheckPacksIconsGeneratorTest) {
    CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["NoIconFound"] = 101] = "NoIconFound";
    CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["MultipleIconsFound"] = 102] = "MultipleIconsFound";
    CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["IconNotValidImage"] = 103] = "IconNotValidImage";
    CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["IconNotValidSize"] = 104] = "IconNotValidSize";
})(CheckPacksIconsGeneratorTest || (CheckPacksIconsGeneratorTest = {}));
const IconMaxWidth = 256;
const IconMinWidth = 2;
/***********
 * Generator for validating Pack Icons
 *
 * Will ensure:
 *  * exactly 1 icon per pack
 *  * icon is a valid .png
 *  * icon is square
 *  * icon is within size limits
 *
 */
class CheckPackIconsGenerator {
    id = "CPACKICON";
    title = "Check Pack Icon Generator";
    canAlwaysProcess = true;
    severity = IInfoItemData_1.InfoItemType.error;
    async generate(project) {
        const results = [];
        for (const pack of project.packs) {
            if (requiresPackIcon(pack)) {
                const result = await this.getResultForPack(pack);
                if (result) {
                    results.push(result);
                }
            }
        }
        return results;
    }
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(CheckPacksIconsGeneratorTest, topicId),
        };
    }
    summarize() { }
    async getResultForPack(pack) {
        const icons = await pack.getFiles(ImageUtilites_1.isPackIcon);
        if (icons.length === 0) {
            const message = `pack_icon image file not found for ${pack.name}. It must use the .png extension.`;
            return this.createResult(CheckPacksIconsGeneratorTest.NoIconFound, message);
        }
        else if (icons.length > 1) {
            const message = `Found multiple pack icon files for ${pack.name}, there should be only one.`;
            return this.createResult(CheckPacksIconsGeneratorTest.MultipleIconsFound, message);
        }
        const file = icons[0];
        const image = await (0, ImageUtilites_1.parseImageMetadata)(file);
        if (!image?.ImageWidth || !image?.ImageHeight) {
            const message = `Pack Image (${file.name}) is not valid`;
            return this.createResult(CheckPacksIconsGeneratorTest.IconNotValidImage, message);
        }
        if (this.isValidSize(image.ImageWidth, image.ImageHeight)) {
            const message = `pack_icon must be square with size 2, 4, 8, 16, 32, 64, 128, or 256. Found [${image.ImageWidth} x ${image.ImageHeight}] on ${file.name}`;
            return this.createResult(CheckPacksIconsGeneratorTest.IconNotValidSize, message);
        }
        return null;
    }
    createResult(test, message) {
        return new ProjectInfoItem_1.default(this.severity, this.id, test, message);
    }
    isValidSize(width, height) {
        return width !== height || width < IconMinWidth || width > IconMaxWidth || (width & (width - 1)) !== 0;
    }
}
exports.default = CheckPackIconsGenerator;
function requiresPackIcon(pack) {
    return pack.packType !== Pack_1.PackType.skin && pack.packType !== Pack_1.PackType.design;
}

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