"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetExpressionRuleIcon = exports.RenderExpressionRule = exports.RegisterExpressionRuleEditor = exports.ExpressionRuleEditorRegistration = exports.ExpressionRuleEditorRegistrationData = void 0;
const AppGlueError_1 = require("@appglue/common/dist/AppGlueError");
class ExpressionRuleEditorRegistrationData {
    typeName;
    editorPrototype;
    icon;
    constructor(editorPrototype, typeName, icon) {
        this.typeName = typeName;
        this.editorPrototype = editorPrototype;
        this.icon = icon;
    }
    create(rule) {
        let prototype = Object.getPrototypeOf(this.editorPrototype);
        let editor = new prototype.constructor();
        // console.log(rule.__type, editor)
        Reflect.set(editor, "ruleData", rule);
        return editor;
    }
}
exports.ExpressionRuleEditorRegistrationData = ExpressionRuleEditorRegistrationData;
class ExpressionRuleEditorRegistration {
    static registrations = {};
    static registerExpressionEditor(example, expressionRule, icon) {
        ExpressionRuleEditorRegistration.registrations[expressionRule.__type] =
            new ExpressionRuleEditorRegistrationData(example, expressionRule.__type, icon);
    }
    static getExpressionByTypeName(typeName) {
        return ExpressionRuleEditorRegistration.registrations[typeName];
    }
}
exports.ExpressionRuleEditorRegistration = ExpressionRuleEditorRegistration;
function RegisterExpressionRuleEditor(exampleExpression, forExpressionRule, icon) {
    ExpressionRuleEditorRegistration.registerExpressionEditor(exampleExpression, forExpressionRule, icon);
    if (!ExpressionRuleEditorRegistration.getExpressionByTypeName(forExpressionRule.__type))
        throw new Error("registration failed");
}
exports.RegisterExpressionRuleEditor = RegisterExpressionRuleEditor;
function RenderExpressionRule(rule) {
    if (!rule)
        return null;
    let editor = Reflect.get(rule, "___editor");
    if (!editor) {
        editor = ExpressionRuleEditorRegistration.getExpressionByTypeName(rule.__type)?.create(rule);
        Reflect.set(rule, "___editor", editor);
    }
    if (!editor)
        throw new AppGlueError_1.AppGlueError("no editor found for " + rule.__type);
    return editor.render();
}
exports.RenderExpressionRule = RenderExpressionRule;
function GetExpressionRuleIcon(type) {
    let reg = ExpressionRuleEditorRegistration.getExpressionByTypeName(type);
    if (!reg)
        throw new AppGlueError_1.AppGlueError("could not find registration " + type);
    return reg.icon;
}
exports.GetExpressionRuleIcon = GetExpressionRuleIcon;
//# sourceMappingURL=RegisterExpressionRuleEditor.js.map