"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlowStepEditorRegistration = exports.FlowStepEditorRegistrationClass = exports.RegisterFlowEditor = exports.FlowStepEditorRegistrationData = void 0;
const RegisterFlowStepData_1 = require("@appglue/common/dist/Flow/Utilities/RegisterFlowStepData");
const AppGlueError_1 = require("@appglue/common/dist/AppGlueError");
class FlowStepEditorRegistrationData {
    category;
    type;
    name;
    icon;
    include;
    prototype;
    constructor(prototype, type, category, name, icon, include) {
        this.prototype = prototype;
        this.category = category;
        this.type = type;
        this.name = name;
        this.icon = icon;
        this.include = include;
    }
    create(editContext, data) {
        let editor = new this.prototype.constructor();
        editor.editContext = editContext;
        if (!data) {
            let dataReg = RegisterFlowStepData_1.FlowStepDataRegistration.getByTypeName(this.type);
            if (!dataReg)
                throw new AppGlueError_1.AppGlueError("no data registration found for " + this.type);
            data = dataReg.create();
        }
        editor.stepData = data;
        return editor;
    }
}
exports.FlowStepEditorRegistrationData = FlowStepEditorRegistrationData;
function RegisterFlowEditor(step, forStep, category, name, icon, include) {
    let prototype = Object.getPrototypeOf(step);
    prototype.__type = forStep.__type;
    exports.FlowStepEditorRegistration.addRegistration(new FlowStepEditorRegistrationData(prototype, forStep.__type, category, name, icon, include));
}
exports.RegisterFlowEditor = RegisterFlowEditor;
class FlowStepEditorRegistrationClass {
    registrations = {};
    addRegistration(data) {
        this.registrations[data.type] = data;
    }
    getByTypeName(type) {
        return this.registrations[type];
    }
    getAll(forData) {
        let output = [];
        Object.values(this.registrations).forEach((v) => {
            if (v.include) {
                // if it has a filter, will only add if there is data and there filter passes.
                if (forData && v.include(forData))
                    output.push(v);
            }
            else {
                output.push(v);
            }
        });
        return output;
    }
    getItemsByCategories(category) {
        const itemsByCategory = this.getAll().reduce((obj, data) => {
            if (!category || (category && category === data.category)) {
                if (!obj[data.category]) {
                    obj[data.category] = [];
                }
                obj[data.category].push(data);
            }
            return obj;
        }, {});
        return itemsByCategory;
    }
}
exports.FlowStepEditorRegistrationClass = FlowStepEditorRegistrationClass;
exports.FlowStepEditorRegistration = new FlowStepEditorRegistrationClass();
//# sourceMappingURL=RegisterFlowEditor.js.map