"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const EntityTypeDefinition_1 = require("./EntityTypeDefinition");
const Log_1 = require("../core/Log");
const Database_1 = require("./Database");
const IField_1 = require("../dataform/IField");
const ManagedComponent_1 = require("./ManagedComponent");
class EntityTypeComponentSetUtilities {
    static async getTriggers(componentSet, isBaseComponent) {
        const allTriggers = [];
        const comps = componentSet.getComponents();
        for (const comp of comps) {
            let formName = comp.id;
            if (formName.startsWith("minecraft:") && (isBaseComponent || formName !== "minecraft:breedable")) {
                formName = EntityTypeDefinition_1.default.getFormIdFromComponentId(formName);
                const form = await Database_1.default.ensureFormLoaded("entity", formName);
                Log_1.default.assert(form !== undefined);
                if (form) {
                    await EntityTypeComponentSetUtilities.processFormForTriggers(allTriggers, form, comp, comp.id);
                }
            }
        }
        return allTriggers;
    }
    static async processFormForTriggers(triggers, form, component, propertyPath) {
        for (const field of form.fields) {
            if ((field.dataType === IField_1.FieldDataType.object || field.dataType === IField_1.FieldDataType.objectArray) && field.subForm) {
                let obj = undefined;
                if (component) {
                    obj = component.getProperty(field.id);
                }
                const data = component?.getData();
                if (data && typeof data === "object") {
                    if (field.dataType === IField_1.FieldDataType.objectArray && Array.isArray(obj)) {
                        let index = 0;
                        for (const arrObj of obj) {
                            await this.processFormForTriggers(triggers, field.subForm, new ManagedComponent_1.ManagedComponent(data, field.id, arrObj), propertyPath + "." + field.id + "." + index);
                            index++;
                        }
                    }
                    else {
                        await this.processFormForTriggers(triggers, field.subForm, new ManagedComponent_1.ManagedComponent(data, field.id, obj), propertyPath + "." + field.id);
                    }
                }
            }
            else if (field.dataType === IField_1.FieldDataType.minecraftEventTrigger ||
                field.dataType === IField_1.FieldDataType.minecraftEventReference) {
                let ref = undefined;
                if (component) {
                    ref = component.getProperty(field.id);
                }
                const path = propertyPath + "." + field.id;
                if (!ref) {
                    triggers.push({
                        path: path,
                    });
                }
                else {
                    triggers.push({
                        path: path,
                        reference: ref,
                        referenceId: ref.event ? ref.event : ref,
                    });
                }
            }
        }
    }
}
exports.default = EntityTypeComponentSetUtilities;

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