"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 StorageUtilities_1 = require("../storage/StorageUtilities");
const Database_1 = require("./Database");
const MinecraftUtilities_1 = require("./MinecraftUtilities");
const IProjectItemData_1 = require("../app/IProjectItemData");
const ItemTypeDefinition_1 = require("./ItemTypeDefinition");
class RecipeBehaviorDefinition {
    _file;
    _id;
    _isLoaded = false;
    _data;
    _interior;
    _onLoaded = new ste_events_1.EventDispatcher();
    get data() {
        return this._data;
    }
    set data(newData) {
        this._data = newData;
        if (this._data && this._data["minecraft:recipe_shaped"]) {
            this._interior = this._data["minecraft:recipe_shaped"];
        }
        else if (this._data && this._data["minecraft:recipe_shapeless"]) {
            this._interior = this._data["minecraft:recipe_shapeless"];
        }
        else {
            this._interior = undefined;
        }
    }
    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._interior && this._interior.description && this._interior.description.identifier) {
            return this._interior.description.identifier;
        }
        return this._id;
    }
    set id(newId) {
        if (this._interior && this._interior.description) {
            this._interior.description.identifier = newId;
        }
        this._id = newId;
    }
    get shortId() {
        if (this._id !== undefined) {
            if (this._id.startsWith("minecraft:")) {
                return this._id.substring(10, this._id.length);
            }
            return this._id;
        }
        return undefined;
    }
    async getFormatVersionIsCurrent() {
        const fv = this.getFormatVersion();
        if (fv === undefined || fv.length !== 3) {
            return false;
        }
        return await Database_1.default.isRecentVersionFromVersionArray(fv);
    }
    getFormatVersion() {
        if (!this._data || !this._data.format_version) {
            return undefined;
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this._data.format_version);
    }
    setBehaviorPackFormatVersion(versionStr) {
        this._ensureDataInitialized();
        if (this._data) {
            this._data.format_version = versionStr;
        }
    }
    _ensureDataInitialized() {
        if (this._data === undefined) {
            this._data = {};
        }
    }
    async addChildItems(project, recipeItem) {
        const itemsCopy = project.getItemsCopy();
        const dependentItemIds = [];
        // Collect all item IDs that this recipe depends on
        if (this._interior) {
            // For shapeless recipes, collect ingredients
            if ("ingredients" in this._interior && this._interior.ingredients) {
                for (const ingredient of this._interior.ingredients) {
                    if (ingredient && ingredient.item) {
                        dependentItemIds.push(ingredient.item);
                    }
                }
            }
            // For shaped recipes, collect key items
            if ("key" in this._interior && this._interior.key) {
                for (const keyItem of Object.values(this._interior.key)) {
                    if (keyItem && keyItem.item) {
                        dependentItemIds.push(keyItem.item);
                    }
                }
            }
        }
        // Check each dependent item ID against project items
        for (const dependentItemId of dependentItemIds) {
            let foundMatch = false;
            for (const candItem of itemsCopy) {
                if (candItem.itemType === IProjectItemData_1.ProjectItemType.itemTypeBehavior) {
                    await candItem.ensureStorage();
                    if (candItem.primaryFile) {
                        const itd = await ItemTypeDefinition_1.default.ensureOnFile(candItem.primaryFile);
                        if (itd && itd.id === dependentItemId) {
                            recipeItem.addChildItem(candItem);
                            foundMatch = true;
                            break;
                        }
                    }
                }
            }
            // If no matching item type was found, add as unfulfilled relationship
            if (!foundMatch) {
                recipeItem.addUnfulfilledRelationship(dependentItemId, IProjectItemData_1.ProjectItemType.itemTypeBehavior, false);
            }
        }
    }
    static async ensureOnFile(file, loadHandler) {
        let rbd;
        if (file.manager === undefined) {
            rbd = new RecipeBehaviorDefinition();
            rbd.file = file;
            file.manager = rbd;
        }
        if (file.manager !== undefined && file.manager instanceof RecipeBehaviorDefinition) {
            rbd = file.manager;
            if (!rbd.isLoaded && loadHandler) {
                rbd.onLoaded.subscribe(loadHandler);
            }
            await rbd.load();
        }
        return rbd;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const bpString = JSON.stringify(this._data, null, 2);
        this._file.setContent(bpString);
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        await this._file.loadContent();
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this.data = StorageUtilities_1.default.getJsonObject(this._file);
        this._isLoaded = true;
    }
}
exports.default = RecipeBehaviorDefinition;

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