"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const ProjectItemCreateManager_1 = require("../app/ProjectItemCreateManager");
const StorageUtilities_1 = require("../storage/StorageUtilities");
class AudioDefinition {
    _file;
    _id;
    _isLoaded = false;
    _onLoaded = new ste_events_1.EventDispatcher();
    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._file) {
            return this._file.name;
        }
        return this._id;
    }
    set id(newId) {
        this._id = newId;
    }
    static async ensureOnFile(file, loadHandler) {
        let afd;
        if (file.manager === undefined) {
            afd = new AudioDefinition();
            afd.file = file;
            file.manager = afd;
        }
        if (file.manager !== undefined && file.manager instanceof AudioDefinition) {
            afd = file.manager;
            if (!afd.isLoaded) {
                if (loadHandler) {
                    afd.onLoaded.subscribe(loadHandler);
                }
                await afd.load();
            }
        }
        return afd;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
    }
    static canonicalizeAudioPath(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);
            }
        }
        /*
        Log.assert(
          projectPath.startsWith("sounds/") || projectPath.startsWith("music/") || projectPath.startsWith("$"),
          "Unexpected audio path: " + projectPath
        );*/
        return projectPath;
    }
    async ensureSoundDefinitionsForFile(project) {
        if (!this._file) {
            return;
        }
        let soundDefinitionCat = await ProjectItemCreateManager_1.default.ensureSoundDefinitionCatalogDefinition(project);
        if (this._file && soundDefinitionCat) {
            let matches = soundDefinitionCat.getSoundDefinitionMatchesByPath(this._file);
            if (matches) {
                let keyCount = 0;
                for (const key in matches) {
                    if (key) {
                        keyCount++;
                    }
                }
                if (keyCount === 0) {
                    soundDefinitionCat.ensureDefintionForFile(project, this._file);
                }
            }
            soundDefinitionCat.persist();
        }
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        if (!this._file.isContentLoaded) {
            await this._file.loadContent();
        }
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this._isLoaded = true;
    }
}
exports.default = AudioDefinition;

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