"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckWorldPackReferencesGeneratorTest = void 0;
const ProjectInfoItem_1 = require("../ProjectInfoItem");
const IInfoItemData_1 = require("../IInfoItemData");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const IProjectItemData_1 = require("../../app/IProjectItemData");
const StorageUtilities_1 = require("../../storage/StorageUtilities");
const Utilities_1 = require("../../core/Utilities");
const ResourceManifestDefinition_1 = require("../../minecraft/ResourceManifestDefinition");
const BehaviorManifestDefinition_1 = require("../../minecraft/BehaviorManifestDefinition");
var CheckWorldPackReferencesGeneratorTest;
(function (CheckWorldPackReferencesGeneratorTest) {
    CheckWorldPackReferencesGeneratorTest[CheckWorldPackReferencesGeneratorTest["invalidWorldPackReferencesJson"] = 201] = "invalidWorldPackReferencesJson";
    CheckWorldPackReferencesGeneratorTest[CheckWorldPackReferencesGeneratorTest["missingWorldPackReferencesFile"] = 202] = "missingWorldPackReferencesFile";
    CheckWorldPackReferencesGeneratorTest[CheckWorldPackReferencesGeneratorTest["invalidPackId"] = 203] = "invalidPackId";
    CheckWorldPackReferencesGeneratorTest[CheckWorldPackReferencesGeneratorTest["missingManifestVersion"] = 204] = "missingManifestVersion";
    CheckWorldPackReferencesGeneratorTest[CheckWorldPackReferencesGeneratorTest["invalidManifestVersion"] = 205] = "invalidManifestVersion";
    CheckWorldPackReferencesGeneratorTest[CheckWorldPackReferencesGeneratorTest["packReferenceNotFound"] = 206] = "packReferenceNotFound";
    CheckWorldPackReferencesGeneratorTest[CheckWorldPackReferencesGeneratorTest["internalProcessingError"] = 207] = "internalProcessingError";
})(CheckWorldPackReferencesGeneratorTest = exports.CheckWorldPackReferencesGeneratorTest || (exports.CheckWorldPackReferencesGeneratorTest = {}));
class CheckWorldPackReferencesGenerator {
    id = "WPACKREFS";
    title = "World Pack References";
    canAlwaysProcess = true;
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(CheckWorldPackReferencesGeneratorTest, topicId),
        };
    }
    summarize(info, infoSet) {
        info.invalidWorldPackReferencesJson = infoSet.getSummedDataValue(this.id, CheckWorldPackReferencesGeneratorTest.invalidWorldPackReferencesJson);
        info.missingWorldPackReferencesFile = infoSet.getSummedDataValue(this.id, CheckWorldPackReferencesGeneratorTest.missingWorldPackReferencesFile);
        info.invalidPackIds = infoSet.getSummedDataValue(this.id, CheckWorldPackReferencesGeneratorTest.invalidPackId);
        info.missingManifestVersion = infoSet.getSummedDataValue(this.id, CheckWorldPackReferencesGeneratorTest.missingManifestVersion);
        info.invalidManifestVersion = infoSet.getSummedDataValue(this.id, CheckWorldPackReferencesGeneratorTest.invalidManifestVersion);
        info.packReferencesNotFound = infoSet.getSummedDataValue(this.id, CheckWorldPackReferencesGeneratorTest.packReferenceNotFound);
        info.internalProcessingErrors = infoSet.getSummedDataValue(this.id, CheckWorldPackReferencesGeneratorTest.internalProcessingError);
    }
    async generate(project) {
        const items = [];
        const projItems = project.getItemsCopy();
        const availablePacks = [];
        try {
            // First, collect all available pack UUIDs from manifests
            await this.collectAvailablePacks(projItems, availablePacks, items);
            // Check for pack reference files
            for (const item of projItems) {
                if (item.itemType === IProjectItemData_1.ProjectItemType.resourcePackListJson) {
                    await this.checkPackReferences(item, availablePacks, items);
                }
                else if (item.itemType === IProjectItemData_1.ProjectItemType.behaviorPackListJson) {
                    await this.checkPackReferences(item, availablePacks, items);
                }
            }
        }
        catch (error) {
            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.internalProcessingError, `Internal processing error: ${error}`, undefined));
        }
        return items;
    }
    async collectAvailablePacks(projItems, availablePacks, items) {
        for (const item of projItems) {
            if (item.itemType !== IProjectItemData_1.ProjectItemType.resourcePackManifestJson &&
                item.itemType !== IProjectItemData_1.ProjectItemType.behaviorPackManifestJson) {
                continue;
            }
            if (!item.isContentLoaded) {
                await item.loadContent();
            }
            if (!item.primaryFile) {
                continue;
            }
            if (!item.primaryFile.isContentLoaded) {
                await item.primaryFile.loadContent();
            }
            try {
                if (item.itemType === IProjectItemData_1.ProjectItemType.resourcePackManifestJson) {
                    const resourceManifest = await ResourceManifestDefinition_1.default.ensureOnFile(item.primaryFile);
                    if (resourceManifest && resourceManifest.id && Utilities_1.default.isValidUuid(resourceManifest.id)) {
                        availablePacks.push({
                            uuid: resourceManifest.id,
                            manifestItem: item,
                        });
                    }
                }
                else if (item.itemType === IProjectItemData_1.ProjectItemType.behaviorPackManifestJson) {
                    const behaviorManifest = await BehaviorManifestDefinition_1.default.ensureOnFile(item.primaryFile);
                    if (behaviorManifest && behaviorManifest.id && Utilities_1.default.isValidUuid(behaviorManifest.id)) {
                        availablePacks.push({
                            uuid: behaviorManifest.id,
                            manifestItem: item,
                        });
                    }
                }
            }
            catch (error) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.internalProcessingError, `Error processing manifest ${item.name}: ${error}`, item));
            }
        }
    }
    async checkPackReferences(packReferencesFile, availablePacks, items) {
        if (!packReferencesFile.isContentLoaded) {
            await packReferencesFile.loadContent();
        }
        if (!packReferencesFile.primaryFile) {
            return;
        }
        if (!packReferencesFile.primaryFile.isContentLoaded) {
            await packReferencesFile.primaryFile.loadContent();
        }
        const parsedContent = StorageUtilities_1.default.getJsonObject(packReferencesFile.primaryFile);
        if (!parsedContent || !Array.isArray(parsedContent)) {
            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.invalidWorldPackReferencesJson, `Invalid JSON format in ${packReferencesFile.name}. Expected an array of pack references.`, packReferencesFile));
            return;
        }
        const packRefsFoundInJson = [];
        // Validate each pack reference object
        for (let i = 0; i < parsedContent.length; i++) {
            const packRef = parsedContent[i];
            if (typeof packRef !== "object" || packRef === null) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.invalidWorldPackReferencesJson, `Invalid pack reference object at index ${i} in ${packReferencesFile.name}`, packReferencesFile));
                continue;
            }
            // Validate pack_id
            if (!packRef.pack_id || typeof packRef.pack_id !== "string") {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.invalidPackId, `Missing or invalid pack_id at index ${i} in ${packReferencesFile.name}`, packReferencesFile));
                continue;
            }
            if (!Utilities_1.default.isValidUuid(packRef.pack_id)) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.invalidPackId, `Invalid UUID format for pack_id [${packRef.pack_id}] at index ${i} in ${packReferencesFile.name}`, packReferencesFile));
                continue;
            }
            // Valid pack_id found, add to list
            packRefsFoundInJson.push(packRef.pack_id);
            // Validate version
            const versionValidation = ResourceManifestDefinition_1.default.validatePackReferenceVersion(packRef.version);
            if (!versionValidation.isValid) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.invalidManifestVersion, `${versionValidation.errorMessage} for pack_id [${packRef.pack_id}] at index ${i} in ${packReferencesFile.name}`, packReferencesFile));
                continue;
            }
        }
        // Check if each referenced pack exists in the available packs
        for (const packRef of packRefsFoundInJson) {
            const foundPack = availablePacks.find((pack) => pack.uuid === packRef);
            if (!foundPack) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckWorldPackReferencesGeneratorTest.packReferenceNotFound, `Pack reference [${packRef}] not found in project.`, packReferencesFile));
            }
        }
    }
}
exports.default = CheckWorldPackReferencesGenerator;

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