"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 JigsawProcessorListDefinition_1 = require("./JigsawProcessorListDefinition");
const Log_1 = require("../core/Log");
class JigsawTemplatePoolDefinition {
    _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:template_pool"]?.description?.identifier) {
            return this._data["minecraft:template_pool"].description.identifier;
        }
        return "";
    }
    get elements() {
        if (this._data?.["minecraft:template_pool"]?.elements) {
            return this._data["minecraft:template_pool"].elements;
        }
        return [];
    }
    async getFormatVersionIsCurrent() {
        // For now, assume format version is current
        return true;
    }
    async addChildItems(project, item) {
        const elements = this.elements;
        if (!elements || elements.length === 0) {
            return;
        }
        const jigsawProcessorListItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.jigsawProcessorList);
        // Find structure files and processors referenced by this template pool
        for (const element of elements) {
            // Find structure files referenced by location
            if (element.element.location) {
                const structurePath = element.element.location + ".mcstructure";
                for (const candItem of jigsawProcessorListItems) {
                    if (candItem.itemType === IProjectItemData_1.ProjectItemType.structure) {
                        if (candItem.projectPath?.endsWith(structurePath) ||
                            candItem.projectPath?.includes(element.element.location)) {
                            item.addChildItem(candItem);
                        }
                    }
                }
            }
            // Find processors referenced by processors field
            if (element.element.processors) {
                for (const candItem of jigsawProcessorListItems) {
                    if (!candItem.isContentLoaded) {
                        await candItem.loadContent();
                    }
                    if (candItem.primaryFile) {
                        const processorList = await JigsawProcessorListDefinition_1.default.ensureOnFile(candItem.primaryFile);
                        if (processorList && processorList.id === element.element.processors) {
                            item.addChildItem(candItem);
                        }
                    }
                }
            }
        }
    }
    static async ensureOnFile(file) {
        let jigsawTemplatePool;
        if (file.manager === undefined) {
            jigsawTemplatePool = new JigsawTemplatePoolDefinition();
            jigsawTemplatePool.file = file;
            file.manager = jigsawTemplatePool;
        }
        if (file.manager !== undefined && file.manager instanceof JigsawTemplatePoolDefinition) {
            jigsawTemplatePool = file.manager;
            if (!jigsawTemplatePool.isLoaded) {
                await jigsawTemplatePool.load();
            }
        }
        return jigsawTemplatePool;
    }
    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;
        }
        if (!this._data) {
            Log_1.default.unexpectedUndefined("ITRDP");
            return;
        }
        const dataString = JSON.stringify(this._data, null, 2);
        this._file.setContent(dataString);
    }
}
exports.default = JigsawTemplatePoolDefinition;

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