"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.VibrantVisualsFileExtensionVariants = void 0;
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");
exports.VibrantVisualsFileExtensionVariants = [
    "_mer.png",
    "_mer.tga",
    "_mers.png",
    "_mers.tga",
    // "_normal.png", <-- too many false positives
    // "_normal.tga",
    ".texture_set.json",
];
class TextureDefinition {
    _file;
    _isLoaded = false;
    _onLoaded = new ste_events_1.EventDispatcher();
    id;
    get data() {
        if (!this._file || !this._file.content || typeof this._file.content === "string") {
            return undefined;
        }
        return this._file.content;
    }
    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 texd;
        if (file.manager === undefined) {
            texd = new TextureDefinition();
            texd.file = file;
            file.manager = texd;
        }
        if (file.manager !== undefined && file.manager instanceof TextureDefinition) {
            texd = file.manager;
            if (!texd.isLoaded && loadHandler) {
                texd.onLoaded.subscribe(loadHandler);
            }
            await texd.load();
        }
        return texd;
    }
    getReferencePath() {
        if (!this._file) {
            return undefined;
        }
        let projectPath = this._file.storageRelativePath;
        return TextureDefinition.getTexturePath(projectPath);
    }
    static canonicalizeTexturePath(projectPath) {
        if (projectPath === undefined) {
            return undefined;
        }
        projectPath = projectPath.toLowerCase();
        const lastPeriod = projectPath.lastIndexOf(".");
        if (lastPeriod >= 0) {
            const removedPart = projectPath.substring(lastPeriod + 1);
            if (StorageUtilities_1.AllowedExtensionsSet.has(removedPart)) {
                projectPath = projectPath.substring(0, lastPeriod);
            }
        }
        // particles has atlas.terrain in bedrock
        // JsonUI has # and $ as variabbles
        /*Log.assert(
          projectPath.startsWith("textures/") ||
            projectPath.startsWith("images/") ||
            projectPath.startsWith("#") ||
            projectPath.startsWith("$") ||
            projectPath.startsWith("atlas"),
          "Unexpected texture path: " + projectPath
        );*/
        return projectPath;
    }
    static getTexturePath(projectPath) {
        const lastPeriod = projectPath.lastIndexOf(".");
        if (lastPeriod >= 0) {
            projectPath = projectPath.substring(0, lastPeriod);
        }
        const ppLower = projectPath.toLowerCase();
        const texturesIndex = ppLower.indexOf("/textures/");
        if (texturesIndex < 0) {
            return undefined;
        }
        return projectPath.substring(texturesIndex + 1);
    }
    persist() { }
    async load() {
        if (this._isLoaded) {
            return;
        }
        if (this._file === undefined) {
            Log_1.default.unexpectedUndefined("TSCDF");
            return;
        }
        await this._file.loadContent();
        if (!this._file.content || typeof this._file.content === "string") {
            return;
        }
        this._isLoaded = true;
        this._onLoaded.dispatch(this, this);
    }
    async addChildItems(project, item) {
        const itemsCopy = project.getItemsCopy();
        for (const candItem of itemsCopy) {
            if (candItem.itemType === IProjectItemData_1.ProjectItemType.texture) {
                await candItem.ensureStorage();
                if (candItem.primaryFile) {
                    const parentFolder = candItem.primaryFile.parentFolder;
                    if (!parentFolder) {
                        continue;
                    }
                    await parentFolder.load();
                    let baseName = StorageUtilities_1.default.getBaseFromName(candItem.primaryFile.name);
                    for (const ext of exports.VibrantVisualsFileExtensionVariants) {
                        const vvSidecarFile = parentFolder.files[baseName + ext];
                        if (vvSidecarFile !== undefined && vvSidecarFile.extendedPath) {
                            const sidecarItem = project.getItemByExtendedOrProjectPath(vvSidecarFile.extendedPath);
                            if (sidecarItem && sidecarItem !== candItem) {
                                candItem.addChildItem(sidecarItem);
                            }
                        }
                    }
                }
            }
        }
    }
}
exports.default = TextureDefinition;

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