"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ProjectInfoItem_1 = require("./ProjectInfoItem");
const IProjectItemData_1 = require("../app/IProjectItemData");
const IInfoItemData_1 = require("./IInfoItemData");
const Database_1 = require("../minecraft/Database");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const TextureInfoGenerator_1 = require("./TextureInfoGenerator");
const exifr_1 = require("exifr");
class TextureImageInfoGenerator {
    constructor() {
        this.id = "TEXTUREIMAGE";
        this.title = "Texture Image Validation";
        this.performAddOnValidations = false;
    }
    getTopicData(topicId) {
        switch (topicId) {
            case 1:
                return { title: "Texture Images" };
            case 401:
                return { title: "Image Processing Error" };
            default:
                return { title: topicId.toString() };
        }
    }
    summarize(info, infoSet) {
        info.textureCount = infoSet.getSummedNumberValue(this.id, 1);
    }
    async generate(project, contentIndex) {
        const items = [];
        const textureImagePi = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 1, "Texture Images");
        items.push(textureImagePi);
        const rpFolder = await Database_1.default.loadDefaultResourcePack();
        const itemsCopy = project.getItemsCopy();
        for (const projectItem of itemsCopy) {
            if (projectItem.itemType === IProjectItemData_1.ProjectItemType.texture) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    let pathInRp = projectItem.getPackRelativePath();
                    let isVanilla = false;
                    // Log.assertDefined(pathInRp, "TIIGGP");
                    if (pathInRp) {
                        pathInRp = StorageUtilities_1.default.getBaseFromName(StorageUtilities_1.default.ensureNotStartsWithDelimiter(pathInRp));
                        if (await TextureInfoGenerator_1.default.matchesVanillaPath(pathInRp, rpFolder)) {
                            textureImagePi.incrementFeature("Vanilla Override Texture");
                            isVanilla = true;
                        }
                        else {
                            textureImagePi.incrementFeature("Custom Texture");
                        }
                    }
                    else {
                        textureImagePi.incrementFeature("Custom Texture");
                    }
                    await projectItem.file.loadContent();
                    if (projectItem.file.content &&
                        projectItem.file.content instanceof Uint8Array &&
                        projectItem.file.type !== "tga") {
                        const exifr = new exifr_1.Exifr({});
                        try {
                            await exifr.read(projectItem.file.content);
                            const results = await exifr.parse();
                            if (results.ImageWidth && results.ImageHeight) {
                                textureImagePi.spectrumIntFeature("ImageWidth", results.ImageWidth);
                                textureImagePi.spectrumIntFeature("ImageHeight", results.ImageHeight);
                                textureImagePi.spectrumIntFeature("ImageSize", results.ImageWidth * results.ImageHeight);
                                if (!isVanilla) {
                                    textureImagePi.spectrumIntFeature("NonVanillaImageSize", results.ImageWidth * results.ImageHeight);
                                }
                            }
                        }
                        catch (e) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 401, `Error processing image`, projectItem, e.toString()));
                        }
                    }
                }
            }
        }
        return items;
    }
}
exports.default = TextureImageInfoGenerator;

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