"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityRulePredicatesEditor = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const HelpBlock_1 = tslib_1.__importDefault(require("../../../../components/HelpBlock"));
const AdaptableContext_1 = require("../../../AdaptableContext");
const ButtonInfo_1 = require("../../Buttons/ButtonInfo");
const DocumentationLinkConstants_1 = require("../../../../Utilities/Constants/DocumentationLinkConstants");
const SimpleButton_1 = tslib_1.__importDefault(require("../../../../components/SimpleButton"));
const Tag_1 = require("../../../../components/Tag");
const EntityRulePredicateEditor_1 = require("./EntityRulePredicateEditor");
const Radio_1 = tslib_1.__importDefault(require("../../../../components/Radio"));
const getScopeViewItems_1 = require("../../../../Utilities/getScopeViewItems");
const EntityRulePredicatesEditor = (props) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const showDocumentationLinks = api.internalApi.isDocumentationLinksDisplayed();
    const columnPredicateEnabled = props.data.Rule.Predicates.some((predicate) => predicate.ColumnId !== undefined);
    // filter out predicates that are already in use
    const defaultPredicateDefs = props.predicateDefs.filter((predicateDef) => {
        return props.data.Rule?.Predicates?.every((rulePrediate) => rulePrediate.PredicateId !== predicateDef.id);
    });
    const handlePredicateScopeTypeChange = (columnPredicateEnabled) => {
        if (columnPredicateEnabled) {
            props.onChange({
                ...props.data,
                Rule: {
                    Predicates: [
                        {
                            PredicateId: null,
                            ColumnId: '',
                        },
                    ],
                },
            });
        }
        else {
            props.onChange({
                ...props.data,
                Rule: {
                    Predicates: [],
                },
            });
        }
    };
    const handleAddNewPredicate = () => {
        props.onChange({
            ...props.data,
            Rule: {
                Predicates: [
                    ...(props.data.Rule?.Predicates || []),
                    {
                        PredicateId: defaultPredicateDefs[0].id,
                        Inputs: [],
                    },
                ],
            },
        });
    };
    const scopeString = (0, getScopeViewItems_1.getScopeViewItems)(props.data.Scope, api).values.join(', ');
    const enablePredicateColumnId = props.enablePredicateColumnId ?? true;
    return (React.createElement(React.Fragment, null,
        React.createElement(rebass_1.Flex, { mb: 2, justifyContent: "space-between" },
            React.createElement(rebass_1.Text, { fontSize: 2, mb: 2 }, props.descriptions.selectPredicate),
            React.createElement(SimpleButton_1.default, { disabled: !props.data.Rule?.Predicates?.length, onClick: handleAddNewPredicate, icon: "plus", variant: "raised" }, "Add Predicate")),
        enablePredicateColumnId && (React.createElement(rebass_1.Flex, { className: "ab-EntityRulePredicateEditor-ScopeTypeSelector", p: 2, mb: 2, flexDirection: "column" },
            React.createElement(rebass_1.Text, { mb: 2 }, "Create Predicate Rule(s) using"),
            React.createElement(Radio_1.default, { "data-name": "entity-scope", onClick: () => handlePredicateScopeTypeChange(false), mr: 3, checked: !columnPredicateEnabled },
                "Active Column:",
                ' ',
                React.createElement(Tag_1.Tag, { ml: 1, mr: 1 }, scopeString || 'Not Defined'),
                ' '),
            React.createElement(Radio_1.default, { "data-name": "column-scope", onClick: () => handlePredicateScopeTypeChange(true), checked: columnPredicateEnabled }, "Selected Columns (overriding Active Column)"))),
        (props.data.Rule?.Predicates?.length ? props.data.Rule.Predicates : [null]).map((predicate, index) => {
            const currentPredicatDef = props.predicateDefs.find((pd) => pd.id === predicate?.PredicateId);
            let editorPredicateDefs = defaultPredicateDefs;
            if (currentPredicatDef) {
                editorPredicateDefs = [currentPredicatDef, ...defaultPredicateDefs];
            }
            return (React.createElement(React.Fragment, { key: `${index}-${predicate?.PredicateId}` },
                index > 0 && (React.createElement(rebass_1.Box, null,
                    React.createElement(Tag_1.Tag, { mb: 3 }, "AND"))),
                React.createElement(EntityRulePredicateEditor_1.EntityRulePredicateEditor, { data: props.data, predicate: predicate, onChange: props.onChange, predicateDefs: editorPredicateDefs, getPredicateDefsForColId: props.getPredicateDefsForColId, columnPredicateEnabled: columnPredicateEnabled })));
        }),
        showDocumentationLinks && (React.createElement(HelpBlock_1.default, { "data-name": "query-documentation", mt: 3, mb: 2, style: {
                fontSize: 'var(--ab-font-size-3)',
                padding: 0,
            } },
            React.createElement(ButtonInfo_1.ButtonInfo, { mr: 2, onClick: () => window.open(DocumentationLinkConstants_1.PredicateDocsLink, '_blank') }),
            "See Predicate documentation for more details and examples"))));
};
exports.EntityRulePredicatesEditor = EntityRulePredicatesEditor;
