"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 JigsawStructureDefinition_1 = require("./JigsawStructureDefinition");
class JigsawStructureSetDefinition {
    _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:structure_set"]?.description?.identifier) {
            return this._data["minecraft:structure_set"].description.identifier;
        }
        return "";
    }
    get structures() {
        if (this._data?.["minecraft:structure_set"]?.structures) {
            return this._data["minecraft:structure_set"].structures;
        }
        return [];
    }
    async getFormatVersionIsCurrent() {
        // For now, assume format version is current
        return true;
    }
    async addChildItems(project, item) {
        const structures = this.structures;
        if (!structures || structures.length === 0) {
            return;
        }
        const itemsCopy = project.getItemsCopy();
        // Find jigsaw structures referenced by this structure set
        for (const structureRef of structures) {
            for (const candItem of itemsCopy) {
                if (candItem.itemType === IProjectItemData_1.ProjectItemType.jigsawStructure) {
                    if (!candItem.isContentLoaded) {
                        await candItem.loadContent();
                    }
                    if (candItem.primaryFile) {
                        const jigsawStructure = await JigsawStructureDefinition_1.default.ensureOnFile(candItem.primaryFile);
                        if (jigsawStructure && jigsawStructure.id === structureRef.structure) {
                            item.addChildItem(candItem);
                        }
                    }
                }
            }
        }
    }
    static async ensureOnFile(file) {
        let jigsawStructureSet;
        if (file.manager === undefined) {
            jigsawStructureSet = new JigsawStructureSetDefinition();
            jigsawStructureSet.file = file;
            file.manager = jigsawStructureSet;
        }
        if (file.manager !== undefined && file.manager instanceof JigsawStructureSetDefinition) {
            jigsawStructureSet = file.manager;
            if (!jigsawStructureSet.isLoaded) {
                await jigsawStructureSet.load();
            }
        }
        return jigsawStructureSet;
    }
    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 = JigsawStructureSetDefinition;

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