import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { XExpressionView } from "../../../../Expressions/XExpressionView";
import { RuleTableRuleColumn } from "../../../DesignUI/CommonStyles";
import { Error } from "../../CommonUI/Error";
import { StateManager } from "../../../../CommonUI/StateManagement/StateManager";
import { ObserveMultiState } from "../../../../CommonUI/StateManagement/ObserveMultiState";
import { TestContext } from "../../../../Tester/TestContext";
import { ValidationIssue } from "../../../../Common/IDesignValidationProvider";
import { TestingConstants } from "../../../../CommonUI/Testing/TestingConstants";
import { ExpressionTestResultColorContext } from "../../../../Expressions/Test/ExpressionTestResultColorContext";
import { ExpressionContext, ExpressionRenderType, expressionSettings } from "../../../../Expressions/ExpressionContext";
// need to fix for selected
export function RuleTableRuleCell(props) {
    const { cell, ruleDefinition, rule, rowIndex, cellIndex } = props;
    const testContext = React.useContext(TestContext);
    const row = ruleDefinition.rows[rowIndex];
    const rowTestResult = testContext === null || testContext === void 0 ? void 0 : testContext.findItem(row._id);
    const cellTestResult = rowTestResult === null || rowTestResult === void 0 ? void 0 : rowTestResult.findItem(`${row._id}-${cell._columnId}`);
    if (testContext) {
        let issues = [];
        if (cellTestResult === null || cellTestResult === void 0 ? void 0 : cellTestResult.isError) {
            issues = [new ValidationIssue((cellTestResult === null || cellTestResult === void 0 ? void 0 : cellTestResult.result) || "")];
        }
        let testcolor;
        let borderColor;
        let backgroundColor;
        if ((cellTestResult === null || cellTestResult === void 0 ? void 0 : cellTestResult.result) === "true") {
            testcolor = TestingConstants.TEXT_SUCCESS;
            borderColor = TestingConstants.BORDER_SUCCESS;
        }
        else if ((cellTestResult === null || cellTestResult === void 0 ? void 0 : cellTestResult.result) === "false") {
            testcolor = TestingConstants.TEXT_FAIL;
            borderColor = TestingConstants.BORDER_FAIL;
        }
        if (cellTestResult === null || cellTestResult === void 0 ? void 0 : cellTestResult.isError) {
            backgroundColor = TestingConstants.BACKGROUND_FAIL;
            borderColor = TestingConstants.BORDER_FAIL;
        }
        return (_jsx(TestContext.Provider, Object.assign({ value: cellTestResult }, { children: _jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: testcolor }, { children: _jsx(ExpressionContext.Provider, Object.assign({ value: expressionSettings[ExpressionRenderType.TEXT] }, { children: _jsxs(RuleTableRuleColumn, Object.assign({ selected: false, x: cellIndex + 1, y: rowIndex + 2, backgroundColor: backgroundColor, borderColor: borderColor }, { children: [_jsx(XExpressionView, { expression: cell.expression, hideValidation: true, color: {
                                    expression: "#4050A6",
                                    value: "#272932",
                                    variable: "#272932",
                                    missing: "rgba(39, 41, 50, 0.3)",
                                } }, void 0),
                            _jsx(Error, { issues: issues }, void 0)] }), void 0) }), void 0) }), void 0) }), void 0));
    }
    return (_jsx(ObserveMultiState, { listenTo: [rule, cell], control: () => {
            let issues = ruleDefinition.getRuleCellValidationIssues(cell);
            return (_jsxs(RuleTableRuleColumn, Object.assign({ selected: false, x: cellIndex + 1, y: rowIndex + 2 }, { children: [_jsx(XExpressionView, { expression: cell.expression, hideValidation: true, color: {
                            expression: "#4050A6",
                            value: "#272932",
                            variable: "#272932",
                            missing: "rgba(39, 41, 50, 0.3)",
                        }, onDone: () => {
                            StateManager.changed(cell);
                            StateManager.changed(ruleDefinition);
                        } }, void 0),
                    _jsx(Error, { issues: issues }, void 0)] }), void 0));
        } }, void 0));
}
//# sourceMappingURL=RuleTableRuleCell.js.map