"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 Utilities_1 = require("../core/Utilities");
const TextureDefinition_1 = require("./TextureDefinition");
class SkinCatalogDefinition {
    _file;
    _isLoaded = false;
    _data;
    _onLoaded = new ste_events_1.EventDispatcher();
    get isLoaded() {
        return this._isLoaded;
    }
    get data() {
        return this._data;
    }
    get file() {
        return this._file;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    set file(newFile) {
        this._file = newFile;
    }
    get id() {
        if (!this._data) {
            return undefined;
        }
        return this._data.serialize_name;
    }
    get skins() {
        if (!this._data) {
            return undefined;
        }
        if (this._data.skins === undefined) {
            this._data.skins = [];
        }
        return this._data.skins;
    }
    getCanonicalizedTexturesList() {
        if (!this._data || !this._data.skins) {
            return undefined;
        }
        const textureList = [];
        for (const skin of this._data.skins) {
            if (skin.texture) {
                const texturePath = TextureDefinition_1.default.canonicalizeTexturePath(skin.texture);
                if (texturePath) {
                    textureList.push(texturePath);
                }
            }
        }
        return textureList;
    }
    get skinNameList() {
        if (!this._data || !this._data.skins) {
            return undefined;
        }
        const skinNameList = [];
        for (const skin of this._data.skins) {
            if (skin.localization_name) {
                skinNameList.push(skin.localization_name);
            }
        }
        return skinNameList;
    }
    static async ensureOnFile(file, loadHandler) {
        let et;
        if (file.manager === undefined) {
            et = new SkinCatalogDefinition();
            et.file = file;
            file.manager = et;
        }
        if (file.manager !== undefined && file.manager instanceof SkinCatalogDefinition) {
            et = file.manager;
            if (!et.isLoaded && loadHandler) {
                et.onLoaded.subscribe(loadHandler);
            }
            await et.load();
        }
        return et;
    }
    ensureData() {
        if (this._data) {
            return this._data;
        }
        this._data = {
            skins: [],
        };
        return this._data;
    }
    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("SSDL");
            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);
    }
    getRelativePath(file, packRootFolder) {
        let relativePath = file.getFolderRelativePath(packRootFolder);
        if (relativePath) {
            const lastPeriod = relativePath?.lastIndexOf(".");
            if (lastPeriod >= 0) {
                relativePath = relativePath?.substring(0, lastPeriod).toLowerCase();
            }
            relativePath = StorageUtilities_1.default.ensureNotStartsWithDelimiter(relativePath);
        }
        return relativePath;
    }
    async addChildItems(project, item) {
        const itemsCopy = project.getItemsCopy();
        const pack = await item.getPack();
        if (!pack) {
            return;
        }
        let packRootFolder = pack.folder;
        let textureList = this.getCanonicalizedTexturesList();
        for (const candItem of itemsCopy) {
            if (candItem.itemType === IProjectItemData_1.ProjectItemType.texture && packRootFolder && textureList) {
                await candItem.ensureStorage();
                if (candItem.defaultFile) {
                    let relativePath = TextureDefinition_1.default.canonicalizeTexturePath(this.getRelativePath(candItem.defaultFile, packRootFolder));
                    if (relativePath) {
                        if (textureList && textureList.includes(relativePath)) {
                            item.addChildItem(candItem);
                            textureList = Utilities_1.default.removeItemInArray(relativePath, textureList);
                        }
                    }
                }
            }
        }
        if (textureList) {
            for (const texturePath of textureList) {
                item.addUnfulfilledRelationship(texturePath, IProjectItemData_1.ProjectItemType.texture, false);
            }
        }
    }
}
exports.default = SkinCatalogDefinition;

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