"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuleTableBuilder = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importDefault(require("react"));
const AutoBind_1 = require("@appglue/common/dist/Common/AutoBind");
const Sidebar_1 = require("../../../CommonUI/BuilderUI/Sidebar");
const Topbar_1 = require("../../../CommonUI/BuilderUI/Topbar");
const DataIcon_1 = require("../../../CommonUI/Icon/DataIcon");
const RuleTableIcon_1 = require("../../../CommonUI/Icon/RuleTableIcon");
const ObserveState_1 = require("../../../CommonUI/StateManagement/ObserveState");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
const CommonStyles_1 = require("../../DesignUI/CommonStyles");
const RuleBuilderPage_1 = require("@appglue/common/dist/Rule/RuleBuilderPage");
const RuleTableDesigner_1 = require("./Builder/RuleTableDesigner");
const RuleTableEditContext_1 = require("@appglue/common/dist/Rule/Table/Rule/RuleTableEditContext");
const XExecutableTester_1 = require("../../../CommonUI/Testing/XExecutableTester");
const TestDataIcon_1 = require("../../../CommonUI/Icon/TestDataIcon");
const XBuilderEditPage_1 = require("../../../CommonUI/BuilderUI/XBuilderEditPage");
const CodingIcon_1 = require("../../../CommonUI/Icon/CodingIcon");
const DataBuilder_1 = require("../../../DataBuilder/DataBuilder");
class RuleTableBuilder extends react_1.default.Component {
    editContext;
    tabs;
    ruleDefinition;
    constructor(props) {
        super(props);
        this.ruleDefinition = props.ruleDefinition;
        this.editContext = new RuleTableEditContext_1.RuleTableEditContext(this.ruleDefinition);
        if (props.ruleTableName) {
            this.editContext.title = props.ruleTableName;
        }
        if (props.onRuleSave) {
            this.editContext.onRuleSave = props.onRuleSave;
        }
        if (props.onRuleCancel) {
            this.editContext.onRuleCancel = props.onRuleCancel;
        }
        if (props.dataDefinition) {
            props.ruleDefinition.inputData = props.dataDefinition;
        }
        this.tabs = this.getDefaultTabs();
    }
    onChangeRuleName(newName) {
        this.editContext.title = newName;
        StateManager_1.StateManager.propertyChanged(this.editContext, "title");
    }
    getDefaultTabs() {
        return [
            {
                value: RuleBuilderPage_1.RuleBuilderPage.Rule,
                renderUI: () => ((0, jsx_runtime_1.jsx)(RuleTableDesigner_1.RuleTableDesigner, { editContext: this.editContext, ruleDefinition: this.ruleDefinition })),
                icon: (0, jsx_runtime_1.jsx)(RuleTableIcon_1.RuleTableIcon, {}),
            },
            {
                value: RuleBuilderPage_1.RuleBuilderPage.Data,
                renderUI: () => ((0, jsx_runtime_1.jsx)(DataBuilder_1.DataBuilder, { definition: this.props.ruleDefinition.getInputDefinition(), readOnly: !this.props.ruleDefinition.canChangeInputs })),
                icon: (0, jsx_runtime_1.jsx)(DataIcon_1.DataIcon, {}),
            },
            {
                value: RuleBuilderPage_1.RuleBuilderPage.Configuration,
                renderUI: () => (0, jsx_runtime_1.jsx)(XBuilderEditPage_1.XBuilderEditPage, { builder: this.props.ruleDefinition, updateCallback: () => { } }),
                icon: (0, jsx_runtime_1.jsx)(CodingIcon_1.CodingIcon, {}),
            },
            {
                value: RuleBuilderPage_1.RuleBuilderPage.Test,
                renderUI: () => ((0, jsx_runtime_1.jsx)(XExecutableTester_1.XExecutableTester, { saveUnitTest: this.props.saveUnitTest, executable: this.props.ruleDefinition })),
                icon: (0, jsx_runtime_1.jsx)(TestDataIcon_1.TestDataIcon, {}),
            },
        ];
    }
    render() {
        return ((0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleEditorDiv, { children: [(0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: this.editContext, properties: ["title"], control: () => {
                        return ((0, jsx_runtime_1.jsx)(Topbar_1.Topbar, { name: this.editContext.title, onChangeName: this.onChangeRuleName, editContext: this.editContext, isActionDisabled: !this.editContext.lastSelectionElement, isRequiredActionGroup: false, onSave: this.editContext.onRuleSave
                                ? () => {
                                    if (this.editContext.onRuleSave) {
                                        this.editContext.onRuleSave();
                                    }
                                }
                                : undefined, onClose: this.editContext.onRuleCancel }));
                    } }), (0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: this.editContext, properties: ["mode"], control: () => ((0, jsx_runtime_1.jsx)(Sidebar_1.Sidebar, { items: this.tabs, value: this.editContext.mode, onChange: (mode) => {
                            Reflect.set(this.editContext, "mode", mode);
                            StateManager_1.StateManager.propertyChanged(this.editContext, "mode");
                        } })) })] }));
    }
}
tslib_1.__decorate([
    AutoBind_1.AutoBind
], RuleTableBuilder.prototype, "onChangeRuleName", null);
exports.RuleTableBuilder = RuleTableBuilder;
//# sourceMappingURL=RuleTableBuilder.js.map