"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 Database_1 = require("../Database");
class LegacyDocumentationDefinition {
    _data;
    _file;
    _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;
    }
    static async loadNode(catalog, nodePath, isPreview) {
        let metadataFolder = undefined;
        if (isPreview) {
            metadataFolder = await Database_1.default.loadPreviewMetadataFolder();
        }
        else {
            metadataFolder = await Database_1.default.loadReleaseMetadataFolder();
        }
        if (!metadataFolder) {
            return undefined;
        }
        const docFile = await metadataFolder.ensureFileFromRelativePath("/doc_modules/" + catalog + ".json");
        if (!docFile) {
            return undefined;
        }
        if (!(await docFile.exists())) {
            return undefined;
        }
        const legacyDocDef = await LegacyDocumentationDefinition.ensureOnFile(docFile);
        if (!legacyDocDef) {
            return undefined;
        }
        return legacyDocDef.getNode(nodePath);
    }
    getNode(nodePath) {
        if (!this._data) {
            return;
        }
        const nodeEntries = nodePath.split("/");
        let curNode = this._data;
        for (let nodeEntry of nodeEntries) {
            if (nodeEntry.length > 0) {
                nodeEntry = nodeEntry.toLowerCase();
                if (!curNode.nodes || curNode.nodes.length === 0) {
                    return undefined;
                }
                let foundNextNode = false;
                for (const node of curNode.nodes) {
                    if (!foundNextNode &&
                        ((node.name && node.name.toLowerCase() === nodeEntry) ||
                            (node.examples_title && node.examples_title.toLowerCase() === nodeEntry))) {
                        curNode = node;
                        foundNextNode = true;
                    }
                }
                if (!foundNextNode) {
                    return undefined;
                }
            }
        }
        return curNode;
    }
    static async ensureOnFile(file, loadHandler) {
        let ldd;
        if (file.manager === undefined) {
            ldd = new LegacyDocumentationDefinition();
            ldd.file = file;
            file.manager = ldd;
        }
        if (file.manager !== undefined && file.manager instanceof LegacyDocumentationDefinition) {
            ldd = file.manager;
            if (!ldd.isLoaded && loadHandler) {
                ldd.onLoaded.subscribe(loadHandler);
            }
            await ldd.load();
        }
        return ldd;
    }
    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("LDDF");
            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);
    }
}
exports.default = LegacyDocumentationDefinition;

//# sourceMappingURL=../../maps/minecraft/docs/LegacyDocumentationDefinition.js.map
