"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuleTableDesigner = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const ObserveState_1 = require("../../../../CommonUI/StateManagement/ObserveState");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
const ConfirmDialog_1 = require("../../../../CommonUI/ConfirmDialog");
const RuleTableHeader_1 = require("./RuleTableHeader");
const PlusGrayIcon_1 = require("../../../../CommonUI/Icon/PlusGrayIcon");
const Notification_1 = require("../../../../CommonUI/BuilderUI/Notification");
const CommonStyles_1 = require("../../../DesignUI/CommonStyles");
const SettingButton_1 = require("../../../DesignUI/SettingButton");
const XResultCell_1 = require("../../CommonUI/XResultCell");
const ObserveMultiState_1 = require("../../../../CommonUI/StateManagement/ObserveMultiState");
const ValidationInformation_1 = require("../../../DesignUI/ValidationInformation");
const DockSidebar_1 = require("../../../../CommonUI/BuilderUI/DockSidebar");
const CommonStyles_2 = require("../../../../CommonUI/CommonStyles");
const UISwitch_1 = require("../../../../CommonUI/BuildlingBlocks/UISwitch");
const RuleTableRow_1 = require("./RuleTableRow");
const TestContext_1 = require("../../../../CommonUI/Testing/TestContext");
const TableRowTestContext_1 = require("@appglue/common/dist/Rule/Table/Common/TableRowTestContext");
const TestResult_1 = require("../../../../CommonUI/Testing/TestResult");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
const material_1 = require("@mui/material");
const UILabel_1 = require("../../../../CommonUI/BuildlingBlocks/UILabel");
const RuleTableDesigner = (props) => {
    const { ruleDefinition, editContext } = props;
    const [openSettingsLayer, setOpenSettingsLayer] = (0, react_1.useState)(false);
    const testContext = react_1.default.useContext(TestContext_1.TestContext);
    const onAddNewRow = () => {
        if (testContext)
            return;
        ruleDefinition.addRow();
        StateManager_1.StateManager.propertyChanged(ruleDefinition, "rows");
    };
    const isDefaultTestResult = !!testContext &&
        !testContext.isError &&
        !(testContext?.items || []).find((rowResult) => rowResult.result === "true");
    if (testContext) {
        return ((0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleEditorInTester, { children: [(0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleEditorRender, { columnCount: ruleDefinition.ruleColumns.length + ruleDefinition.resultColumns.length + 1, className: "RuleEditorRender", rowsGap: 6, children: [(0, jsx_runtime_1.jsx)(RuleTableHeader_1.RuleTableHeader, { ...props }), (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: ruleDefinition.rows.map((row, idx) => {
                                return ((0, jsx_runtime_1.jsx)(RuleTableRow_1.XRuleTableRow, { editContext: editContext, row: row, ruleDefinition: ruleDefinition, rowIndex: idx }, row._id));
                            }) }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyIndexColumn, { "data-testid": "btn-rule-table-add-new-row" }), ruleDefinition.ruleColumns.map((_, idx) => {
                            return (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyColumn, {}, idx);
                        }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, {}), ruleDefinition.resultColumns.map((col) => {
                            return (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyColumn, {}, col._id);
                        }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, {}), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, {}), ruleDefinition.allowDefaultResults && ((0, jsx_runtime_1.jsxs)(TableRowTestContext_1.TableRowTestContext.Provider, { value: isDefaultTestResult ? TableRowTestContext_1.ITableRowTestValue.PASSED : undefined, children: [(0, jsx_runtime_1.jsx)(CommonStyles_1.RuleLabelColumn, { colSpan: ruleDefinition.ruleColumns.length + 1, colStart: 1, y: ruleDefinition.rows.length + 3, children: "Default Result Values" }), ruleDefinition.resultColumns.map((rescol, index2) => {
                                    return ((0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: rescol, control: () => ((0, jsx_runtime_1.jsx)(XResultCell_1.XResultCell, { ruleDefinition: ruleDefinition, resultColumn: rescol, editContext: editContext, resultCell: ruleDefinition.defaultResult[rescol._id], rowIndex: ruleDefinition.rows.length + 3, cellIndex: ruleDefinition.ruleColumns.length + 2 + index2 })) }, rescol._id));
                                })] }))] }), (0, jsx_runtime_1.jsx)(TestResult_1.TestResult, { executableId: ruleDefinition._id })] }));
    }
    return ((0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleDesignerRender, { children: [(0, jsx_runtime_1.jsx)(CommonStyles_1.RuleToolbarRender, { children: (0, jsx_runtime_1.jsx)(SettingButton_1.SettingButton, { onClick: () => setOpenSettingsLayer(true) }) }), (0, jsx_runtime_1.jsx)(DockSidebar_1.DockedSidebar, { sidebar: () => {
                    return ((0, jsx_runtime_1.jsx)(CommonStyles_2.EditLayerConfigArea, { children: (0, jsx_runtime_1.jsxs)(CommonStyles_2.EditLayerStyledAccordion, { expanded: true, onChange: () => { }, defaultExpanded: true, children: [(0, jsx_runtime_1.jsx)(CommonStyles_2.EditLayerStyledAccordionSummary, { children: (0, jsx_runtime_1.jsx)(CommonStyles_2.EditLayerStyledTypography, { variant: "subtitle1", classes: { root: "config-form-header" }, children: "Settings" }) }), (0, jsx_runtime_1.jsx)(CommonStyles_2.EditLayerStyledAccordionDetails, { classes: { root: "config-form-content" }, children: (0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: ruleDefinition, control: () => ((0, jsx_runtime_1.jsxs)(material_1.Box, { display: "flex", alignContent: "center", children: [(0, jsx_runtime_1.jsx)(UILabel_1.UILabel, { children: "Allow Default Results" }), (0, jsx_runtime_1.jsx)(material_1.Box, { display: "flex", paddingLeft: "10px", children: (0, jsx_runtime_1.jsx)(UISwitch_1.UISwitch, { checked: ruleDefinition.allowDefaultResults, onSet: (newValue) => {
                                                            ruleDefinition.allowDefaultResults = newValue;
                                                            StateManager_1.StateManager.propertyChanged(ruleDefinition, "allowDefaultResults");
                                                        } }) })] })) }) })] }) }));
                }, content: () => ((0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: ruleDefinition, properties: ["ruleColumns", "resultColumns"], control: () => {
                        return ((0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleEditorRender, { columnCount: ruleDefinition.ruleColumns.length + ruleDefinition.resultColumns.length + 1, children: [(0, jsx_runtime_1.jsx)(RuleTableHeader_1.RuleTableHeader, { ...props }), (0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: ruleDefinition, properties: ["rows"], control: () => {
                                        return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: ruleDefinition.rows.map((row, idx) => {
                                                return ((0, jsx_runtime_1.jsx)(RuleTableRow_1.XRuleTableRow, { editContext: editContext, row: row, ruleDefinition: ruleDefinition, rowIndex: idx }, row._id));
                                            }) }));
                                    } }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyIndexColumn, { onClick: onAddNewRow, "data-testid": "btn-rule-table-add-new-row", children: !testContext && (0, jsx_runtime_1.jsx)(PlusGrayIcon_1.PlusGrayIcon, {}) }), ruleDefinition.ruleColumns.map((_, idx) => {
                                    return (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyColumn, {}, idx);
                                }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, {}), ruleDefinition.resultColumns.map((col) => {
                                    return (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyColumn, {}, col._id);
                                }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, {}), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, {}), (0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: ruleDefinition, properties: ["allowDefaultResults"], control: () => {
                                        if (!ruleDefinition.allowDefaultResults)
                                            return null;
                                        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(CommonStyles_1.RuleLabelColumn, { colSpan: ruleDefinition.ruleColumns.length + 1, colStart: 1, children: "Default Result Values" }), ruleDefinition.resultColumns.map((rescol) => {
                                                    return ((0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: rescol, control: () => ((0, jsx_runtime_1.jsx)(XResultCell_1.XResultCell, { ruleDefinition: ruleDefinition, resultColumn: rescol, editContext: editContext, resultCell: ruleDefinition.defaultResult[rescol._id] })) }, rescol._id));
                                                })] }));
                                    } })] }));
                    } })), sidebarWidth: 350, open: openSettingsLayer, onClose: () => setOpenSettingsLayer(false) }), (0, jsx_runtime_1.jsx)(Notification_1.Notification, { editContext: editContext }), (0, jsx_runtime_1.jsx)(ObserveMultiState_1.ObserveMultiState, { listenTo: [
                    ...props.ruleDefinition.ruleColumns,
                    ...props.ruleDefinition.resultColumns,
                    props.ruleDefinition,
                ], control: () => (0, jsx_runtime_1.jsx)(ValidationInformation_1.ValidationInformation, { provider: ruleDefinition }) }), (0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: props.editContext, properties: ["deletingColumn"], control: () => {
                    if (props.editContext.deletingColumn) {
                        return ((0, jsx_runtime_1.jsx)(ConfirmDialog_1.ConfirmDialog, { open: true, onConfirm: (dontAskAgain) => {
                                if (dontAskAgain) {
                                    DataUtilities_1.DataUtilities.setCookieValue("RULE_TABLE_DELETE_CONFIRM", "true");
                                }
                                props.editContext.forceRemoveColumn(props.editContext.deletingColumn);
                                StateManager_1.StateManager.propertyChanged(props.ruleDefinition, "ruleColumns");
                                props.editContext.deletingColumn = undefined;
                            }, onCancel: () => (props.editContext.deletingColumn = undefined) }));
                    }
                    return null;
                } })] }));
};
exports.RuleTableDesigner = RuleTableDesigner;
//# sourceMappingURL=RuleTableDesigner.js.map