"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Log_1 = require("../core/Log");
const ste_events_1 = require("ste-events");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const Project_1 = require("../app/Project");
const IProjectItemData_1 = require("../app/IProjectItemData");
const SoundDefinitionCatalogDefinition_1 = require("./SoundDefinitionCatalogDefinition");
const Database_1 = require("./Database");
const Utilities_1 = require("../core/Utilities");
const MinecraftDefinitions_1 = require("./MinecraftDefinitions");
class SoundCatalogDefinition {
    _data;
    _file;
    _isLoaded = false;
    _onLoaded = new ste_events_1.EventDispatcher();
    id;
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get data() {
        return this._data;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get entityIdList() {
        if (!this._data) {
            return undefined;
        }
        const entityIdList = [];
        if (this._data.entity_sounds && this._data.entity_sounds.entities) {
            for (const key in this._data.entity_sounds.entities) {
                if (!entityIdList.includes(key)) {
                    entityIdList.push(key);
                }
            }
        }
        return entityIdList;
    }
    getSoundEventNameList() {
        if (!this._data) {
            return undefined;
        }
        const soundEventNameList = [];
        if (this._data.entity_sounds && this._data.entity_sounds.entities) {
            for (const key in this._data.entity_sounds.entities) {
                const def = this._data.entity_sounds.entities[key];
                if (def && def.events) {
                    for (const eventKey in def.events) {
                        const event = def.events[eventKey];
                        if (event) {
                            for (const eventInstanceKey in def.events) {
                                const eventInstance = def.events[eventInstanceKey];
                                if (typeof eventInstance === "string") {
                                    if (!soundEventNameList.includes(eventInstance)) {
                                        soundEventNameList.push(eventInstance);
                                    }
                                }
                                else if (eventInstance.sound) {
                                    if (!soundEventNameList.includes(eventInstance.sound)) {
                                        soundEventNameList.push(eventInstance.sound);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (this._data.entity_sounds && this._data.entity_sounds.defaults && this._data.entity_sounds.defaults.events) {
            for (const eventKey in this._data.entity_sounds.defaults.events) {
                const eventInstance = this._data.entity_sounds.defaults.events[eventKey];
                if (typeof eventInstance === "string") {
                    if (!soundEventNameList.includes(eventInstance)) {
                        soundEventNameList.push(eventInstance);
                    }
                }
                else if (eventInstance.sound) {
                    if (!soundEventNameList.includes(eventInstance.sound)) {
                        soundEventNameList.push(eventInstance.sound);
                    }
                }
            }
        }
        if (this._data.block_sounds) {
            for (const key in this._data.block_sounds) {
                const def = this._data.block_sounds[key];
                if (def && def.events) {
                    for (const eventKey in def.events) {
                        const event = def.events[eventKey];
                        if (event) {
                            for (const eventInstanceKey in def.events) {
                                const eventInstance = def.events[eventInstanceKey];
                                if (typeof eventInstance === "string") {
                                    if (!soundEventNameList.includes(eventInstance)) {
                                        soundEventNameList.push(eventInstance);
                                    }
                                }
                                else if (eventInstance.sound) {
                                    if (!soundEventNameList.includes(eventInstance.sound)) {
                                        soundEventNameList.push(eventInstance.sound);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (this._data.individual_event_sounds && this._data.individual_event_sounds.events) {
            for (const key in this._data.individual_event_sounds.events) {
                const eventInstance = this._data.individual_event_sounds.events[key];
                if (typeof eventInstance === "string") {
                    if (!soundEventNameList.includes(eventInstance)) {
                        soundEventNameList.push(eventInstance);
                    }
                }
                else if (eventInstance.sound) {
                    if (!soundEventNameList.includes(eventInstance.sound)) {
                        soundEventNameList.push(eventInstance.sound);
                    }
                }
            }
        }
        return soundEventNameList;
    }
    ensureEntityEvent(idSound) {
        this.ensureDefault();
        if (!this._data) {
            return;
        }
        let es = this._data.entity_sounds;
        if (es === undefined) {
            es = {
                entities: {},
            };
            this._data.entity_sounds = es;
        }
        let entities = es.entities;
        if (entities === undefined) {
            entities = {};
            es.entities = entities;
        }
        let elt = entities[idSound];
        if (!elt) {
            if (idSound.startsWith("minecraft:") && Utilities_1.default.isUsableAsObjectKey(idSound.substring(10))) {
                elt = entities[idSound.substring(10)];
            }
            if (!elt) {
                elt = {
                    events: {},
                };
            }
            if (Utilities_1.default.isUsableAsObjectKey(idSound)) {
                entities[idSound] = elt;
            }
            return elt;
        }
        return elt;
    }
    ensureDefault() {
        if (this._data === undefined) {
            this._data = {};
        }
    }
    static async ensureForProject(project) {
        const items = project.getItemsCopy();
        for (const item of items) {
            if (item.itemType === IProjectItemData_1.ProjectItemType.soundCatalog) {
                await item.ensureFileStorage();
                if (item.primaryFile) {
                    const soundCatalog = await SoundCatalogDefinition.ensureOnFile(item.primaryFile);
                    if (soundCatalog) {
                        return soundCatalog;
                    }
                }
            }
        }
        const defaultRpFolder = await project.getDefaultResourcePackFolder();
        if (defaultRpFolder) {
            const newFile = defaultRpFolder.ensureFile("sounds.json");
            const soundGen = await SoundCatalogDefinition.ensureOnFile(newFile);
            if (soundGen) {
                soundGen.ensureDefault();
                project.ensureItemFromFile(newFile, IProjectItemData_1.ProjectItemType.soundCatalog, Project_1.FolderContext.resourcePack);
                return soundGen;
            }
        }
        return undefined;
    }
    static async ensureOnFile(file, loadHandler) {
        let et;
        if (file.manager === undefined) {
            et = new SoundCatalogDefinition();
            et.file = file;
            file.manager = et;
        }
        if (file.manager !== undefined && file.manager instanceof SoundCatalogDefinition) {
            et = file.manager;
            if (!et.isLoaded && loadHandler) {
                et.onLoaded.subscribe(loadHandler);
            }
            await et.load();
        }
        return et;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const defString = JSON.stringify(this._data, null, 2);
        this._file.setContent(defString);
    }
    async load() {
        if (this._isLoaded) {
            return;
        }
        if (this._file === undefined) {
            Log_1.default.unexpectedUndefined("TTCDF");
            return;
        }
        await this._file.loadContent();
        if (!this._file.content || this._file.content instanceof Uint8Array) {
            return;
        }
        let data = {};
        let result = StorageUtilities_1.default.getJsonObject(this._file);
        if (result) {
            data = result;
        }
        this._data = data;
        this._isLoaded = true;
        this._onLoaded.dispatch(this, this);
    }
    async addChildItems(project, item) {
        const itemsCopy = project.getItemsCopy();
        let soundEventList = this.getSoundEventNameList();
        let entityIdList = this.entityIdList;
        for (const candItem of itemsCopy) {
            if ((candItem.itemType === IProjectItemData_1.ProjectItemType.entityTypeResource ||
                candItem.itemType === IProjectItemData_1.ProjectItemType.entityTypeBehavior) &&
                entityIdList) {
                const entityDef = (await MinecraftDefinitions_1.default.get(candItem));
                if (entityDef && entityDef.id && entityIdList?.includes(entityDef?.id)) {
                    item.addParentItem(candItem);
                    entityIdList = Utilities_1.default.removeItemInArray(entityDef.id, entityIdList);
                }
            }
            else if (candItem.itemType === IProjectItemData_1.ProjectItemType.soundDefinitionCatalog && soundEventList) {
                await candItem.ensureStorage();
                if (candItem.primaryFile) {
                    const soundDef = await SoundDefinitionCatalogDefinition_1.default.ensureOnFile(candItem.primaryFile);
                    const soundSetNames = soundDef?.getSoundDefinitionSetNameList();
                    if (soundSetNames) {
                        for (const soundEventName of soundEventList) {
                            if (soundEventName.trim().length > 0) {
                                if (soundSetNames.includes(soundEventName)) {
                                    item.addChildItem(candItem);
                                    soundEventList = Utilities_1.default.removeItemInArray(soundEventName, soundEventList);
                                }
                            }
                        }
                    }
                }
            }
        }
        if (soundEventList) {
            for (const soundEvent of soundEventList) {
                if (soundEvent.trim().length > 0) {
                    const isVanilla = await Database_1.default.isVanillaToken(soundEvent);
                    item.addUnfulfilledRelationship(soundEvent, IProjectItemData_1.ProjectItemType.soundDefinitionCatalog, isVanilla);
                }
            }
        }
        if (entityIdList) {
            for (const entityId of entityIdList) {
                if (entityId.length > 0) {
                    const isVanilla = await Database_1.default.isVanillaToken(entityId);
                    item.addUnfulfilledRelationship(entityId, IProjectItemData_1.ProjectItemType.entityTypeBehavior, isVanilla);
                }
            }
        }
    }
}
exports.default = SoundCatalogDefinition;

//# sourceMappingURL=../maps/minecraft/SoundCatalogDefinition.js.map
