"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XRuleTableRow = void 0;
const tslib_1 = require("tslib");
const react_1 = require("react");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_2 = tslib_1.__importStar(require("react"));
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
const ConfirmDialog_1 = require("../../../../CommonUI/ConfirmDialog");
const ApplyIcon_1 = require("../../../../CommonUI/Icon/ApplyIcon");
const CloseRedIcon_1 = require("../../../../CommonUI/Icon/CloseRedIcon");
const DeleteGrayIcon_1 = require("../../../../CommonUI/Icon/DeleteGrayIcon");
const ObserveState_1 = require("../../../../CommonUI/StateManagement/ObserveState");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
const TestContext_1 = require("../../../../CommonUI/Testing/TestContext");
const CommonStyles_1 = require("../../../DesignUI/CommonStyles");
const TableRowTestContext_1 = require("@appglue/common/dist/Rule/Table/Common/TableRowTestContext");
const CommonStyles_2 = require("../../CommonStyles");
const XResultCell_1 = require("../../CommonUI/XResultCell");
const RuleTableRuleCell_1 = require("./RuleTableRuleCell");
const XRuleTableRow = (props) => {
    const { rowIndex, ruleDefinition, row, editContext } = props;
    const testContext = react_2.default.useContext(TestContext_1.TestContext);
    const rowtestitem = testContext?.findItem(row._id);
    const [openConfirmDialog, setOpenConfirmDialog] = (0, react_2.useState)(false);
    let testValue;
    if (rowtestitem && !rowtestitem.isError) {
        testValue = rowtestitem.result === "false" ? TableRowTestContext_1.ITableRowTestValue.FAILED : TableRowTestContext_1.ITableRowTestValue.PASSED;
    }
    return ((0, jsx_runtime_1.jsxs)(TableRowTestContext_1.TableRowTestContext.Provider, { value: testValue, children: [rowtestitem?.result === "true" ? ((0, jsx_runtime_1.jsx)(CommonStyles_2.PassedTestRow, { "data-testid": `passedtestrow`, columnsSpan: ruleDefinition.ruleColumns.length + ruleDefinition.resultColumns.length + 4, y: rowIndex + 2 })) : null, (0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleTableIndexColumn, { y: rowIndex + 2, x: 0, isTestPassed: testValue === TableRowTestContext_1.ITableRowTestValue.PASSED, children: [rowtestitem?.result === "true" ? (0, jsx_runtime_1.jsx)(ApplyIcon_1.ApplyIcon, {}) : null, rowtestitem?.result === "false" ? (0, jsx_runtime_1.jsx)(CloseRedIcon_1.CloseRedIcon, {}) : null, rowIndex + 1] }), ruleDefinition.ruleColumns.map((col, idx) => ((0, react_1.createElement)(RuleTableRuleCell_1.RuleTableRuleCell, { cell: row.ruleCells[col._id], rule: col, ...props, key: idx, cellIndex: idx, rowIndex: rowIndex }))), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, { isTestPassed: testValue === TableRowTestContext_1.ITableRowTestValue.PASSED, y: rowIndex + 2, x: ruleDefinition.ruleColumns.length + 1 }), ruleDefinition.resultColumns.map((col, index2) => {
                return ((0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: col, control: () => ((0, jsx_runtime_1.jsx)(XResultCell_1.XResultCell, { ruleDefinition: ruleDefinition, editContext: editContext, resultCell: row.resultCellsData[col._id], resultColumn: col, rowIndex: rowIndex + 2, cellIndex: ruleDefinition.ruleColumns.length + 2 + index2 }, col._id)) }, col._id));
            }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, { y: rowIndex + 2, x: ruleDefinition.ruleColumns.length + ruleDefinition.resultColumns.length + 2 }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableHeaderActionColumn, { y: rowIndex + 2, x: ruleDefinition.ruleColumns.length + ruleDefinition.resultColumns.length + 3, onClick: (event) => {
                    if (testContext)
                        return;
                    event.stopPropagation();
                    if (DataUtilities_1.DataUtilities.getCookie("RULE_TABLE_ROW_DELETE") !== "true") {
                        setOpenConfirmDialog(true);
                    }
                    else {
                        ruleDefinition.removeRow(row);
                        StateManager_1.StateManager.propertyChanged(ruleDefinition, "rows");
                    }
                }, children: !testContext && (0, jsx_runtime_1.jsx)(DeleteGrayIcon_1.DeleteGrayIcon, {}) }), !testContext && ((0, jsx_runtime_1.jsx)(ConfirmDialog_1.ConfirmDialog, { caption: "this row", open: openConfirmDialog, onConfirm: (dontAskAgain) => {
                    if (dontAskAgain) {
                        DataUtilities_1.DataUtilities.setCookieValue("RULE_TABLE_ROW_DELETE", "true");
                    }
                    ruleDefinition.removeRow(row);
                    StateManager_1.StateManager.propertyChanged(ruleDefinition, "rows");
                    setOpenConfirmDialog(false);
                }, onCancel: () => setOpenConfirmDialog(false) }))] }));
};
exports.XRuleTableRow = XRuleTableRow;
//# sourceMappingURL=RuleTableRow.js.map