"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValueTableRuleDesigner = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const PlusGrayIcon_1 = require("../../../../CommonUI/Icon/PlusGrayIcon");
const ObserveState_1 = require("../../../../CommonUI/StateManagement/ObserveState");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
const CommonStyles_1 = require("../../../DesignUI/CommonStyles");
const Notification_1 = require("../../../../CommonUI/BuilderUI/Notification");
const LinkingDataDialog_1 = require("../../../DesignUI/LinkingDataDialog");
const SettingButton_1 = require("../../../DesignUI/SettingButton");
const ValueTableRuleHeader_1 = require("./ValueTableRuleHeader");
const UISwitch_1 = require("../../../../CommonUI/BuildlingBlocks/UISwitch");
const ValueTableResultDefaultCell_1 = require("./ValueTableResultDefaultCell");
const DockSidebar_1 = require("../../../../CommonUI/BuilderUI/DockSidebar");
const CommonStyles_2 = require("../../../../CommonUI/CommonStyles");
const ValidationInformation_1 = require("../../../DesignUI/ValidationInformation");
const ObserveMultiState_1 = require("../../../../CommonUI/StateManagement/ObserveMultiState");
const ValueTableRow_1 = require("./ValueTableRow");
const TestContext_1 = require("../../../../CommonUI/Testing/TestContext");
const TableRowTestContext_1 = require("@appglue/common/dist/Rule/Table/Common/TableRowTestContext");
const XResultCell_1 = require("../../CommonUI/XResultCell");
const CommonStyles_3 = require("../../CommonStyles");
const TestResult_1 = require("../../../../CommonUI/Testing/TestResult");
const ConfirmDialog_1 = require("../../../../CommonUI/ConfirmDialog");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
const material_1 = require("@mui/material");
const UILabel_1 = require("../../../../CommonUI/BuildlingBlocks/UILabel");
const ValueTableRuleDesigner = (props) => {
    const { ruleDefinition, editContext } = props;
    const [openSettingsLayer, setOpenSettingsLayer] = (0, react_1.useState)(false);
    const testContext = react_1.default.useContext(TestContext_1.TestContext);
    const isDefaultTestResult = !!testContext &&
        !testContext.isError &&
        !(testContext?.items || []).find((rowResult) => rowResult.result === "true");
    const onAddNewRow = () => {
        ruleDefinition.addRow();
        StateManager_1.StateManager.propertyChanged(ruleDefinition, "rows");
    };
    if (testContext) {
        return ((0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleEditorInTester, { children: [(0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleEditorRender, { columnCount: ruleDefinition.columns.length + ruleDefinition.resultColumns.length + 1, rowsGap: 6, className: "RuleEditorRender", children: [(0, jsx_runtime_1.jsx)(ValueTableRuleHeader_1.ValueRuleTableHeader, { ...props }), ruleDefinition.rows.map((row, idx) => {
                            return ((0, jsx_runtime_1.jsx)(ValueTableRow_1.XValueTableRow, { row: row, index: idx, ruleDefinition: ruleDefinition, editContext: editContext }, row._id));
                        }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyIndexColumn, { onClick: onAddNewRow, "data-testid": "btn-value-table-add-new-row" }), ruleDefinition.columns.map((_, idx) => ((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: [isDefaultTestResult && ((0, jsx_runtime_1.jsx)(CommonStyles_3.PassedTestRow, { "data-testid": "passedtestrow", x: ruleDefinition.columns.length + 3, y: ruleDefinition.rows.length + 4, columnsSpan: ruleDefinition.resultColumns.length })), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleLabelColumn, { colSpan: ruleDefinition.columns.length + 1, colStart: 1, y: ruleDefinition.rows.length + 4, children: "Default Result Values" }), ruleDefinition.resultColumns.map((rescol, index2) => {
                                    return ((0, jsx_runtime_1.jsx)(XResultCell_1.XResultCell, { ruleDefinition: ruleDefinition, resultColumn: rescol, resultCell: ruleDefinition.defaultResult[rescol._id], editContext: editContext, rowIndex: ruleDefinition.rows.length + 4, cellIndex: ruleDefinition.columns.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: ["columns", "resultColumns"], control: () => {
                        return ((0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleEditorRender, { columnCount: ruleDefinition.columns.length + ruleDefinition.resultColumns.length + 1, children: [(0, jsx_runtime_1.jsx)(ValueTableRuleHeader_1.ValueRuleTableHeader, { ...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)(ValueTableRow_1.XValueTableRow, { row: row, index: idx, ruleDefinition: ruleDefinition, editContext: editContext }, row._id));
                                            }) }));
                                    } }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyIndexColumn, { onClick: onAddNewRow, "data-testid": "btn-value-table-add-new-row", children: (0, jsx_runtime_1.jsx)(PlusGrayIcon_1.PlusGrayIcon, {}) }), ruleDefinition.columns.map((_, idx) => ((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.columns.length + 1, colStart: 1, children: "Default Result Values" }), ruleDefinition.resultColumns.map((rescol) => {
                                                    return ((0, jsx_runtime_1.jsx)(ValueTableResultDefaultCell_1.ValueTableResultDefaultCell, { ruleDefinition: ruleDefinition, resultColumn: rescol, editContext: editContext }, rescol._id));
                                                })] }));
                                    } })] }));
                    } })), sidebarWidth: 350, open: openSettingsLayer, onClose: () => setOpenSettingsLayer(false) }), (0, jsx_runtime_1.jsx)(ObserveMultiState_1.ObserveMultiState, { listenTo: [
                    ...props.ruleDefinition.columns,
                    ...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: editContext, control: () => {
                    return (0, jsx_runtime_1.jsx)(Notification_1.Notification, { editContext: editContext });
                } }), (0, jsx_runtime_1.jsx)(LinkingDataDialog_1.LinkingDataDialog, { editContext: editContext }), (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("VALUE_RULE_DELETE_CONFIRM", "true");
                                }
                                props.ruleDefinition.removeColumn(props.editContext.deletingColumn._id);
                                StateManager_1.StateManager.propertyChanged(props.ruleDefinition, "columns");
                                props.editContext.deletingColumn = undefined;
                            }, onCancel: () => (props.editContext.deletingColumn = undefined) }));
                    }
                    return null;
                } })] }));
};
exports.ValueTableRuleDesigner = ValueTableRuleDesigner;
//# sourceMappingURL=ValueTableRuleDesigner.js.map