"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 MinecraftUtilities_1 = require("./MinecraftUtilities");
const IProjectItemData_1 = require("../app/IProjectItemData");
const Database_1 = require("./Database");
const Utilities_1 = require("../core/Utilities");
const TextureDefinition_1 = require("./TextureDefinition");
class ParticleEffectResourceDefinition {
    _data;
    _file;
    _isLoaded = false;
    _onLoaded = new ste_events_1.EventDispatcher();
    get data() {
        return this._data;
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    set file(newFile) {
        this._file = newFile;
    }
    get id() {
        if (!this._data || !this._data.particle_effect || !this._data.particle_effect.description) {
            return undefined;
        }
        return this._data.particle_effect.description.identifier;
    }
    get description() {
        if (!this._data || !this._data.particle_effect || !this._data.particle_effect.description) {
            return undefined;
        }
        return this._data.particle_effect.description;
    }
    getCanonicalizedTexturesList() {
        if (!this.description ||
            !this.description.basic_render_parameters ||
            !this.description.basic_render_parameters.texture) {
            return undefined;
        }
        const result = TextureDefinition_1.default.canonicalizeTexturePath(this.description.basic_render_parameters.texture);
        return result ? [result] : [];
    }
    async getFormatVersionIsCurrent() {
        const fv = this.getFormatVersion();
        if (fv === undefined || fv.length !== 3) {
            return false;
        }
        return fv[0] > 1 || fv[1] >= 10;
    }
    getFormatVersion() {
        if (!this._data) {
            return undefined;
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this._data.format_version);
    }
    get formatVersion() {
        if (!this._data || !this._data.format_version) {
            return undefined;
        }
        return this._data.format_version;
    }
    static async ensureOnFile(file, loadHandler) {
        let et;
        if (file.manager === undefined) {
            et = new ParticleEffectResourceDefinition();
            et.file = file;
            file.manager = et;
        }
        if (file.manager !== undefined && file.manager instanceof ParticleEffectResourceDefinition) {
            et = file.manager;
            if (!et.isLoaded) {
                if (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("PERPF");
            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 deleteLinkToChild(rel) {
        let packRootFolder = this.getPackRootFolder();
        if (this._data === undefined) {
            await this.load();
        }
        if (!this.description ||
            !this.description.basic_render_parameters ||
            !this.description.basic_render_parameters.texture) {
            return;
        }
        const basicTexture = this.description.basic_render_parameters.texture;
        if (rel.childItem.itemType === IProjectItemData_1.ProjectItemType.texture) {
            if (!rel.childItem.isContentLoaded) {
                await rel.childItem.loadContent();
            }
            if (rel.childItem.primaryFile && packRootFolder) {
                let relativePath = this.getRelativePath(rel.childItem.primaryFile, packRootFolder);
                if (relativePath) {
                    if (basicTexture === relativePath) {
                        this.description.basic_render_parameters.texture = undefined;
                    }
                }
            }
        }
        this.persist();
    }
    getPackRootFolder() {
        let packRootFolder = undefined;
        if (this.file && this.file.parentFolder) {
            let parentFolder = this.file.parentFolder;
            packRootFolder = StorageUtilities_1.default.getParentOfParentFolderNamed("particles", 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.getCanonicalizedTexturesList();
        for (const candItem of itemsCopy) {
            if (candItem.itemType === IProjectItemData_1.ProjectItemType.texture && packRootFolder && textureList) {
                if (!candItem.isContentLoaded) {
                    await candItem.loadContent();
                }
                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) {
                const isVanillaToken = await Database_1.default.isVanillaToken(texturePath);
                item.addUnfulfilledRelationship(texturePath, IProjectItemData_1.ProjectItemType.texture, isVanillaToken);
            }
        }
    }
}
exports.default = ParticleEffectResourceDefinition;

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