"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XValueTableRuleColumnHeader = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importDefault(require("react"));
const styled_components_1 = tslib_1.__importDefault(require("styled-components"));
const ObserveState_1 = require("../../../../CommonUI/StateManagement/ObserveState");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
const ExpressionView_1 = require("../../../../Expressions/Builder/ExpressionView");
const CommonStyles_1 = require("../../CommonStyles");
const DeleteGrayIcon_1 = require("../../../../CommonUI/Icon/DeleteGrayIcon");
const Error_1 = require("../../CommonUI/Error");
const DataTypePicker_1 = require("../../../../CommonUI/DataTypePicker");
const TestContext_1 = require("../../../../CommonUI/Testing/TestContext");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
const UIStyleConstants_1 = require("@appglue/common/dist/Common/UIStyleConstants");
const XValueTableRuleColumnHeader = (props) => {
    const { ruleColumn, editContext, ruleDefinition } = props;
    const testContext = react_1.default.useContext(TestContext_1.TestContext);
    const onRemove = (event) => {
        event.stopPropagation();
        if (testContext)
            return;
        if (ruleColumn.expression.isEmpty || DataUtilities_1.DataUtilities.getCookie("VALUE_RULE_DELETE_CONFIRM") === "true") {
            ruleDefinition.removeColumn(ruleColumn._id);
            StateManager_1.StateManager.propertyChanged(ruleDefinition, "columns");
        }
        else {
            editContext.deletingColumn = ruleColumn;
        }
    };
    const onClickSetVariableDialog = (event) => {
        event.stopPropagation();
        editContext.selectedColForVariable = ruleColumn;
    };
    const onClickSetRuleDialog = (event) => {
        event.stopPropagation();
        editContext.selectedColForRule = ruleColumn;
    };
    return ((0, jsx_runtime_1.jsx)(RuleTableHeaderDiv, { children: (0, jsx_runtime_1.jsx)(ObserveState_1.ObserveState, { listenTo: ruleColumn, properties: ["columnName", "getRule", "cellData", "expression"], control: () => {
                let validationIssues = ruleColumn.getDesignValidationIssues();
                return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsxs)(ValueTableRuleColumnRender, { onClick: onClickSetVariableDialog, children: [(0, jsx_runtime_1.jsx)(ExpressionRender, { onClick: onClickSetRuleDialog, children: (0, jsx_runtime_1.jsx)(TestContext_1.TestContext.Provider, { value: undefined, children: (0, jsx_runtime_1.jsx)(ExpressionView_1.ExpressionView, { expression: ruleColumn.expression, 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.GREEN_LIGHT_GRAY_COLOR,
                                        }, hideValidation: true, onDone: () => {
                                            StateManager_1.StateManager.propertyChanged(ruleColumn, "expression");
                                        } }) }) }), (0, jsx_runtime_1.jsx)(Error_1.Error, { issues: validationIssues }), (0, jsx_runtime_1.jsx)(CommonStyles_1.EditColumnWrapper, { className: "edit-wrapper", children: ruleDefinition.columns.length > 1 && !testContext ? ((0, jsx_runtime_1.jsx)(CommonStyles_1.EditColumnAction, { onClick: onRemove, children: (0, jsx_runtime_1.jsx)(DeleteGrayIcon_1.DeleteGrayIcon, {}) })) : null }), (0, jsx_runtime_1.jsx)(CommonStyles_1.ActionWrapper, { children: testContext ? ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: ruleColumn.cellData.type })) : ((0, jsx_runtime_1.jsx)(CommonStyles_1.StyledDataTypeRender, { children: (0, jsx_runtime_1.jsx)(DataTypePicker_1.DataTypePicker, { dataDef: ruleColumn.cellData, updateCallback: (type) => {
                                            ruleDefinition.setRuleColumnType(ruleColumn, type);
                                            StateManager_1.StateManager.propertyChanged(ruleColumn, "cellData");
                                        } }) })) })] }) }));
            } }) }));
};
exports.XValueTableRuleColumnHeader = XValueTableRuleColumnHeader;
const RuleTableHeaderDiv = styled_components_1.default.div `
    display: flex;
    flex-direction: row;
    position: relative;
`;
const ValueTableRuleColumnRender = styled_components_1.default.div `
    position: relative;
    min-width: 120px;
    min-height: 44px;

    width: 100%;
    height: 100%;

    background: #ffffff;
    border: 1px solid #4050a6;
    box-sizing: border-box;
    border-radius: 2px;

    font-size: 12px;
    font-style: normal;
    font-weight: 400;
    line-height: 17px;
    letter-spacing: 0px;
    text-align: left;
    color: rgba(39, 41, 50);

    display: flex;
    flex-direction: column;
    justify-content: center;

    .edit-button,
    .edit-wrapper {
        opacity: 0;
    }

    &:hover {
        .edit-button,
        .edit-wrapper {
            opacity: 1;
        }
    }
`;
const ExpressionRender = styled_components_1.default.div `
    color: #4050a6;
    cursor: pointer;

    padding-top: 15px;
`;
//# sourceMappingURL=ValueTableRuleColumnHeader.js.map