"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 Database_1 = require("./Database");
class FlipbookTextureCatalogDefinition {
    _data;
    _file;
    _isLoaded = false;
    _onLoaded = new ste_events_1.EventDispatcher();
    id;
    get data() {
        return this._data;
    }
    get texturesList() {
        if (!this._data) {
            return undefined;
        }
        const textureList = [];
        for (const flipbookTexture of this._data) {
            const texturePath = flipbookTexture.flipbook_texture;
            if (texturePath) {
                textureList.push(texturePath.toLowerCase());
            }
        }
        return textureList;
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    set file(newFile) {
        this._file = newFile;
    }
    static async ensureOnFile(file, loadHandler) {
        let et;
        if (file.manager === undefined) {
            et = new FlipbookTextureCatalogDefinition();
            et.file = file;
            file.manager = et;
        }
        if (file.manager !== undefined && file.manager instanceof FlipbookTextureCatalogDefinition) {
            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("FBTCDF");
            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 deleteLinkToChild(rel) {
        let packRootFolder = this.getPackRootFolder();
        if (this._data === undefined) {
            await this.load();
        }
        if (!this._data) {
            return;
        }
        if (rel.childItem.itemType === IProjectItemData_1.ProjectItemType.texture) {
            await rel.childItem.ensureStorage();
            if (rel.childItem.primaryFile && packRootFolder) {
                let relativePath = this.getRelativePath(rel.childItem.primaryFile, packRootFolder);
                if (relativePath) {
                    let newFlipbookTextures = [];
                    for (const flipbookTexture of this._data) {
                        if (flipbookTexture.flipbook_texture !== relativePath) {
                            newFlipbookTextures.push(flipbookTexture);
                        }
                    }
                    this._data = newFlipbookTextures;
                }
            }
        }
        this.persist();
    }
    getTexturePaths() {
        const texturePaths = [];
        if (this.data) {
            for (const flipbookResource of this.data) {
                const resource = flipbookResource.flipbook_texture;
                if (!texturePaths.includes(resource)) {
                    texturePaths.push(resource);
                }
            }
        }
        return texturePaths;
    }
    getPackRootFolder() {
        let packRootFolder = undefined;
        if (this.file && this.file.parentFolder) {
            let parentFolder = this.file.parentFolder;
            packRootFolder = StorageUtilities_1.default.getParentOfParentFolderNamed("textures", parentFolder);
        }
        return packRootFolder;
    }
    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();
        let packRootFolder = this.getPackRootFolder();
        let textureList = this.texturesList;
        for (const candItem of itemsCopy) {
            if (candItem.itemType === IProjectItemData_1.ProjectItemType.texture && packRootFolder && textureList) {
                await candItem.ensureStorage();
                if (candItem.primaryFile) {
                    let relativePath = this.getRelativePath(candItem.primaryFile, 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, await Database_1.default.matchesVanillaPath(texturePath));
            }
        }
    }
}
exports.default = FlipbookTextureCatalogDefinition;

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