"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AttachableResourceDefinition_1 = require("../minecraft/AttachableResourceDefinition");
const BlockTypeDefinition_1 = require("../minecraft/BlockTypeDefinition");
const EntityTypeDefinition_1 = require("../minecraft/EntityTypeDefinition");
const EntityTypeResourceDefinition_1 = require("../minecraft/EntityTypeResourceDefinition");
const ItemTextureCatalogDefinition_1 = require("../minecraft/ItemTextureCatalogDefinition");
const ItemTypeDefinition_1 = require("../minecraft/ItemTypeDefinition");
const JsonUIResourceDefinition_1 = require("../minecraft/JsonUIResourceDefinition");
const LootTableBehaviorDefinition_1 = require("../minecraft/LootTableBehaviorDefinition");
const MusicDefinitionCatalogDefinition_1 = require("../minecraft/MusicDefinitionCatalogDefinition");
const ParticleEffectResourceDefinition_1 = require("../minecraft/ParticleEffectResourceDefinition");
const RecipeBehaviorDefinition_1 = require("../minecraft/RecipeBehaviorDefinition");
const SkinCatalogDefinition_1 = require("../minecraft/SkinCatalogDefinition");
const SoundCatalogDefinition_1 = require("../minecraft/SoundCatalogDefinition");
const SoundDefinitionCatalogDefinition_1 = require("../minecraft/SoundDefinitionCatalogDefinition");
const SpawnRulesBehaviorDefinition_1 = require("../minecraft/SpawnRulesBehaviorDefinition");
const TerrainTextureCatalogDefinition_1 = require("../minecraft/TerrainTextureCatalogDefinition");
const TextureDefinition_1 = require("../minecraft/TextureDefinition");
const IProjectItemData_1 = require("./IProjectItemData");
const ITEM_TYPE_CONFIG = new Map([
    [IProjectItemData_1.ProjectItemType.entityTypeBehavior, EntityTypeDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.itemTypeBehavior, ItemTypeDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.blockTypeBehavior, BlockTypeDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.entityTypeResource, EntityTypeResourceDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.skinCatalogJson, SkinCatalogDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.lootTableBehavior, LootTableBehaviorDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.particleJson, ParticleEffectResourceDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.uiJson, JsonUIResourceDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.attachableResourceJson, AttachableResourceDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.itemTextureJson, ItemTextureCatalogDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.terrainTextureCatalogResourceJson, TerrainTextureCatalogDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.soundDefinitionCatalog, SoundDefinitionCatalogDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.musicDefinitionJson, MusicDefinitionCatalogDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.soundCatalog, SoundCatalogDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.recipeBehavior, RecipeBehaviorDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.spawnRuleBehavior, SpawnRulesBehaviorDefinition_1.default],
    [IProjectItemData_1.ProjectItemType.texture, TextureDefinition_1.default],
]);
class ProjectItemRelations {
    static async calculate(project) {
        const items = project.getItemsCopy();
        // clear all existing relations
        for (const item of items) {
            item.childItems = undefined;
            item.parentItems = undefined;
        }
        for (const item of items) {
            const handlerClass = ITEM_TYPE_CONFIG.get(item.itemType);
            if (handlerClass) {
                await item.ensureStorage();
                if (item.primaryFile) {
                    const handler = await handlerClass.ensureOnFile(item.primaryFile);
                    if (handler) {
                        await handler.addChildItems(project, item);
                    }
                }
            }
        }
    }
    static async deleteLinksFromParents(item) {
        if (!item.parentItems || item.parentItems.length === 0) {
            return;
        }
        for (const rel of item.parentItems) {
            if (rel.parentItem && rel.childItem) {
                if (rel.parentItem.itemType === IProjectItemData_1.ProjectItemType.entityTypeResource) {
                    await item.ensureStorage();
                    if (rel.parentItem.primaryFile) {
                        const entityTypeResource = await EntityTypeResourceDefinition_1.default.ensureOnFile(rel.parentItem.primaryFile);
                        if (entityTypeResource) {
                            await entityTypeResource.deleteLinkToChild(rel);
                        }
                    }
                }
                else if (rel.parentItem.itemType === IProjectItemData_1.ProjectItemType.particleJson) {
                    await item.ensureStorage();
                    if (rel.parentItem.primaryFile) {
                        const particleResource = await ParticleEffectResourceDefinition_1.default.ensureOnFile(rel.parentItem.primaryFile);
                        if (particleResource) {
                            await particleResource.deleteLinkToChild(rel);
                        }
                    }
                }
                else if (rel.parentItem.itemType === IProjectItemData_1.ProjectItemType.attachableResourceJson) {
                    await item.ensureStorage();
                    if (rel.parentItem.primaryFile) {
                        const attachableResource = await AttachableResourceDefinition_1.default.ensureOnFile(rel.parentItem.primaryFile);
                        if (attachableResource) {
                            await attachableResource.deleteLinkToChild(rel);
                        }
                    }
                }
                else if (rel.parentItem.itemType === IProjectItemData_1.ProjectItemType.soundCatalog) {
                    await item.ensureStorage();
                    if (rel.parentItem.primaryFile) {
                        const soundCat = await SoundDefinitionCatalogDefinition_1.default.ensureOnFile(rel.parentItem.primaryFile);
                        if (soundCat) {
                            await soundCat.deleteLinkToChild(rel.childItem);
                        }
                    }
                }
            }
        }
        await this.calculate(item.project);
    }
}
exports.default = ProjectItemRelations;

//# sourceMappingURL=../maps/app/ProjectItemRelations.js.map
