"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 IProjectItemData_1 = require("../app/IProjectItemData");
const SoundDefinitionCatalogDefinition_1 = require("./SoundDefinitionCatalogDefinition");
const Database_1 = require("./Database");
class MusicDefinitionCatalogDefinition {
    _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();
    }
    set file(newFile) {
        this._file = newFile;
    }
    get musicDefinitionNameList() {
        if (!this._data) {
            return undefined;
        }
        const musicDefNameList = [];
        for (const key in this._data) {
            musicDefNameList.push(key);
        }
        return musicDefNameList;
    }
    get musicDefinitionEventNameList() {
        if (!this._data) {
            return undefined;
        }
        const musicDefNameList = [];
        for (const key in this._data) {
            const def = this._data[key];
            if (def && def.event_name) {
                if (!musicDefNameList.includes(def.event_name)) {
                    musicDefNameList.push(def.event_name);
                }
            }
        }
        return musicDefNameList;
    }
    static async ensureOnFile(file, loadHandler) {
        let et;
        if (file.manager === undefined) {
            et = new MusicDefinitionCatalogDefinition();
            et.file = file;
            file.manager = et;
        }
        if (file.manager !== undefined && file.manager instanceof MusicDefinitionCatalogDefinition) {
            et = file.manager;
            if (!et.isLoaded && loadHandler) {
                et.onLoaded.subscribe(loadHandler);
            }
            await et.load();
        }
        return et;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        if (!this._data) {
            Log_1.default.unexpectedUndefined("MUSP");
            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;
        }
        if (!this._file.isContentLoaded) {
            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 musicDefList = this.musicDefinitionEventNameList;
        for (const candItem of itemsCopy) {
            if (candItem.itemType === IProjectItemData_1.ProjectItemType.soundDefinitionCatalog && musicDefList) {
                if (!candItem.isContentLoaded) {
                    await candItem.loadContent();
                }
                if (candItem.primaryFile) {
                    const soundDef = await SoundDefinitionCatalogDefinition_1.default.ensureOnFile(candItem.primaryFile);
                    const soundSetNames = soundDef?.getSoundDefinitionSetNameList();
                    if (soundSetNames) {
                        for (const musicDefName of musicDefList) {
                            if (soundSetNames.includes(musicDefName)) {
                                item.addChildItem(candItem);
                                const nextMusicDefNames = [];
                                for (const texturePath of musicDefList) {
                                    if (texturePath !== musicDefName) {
                                        nextMusicDefNames.push(texturePath);
                                    }
                                }
                                musicDefList = nextMusicDefNames;
                            }
                        }
                    }
                }
            }
        }
        if (musicDefList) {
            for (const musicDef of musicDefList) {
                item.addUnfulfilledRelationship(musicDef, IProjectItemData_1.ProjectItemType.soundDefinitionCatalog, await Database_1.default.isVanillaToken(musicDef));
            }
        }
    }
}
exports.default = MusicDefinitionCatalogDefinition;

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