"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckVanillaDuplicatesInfoGeneratorTest = void 0;
const ProjectInfoItem_1 = require("../ProjectInfoItem");
const IInfoItemData_1 = require("../IInfoItemData");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const Database_1 = require("../../minecraft/Database");
var CheckVanillaDuplicatesInfoGeneratorTest;
(function (CheckVanillaDuplicatesInfoGeneratorTest) {
    CheckVanillaDuplicatesInfoGeneratorTest[CheckVanillaDuplicatesInfoGeneratorTest["completeVanillaCopy"] = 101] = "completeVanillaCopy";
    CheckVanillaDuplicatesInfoGeneratorTest[CheckVanillaDuplicatesInfoGeneratorTest["partialVanillaCopy"] = 102] = "partialVanillaCopy";
})(CheckVanillaDuplicatesInfoGeneratorTest = exports.CheckVanillaDuplicatesInfoGeneratorTest || (exports.CheckVanillaDuplicatesInfoGeneratorTest = {}));
// Files that are allowed to be complete copies of vanilla
const ALLOWLISTED_FILES = ["texts/languages.json"];
// Files that are restricted from being partial copies
const RESTRICTED_FILES = [
    "/ui/",
    "/materials/",
    "models/mobs.json",
    "sounds/sound_definitions.json",
    "textures/item_texture.json",
    "textures/terrain_texture.json",
    "blocks.json",
];
// Extensions to check for partial copies
const EXTENSIONS = [".json", ".material"];
// Message suffix matching C# implementation
const RESOURCE_PACK_SUFFIX = "Vanilla files can be seen at https://aka.ms/resourcepacktemplate.";
/***********
 * Generator for Checking Vanilla Duplicates
 *
 * Will check:
 *  * Complete copies of vanilla files
 *  * Partial copies of vanilla files (for restricted files)
 *
 */
class CheckVanillaDuplicatesInfoGenerator {
    id = "VANDUPES";
    title = "Check Vanilla Duplicates";
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(CheckVanillaDuplicatesInfoGeneratorTest, topicId),
        };
    }
    summarize(info, infoSet) {
        info.completeVanillaCopy = infoSet.getSummedDataValue(this.id, CheckVanillaDuplicatesInfoGeneratorTest.completeVanillaCopy);
        info.partialVanillaCopy = infoSet.getSummedDataValue(this.id, CheckVanillaDuplicatesInfoGeneratorTest.partialVanillaCopy);
    }
    async generate(project, contentIndex) {
        const items = [];
        // Get the hash catalog from the project's info set
        const hashCatalog = contentIndex.hashCatalog;
        // Load vanilla hash catalog for comparison
        await Database_1.default.loadReleaseVanillaInfoHashes();
        const projItems = project.getItemsCopy();
        for (const item of projItems) {
            await item.ensureStorage();
            if (!item.primaryFile) {
                continue;
            }
            const itemPath = item.getFile()?.fullPath || "";
            if (ALLOWLISTED_FILES.some((allowedFile) => itemPath.includes(allowedFile))) {
                continue;
            }
            // Find hashes for this file in the catalog
            const fileHashes = Object.entries(hashCatalog).filter(([hash, details]) => details.filePath === item.projectPath);
            // Check for complete copy of vanilla file (file with empty propertyName)
            const completeFileHash = fileHashes.find(([hash, details]) => details.propertyName === "");
            if (completeFileHash) {
                const completeHashResult = await this.checkCompleteVanillaCopy(completeFileHash[0], item);
                if (completeHashResult) {
                    items.push(completeHashResult);
                    continue;
                }
            }
            // Check for partial copies (properties with non-empty propertyName)
            if (this.isRestrictedFile(itemPath) && this.hasRestrictedExtension(item.primaryFile.fullPath)) {
                const propertyHashes = fileHashes.filter(([hash, details]) => details.propertyName !== "");
                const partialHashResults = await this.checkPartialVanillaCopy(propertyHashes, item);
                items.push(...partialHashResults);
            }
        }
        return items;
    }
    isRestrictedFile(filePath) {
        return RESTRICTED_FILES.some((restrictedFile) => filePath.includes(restrictedFile));
    }
    hasRestrictedExtension(filePath) {
        // Extract file extension from path
        const extension = "." + filePath.split(".").pop()?.toLowerCase() || "";
        return EXTENSIONS.includes(extension);
    }
    async checkCompleteVanillaCopy(fileHash, item) {
        // Check if this hash exists in the vanilla hash catalog
        if (!Database_1.default.releaseVanillaContentHashes) {
            return null;
        }
        const vanillaDetails = Database_1.default.releaseVanillaContentHashes[fileHash];
        if (vanillaDetails && vanillaDetails.propertyName === "") {
            // This is a complete file match
            return new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.warning, this.id, CheckVanillaDuplicatesInfoGeneratorTest.completeVanillaCopy, `Complete copy of a vanilla file [${vanillaDetails.fileName}]. ${RESOURCE_PACK_SUFFIX}`, item, item.name);
        }
        return null;
    }
    async checkPartialVanillaCopy(propertyHashes, item) {
        const results = [];
        if (!Database_1.default.releaseVanillaContentHashes) {
            return results;
        }
        try {
            // Check each property hash from the catalog
            for (const [hash, details] of propertyHashes) {
                // Check if this property hash exists in the vanilla hash catalog
                const vanillaDetails = Database_1.default.releaseVanillaContentHashes[hash];
                if (vanillaDetails &&
                    vanillaDetails.propertyName !== "" &&
                    item.name.toLowerCase() === vanillaDetails.fileName.toLowerCase()) {
                    results.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.warning, this.id, CheckVanillaDuplicatesInfoGeneratorTest.partialVanillaCopy, `Partial copy of a vanilla file [${item.name}] at property [${details.propertyName}]. ${RESOURCE_PACK_SUFFIX}`, item, details.propertyName));
                }
            }
        }
        catch (error) {
            console.error("Error checking partial vanilla copy:", error);
        }
        return results;
    }
}
exports.default = CheckVanillaDuplicatesInfoGenerator;

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