"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XValueTableRow = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = 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 ValueTableRuleCell_1 = require("./ValueTableRuleCell");
const XValueTableRow = ({ ruleDefinition, row, index, editContext }) => {
    const testContext = react_1.default.useContext(TestContext_1.TestContext);
    const rowtestitem = testContext?.findItem(row._id);
    let testValue;
    const [openConfirmDialog, setOpenConfirmDialog] = (0, react_1.useState)(false);
    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.columns.length + ruleDefinition.resultColumns.length + 4, y: index + 3 })) : null, (0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleTableIndexColumn, { y: index + 3, 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, index + 1] }), row.cells.map((cell, index2) => ((0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: cell, properties: ["dataType"], control: () => ((0, jsx_runtime_1.jsx)(ValueTableRuleCell_1.XValueTableRuleCell, { ruleDefinition: ruleDefinition, cell: cell, editContext: editContext, rowIndex: index, cellIndex: index2 })) }, index2))), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, { isTestPassed: testValue === TableRowTestContext_1.ITableRowTestValue.PASSED, y: index + 3, x: ruleDefinition.columns.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, { editContext: editContext, resultColumn: col, resultCell: row.resultCellsData[col._id], ruleDefinition: ruleDefinition, rowIndex: index + 3, cellIndex: ruleDefinition.columns.length + 2 + index2 })) }, col._id));
            }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableEmptyAddColumn, { y: index + 3, x: ruleDefinition.columns.length + ruleDefinition.resultColumns.length + 2 }), (0, jsx_runtime_1.jsx)(CommonStyles_1.RuleTableHeaderActionColumn, { y: index + 3, x: ruleDefinition.columns.length + ruleDefinition.resultColumns.length + 3, onClick: (event) => {
                    if (testContext)
                        return null;
                    event.stopPropagation();
                    if (DataUtilities_1.DataUtilities.getCookie("VALUE_TABLE_ROW_DELETE") !== "true") {
                        setOpenConfirmDialog(true);
                    }
                    else {
                        ruleDefinition.removeRow(row);
                        StateManager_1.StateManager.propertyChanged(ruleDefinition, "rows");
                    }
                }, children: testContext ? null : (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("VALUE_TABLE_ROW_DELETE", "true");
                    }
                    ruleDefinition.removeRow(row);
                    StateManager_1.StateManager.propertyChanged(ruleDefinition, "rows");
                    setOpenConfirmDialog(false);
                }, onCancel: () => setOpenConfirmDialog(false) }))] }));
};
exports.XValueTableRow = XValueTableRow;
//# sourceMappingURL=ValueTableRow.js.map