"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AttachableResourceDefinition_1 = require("../minecraft/AttachableResourceDefinition");
const EntityTypeDefinition_1 = require("../minecraft/EntityTypeDefinition");
const EntityTypeResourceDefinition_1 = require("../minecraft/EntityTypeResourceDefinition");
const ItemTextureCatalogDefinition_1 = require("../minecraft/ItemTextureCatalogDefinition");
const MusicDefinitionCatalogDefinition_1 = require("../minecraft/MusicDefinitionCatalogDefinition");
const ParticleEffectResourceDefinition_1 = require("../minecraft/ParticleEffectResourceDefinition");
const SoundCatalogDefinition_1 = require("../minecraft/SoundCatalogDefinition");
const SoundDefinitionCatalogDefinition_1 = require("../minecraft/SoundDefinitionCatalogDefinition");
const TerrainTextureCatalogDefinition_1 = require("../minecraft/TerrainTextureCatalogDefinition");
const IProjectItemData_1 = require("./IProjectItemData");
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) {
            if (item.itemType === IProjectItemData_1.ProjectItemType.entityTypeBehavior) {
                await item.ensureStorage();
                if (item.file) {
                    const entityTypeBehavior = await EntityTypeDefinition_1.default.ensureOnFile(item.file);
                    if (entityTypeBehavior) {
                        await entityTypeBehavior.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.entityTypeResource) {
                await item.ensureStorage();
                if (item.file) {
                    const entityTypeResource = await EntityTypeResourceDefinition_1.default.ensureOnFile(item.file);
                    if (entityTypeResource) {
                        await entityTypeResource.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.particleJson) {
                await item.ensureStorage();
                if (item.file) {
                    const particleResource = await ParticleEffectResourceDefinition_1.default.ensureOnFile(item.file);
                    if (particleResource) {
                        await particleResource.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.attachableResourceJson) {
                await item.ensureStorage();
                if (item.file) {
                    const attachableResource = await AttachableResourceDefinition_1.default.ensureOnFile(item.file);
                    if (attachableResource) {
                        await attachableResource.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.itemTextureJson) {
                await item.ensureStorage();
                if (item.file) {
                    const itemTextures = await ItemTextureCatalogDefinition_1.default.ensureOnFile(item.file);
                    if (itemTextures) {
                        await itemTextures.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.terrainTextureCatalogResourceJson) {
                await item.ensureStorage();
                if (item.file) {
                    const terrainTexture = await TerrainTextureCatalogDefinition_1.default.ensureOnFile(item.file);
                    if (terrainTexture) {
                        await terrainTexture.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.soundDefinitionCatalog) {
                await item.ensureStorage();
                if (item.file) {
                    const soundDefCat = await SoundDefinitionCatalogDefinition_1.default.ensureOnFile(item.file);
                    if (soundDefCat) {
                        await soundDefCat.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.musicDefinitionJson) {
                await item.ensureStorage();
                if (item.file) {
                    const musicDefCat = await MusicDefinitionCatalogDefinition_1.default.ensureOnFile(item.file);
                    if (musicDefCat) {
                        await musicDefCat.addChildItems(project, item);
                    }
                }
            }
            else if (item.itemType === IProjectItemData_1.ProjectItemType.soundCatalog) {
                await item.ensureStorage();
                if (item.file) {
                    const soundCat = await SoundCatalogDefinition_1.default.ensureOnFile(item.file);
                    if (soundCat) {
                        await soundCat.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.file) {
                        const entityTypeResource = await EntityTypeResourceDefinition_1.default.ensureOnFile(rel.parentItem.file);
                        if (entityTypeResource) {
                            await entityTypeResource.deleteLinkToChild(rel);
                        }
                    }
                }
                else if (rel.parentItem.itemType === IProjectItemData_1.ProjectItemType.particleJson) {
                    await item.ensureStorage();
                    if (rel.parentItem.file) {
                        const particleResource = await ParticleEffectResourceDefinition_1.default.ensureOnFile(rel.parentItem.file);
                        if (particleResource) {
                            await particleResource.deleteLinkToChild(rel);
                        }
                    }
                }
                else if (rel.parentItem.itemType === IProjectItemData_1.ProjectItemType.attachableResourceJson) {
                    await item.ensureStorage();
                    if (rel.parentItem.file) {
                        const attachableResource = await AttachableResourceDefinition_1.default.ensureOnFile(rel.parentItem.file);
                        if (attachableResource) {
                            await attachableResource.deleteLinkToChild(rel);
                        }
                    }
                }
                else if (rel.parentItem.itemType === IProjectItemData_1.ProjectItemType.soundCatalog) {
                    await item.ensureStorage();
                    if (rel.parentItem.file) {
                        const soundCat = await SoundDefinitionCatalogDefinition_1.default.ensureOnFile(rel.parentItem.file);
                        if (soundCat) {
                            await soundCat.deleteLinkToChild(rel.childItem);
                        }
                    }
                }
            }
        }
        await this.calculate(item.project);
    }
}
exports.default = ProjectItemRelations;

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