"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuleTableRuleCell = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importDefault(require("react"));
const ExpressionView_1 = require("../../../../Expressions/Builder/ExpressionView");
const CommonStyles_1 = require("../../../DesignUI/CommonStyles");
const Error_1 = require("../../CommonUI/Error");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
const ObserveMultiState_1 = require("../../../../CommonUI/StateManagement/ObserveMultiState");
const TestContext_1 = require("../../../../CommonUI/Testing/TestContext");
const IDesignValidationProvider_1 = require("@appglue/common/dist/Common/IDesignValidationProvider");
const TestingConstants_1 = require("../../../../CommonUI/Testing/TestingConstants");
const ExpressionTestResultColorContext_1 = require("../../../../Expressions/Testing/ExpressionTestResultColorContext");
const ExpressionReactContext_1 = require("../../../../Expressions/Structure/ExpressionReactContext");
const UIStyleConstants_1 = require("@appglue/common/dist/Common/UIStyleConstants");
// need to fix for selected
function RuleTableRuleCell(props) {
    const { cell, ruleDefinition, rule, rowIndex, cellIndex } = props;
    const testContext = react_1.default.useContext(TestContext_1.TestContext);
    const row = ruleDefinition.rows[rowIndex];
    const rowTestResult = testContext?.findItem(row._id);
    const cellTestResult = rowTestResult?.findItem(`${row._id}-${cell._columnId}`);
    if (testContext) {
        let issues = [];
        if (cellTestResult?.isError) {
            issues = [new IDesignValidationProvider_1.ValidationIssue(cellTestResult?.result || "")];
        }
        let testcolor;
        let borderColor;
        let backgroundColor;
        if (cellTestResult?.result === "true") {
            testcolor = TestingConstants_1.TestingConstants.TEXT_SUCCESS;
            borderColor = TestingConstants_1.TestingConstants.BORDER_SUCCESS;
        }
        else if (cellTestResult?.result === "false") {
            testcolor = TestingConstants_1.TestingConstants.TEXT_FAIL;
            borderColor = TestingConstants_1.TestingConstants.BORDER_FAIL;
        }
        if (cellTestResult?.isError) {
            backgroundColor = TestingConstants_1.TestingConstants.BACKGROUND_FAIL;
            borderColor = TestingConstants_1.TestingConstants.BORDER_FAIL;
        }
        return ((0, jsx_runtime_1.jsx)(TestContext_1.TestContext.Provider, { value: cellTestResult, children: (0, jsx_runtime_1.jsx)(ExpressionTestResultColorContext_1.ExpressionTestResultColorContext.Provider, { value: testcolor, children: (0, jsx_runtime_1.jsx)(ExpressionReactContext_1.ExpressionReactContext.Provider, { value: ExpressionReactContext_1.expressionSettings[ExpressionReactContext_1.ExpressionRenderType.TEXT], children: (0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleTableRuleColumn, { selected: false, x: cellIndex + 1, y: rowIndex + 2, backgroundColor: backgroundColor, borderColor: borderColor, children: [(0, jsx_runtime_1.jsx)(ExpressionView_1.ExpressionView, { expression: cell.expression, hideValidation: true, color: {
                                    expression: UIStyleConstants_1.UIStyleConstants.BLUE_COLOR,
                                    value: UIStyleConstants_1.UIStyleConstants.GRAY_DARK_COLOR,
                                    variable: UIStyleConstants_1.UIStyleConstants.GRAY_DARK_COLOR,
                                    missing: UIStyleConstants_1.UIStyleConstants.GRAY_COLOR,
                                } }), (0, jsx_runtime_1.jsx)(Error_1.Error, { issues: issues })] }) }) }) }));
    }
    return ((0, jsx_runtime_1.jsx)(ObserveMultiState_1.ObserveMultiState, { listenTo: [rule, cell], control: () => {
            let issues = ruleDefinition.getRuleCellValidationIssues(cell);
            return ((0, jsx_runtime_1.jsxs)(CommonStyles_1.RuleTableRuleColumn, { selected: false, x: cellIndex + 1, y: rowIndex + 2, children: [(0, jsx_runtime_1.jsx)(ExpressionView_1.ExpressionView, { expression: cell.expression, hideValidation: true, color: {
                            expression: UIStyleConstants_1.UIStyleConstants.BLUE_COLOR,
                            value: UIStyleConstants_1.UIStyleConstants.GRAY_DARK_COLOR,
                            variable: UIStyleConstants_1.UIStyleConstants.GRAY_DARK_COLOR,
                            missing: "rgba(39, 41, 50, 0.3)",
                        }, onDone: () => {
                            StateManager_1.StateManager.changed(cell);
                            StateManager_1.StateManager.changed(ruleDefinition);
                        } }), (0, jsx_runtime_1.jsx)(Error_1.Error, { issues: issues })] }));
        } }));
}
exports.RuleTableRuleCell = RuleTableRuleCell;
//# sourceMappingURL=RuleTableRuleCell.js.map