"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const StorageUtilities_1 = require("../storage/StorageUtilities");
const IProjectItemData_1 = require("../app/IProjectItemData");
const JigsawTemplatePoolDefinition_1 = require("./JigsawTemplatePoolDefinition");
class JigsawStructureDefinition {
    _file;
    _data;
    _isLoaded = false;
    get data() {
        return this._data;
    }
    get file() {
        return this._file;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get id() {
        if (this._data?.["minecraft:jigsaw"]?.description?.identifier) {
            return this._data["minecraft:jigsaw"].description.identifier;
        }
        return "";
    }
    get startPool() {
        if (this._data?.["minecraft:jigsaw"]?.start_pool) {
            return this._data["minecraft:jigsaw"].start_pool;
        }
        return undefined;
    }
    async getFormatVersionIsCurrent() {
        // For now, assume format version is current
        return true;
    }
    async addChildItems(project, item) {
        if (!this.startPool) {
            return;
        }
        const itemsCopy = project.getItemsCopy();
        // Find template pool referenced by start_pool
        for (const candItem of itemsCopy) {
            if (candItem.itemType === IProjectItemData_1.ProjectItemType.jigsawTemplatePool) {
                if (!candItem.isContentLoaded) {
                    await candItem.loadContent();
                }
                if (candItem.primaryFile) {
                    const templatePool = await JigsawTemplatePoolDefinition_1.default.ensureOnFile(candItem.primaryFile);
                    if (templatePool && templatePool.id === this.startPool) {
                        item.addChildItem(candItem);
                    }
                }
            }
        }
    }
    static async ensureOnFile(file) {
        let jigsawStructure;
        if (file.manager === undefined) {
            jigsawStructure = new JigsawStructureDefinition();
            jigsawStructure.file = file;
            file.manager = jigsawStructure;
        }
        if (file.manager !== undefined && file.manager instanceof JigsawStructureDefinition) {
            jigsawStructure = file.manager;
            if (!jigsawStructure.isLoaded) {
                await jigsawStructure.load();
            }
        }
        return jigsawStructure;
    }
    async load() {
        if (this._isLoaded) {
            return;
        }
        if (this._file === undefined) {
            return;
        }
        if (!this._file.isContentLoaded) {
            await this._file.loadContent();
        }
        if (!this._file.content || this._file.content instanceof Uint8Array) {
            return;
        }
        const result = StorageUtilities_1.default.getJsonObject(this._file);
        if (result) {
            this._data = result;
        }
        this._isLoaded = true;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const dataString = JSON.stringify(this._data, null, 2);
        this._file.setContent(dataString);
    }
}
exports.default = JigsawStructureDefinition;

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