import * as React from 'react';
import HelpBlock from '../../../../components/HelpBlock';
import { useAdaptable } from '../../../AdaptableContext';
import { ButtonInfo } from '../../Buttons/ButtonInfo';
import { PredicateDocsLink } from '../../../../Utilities/Constants/DocumentationLinkConstants';
import SimpleButton from '../../../../components/SimpleButton';
import { Tag } from '../../../../components/Tag';
import { EntityRulePredicateEditor } from './EntityRulePredicateEditor';
import Radio from '../../../../components/Radio';
import { getScopeViewItems } from '../../../../Utilities/getScopeViewItems';
import { Box, Flex } from '../../../../components/Flex';
export const EntityRulePredicatesEditor = (props) => {
    const { api } = 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 = getScopeViewItems(props.data.Scope, api).values.join(', ');
    const enablePredicateColumnId = props.enablePredicateColumnId ?? true;
    return (React.createElement(React.Fragment, null,
        React.createElement(Flex, { justifyContent: "space-between", className: "twa:mb-2" },
            React.createElement(Box, { className: "twa:text-2 twa:mb-2" }, props.descriptions.selectPredicate),
            React.createElement(SimpleButton, { disabled: !props.data.Rule?.Predicates?.length, onClick: handleAddNewPredicate, icon: "plus", variant: "raised" }, "Add Predicate")),
        enablePredicateColumnId && (React.createElement(Flex, { className: "ab-EntityRulePredicateEditor-ScopeTypeSelector twa:p-2 twa:mb-2", flexDirection: "column" },
            React.createElement(Box, { className: "twa:mb-2" }, "Create Predicate Rule(s) using"),
            React.createElement(Radio, { "data-name": "entity-scope", onClick: () => handlePredicateScopeTypeChange(false), className: "twa:mr-3", checked: !columnPredicateEnabled },
                "Active Column: ",
                React.createElement(Tag, { className: "twa:mx-1" }, scopeString || 'Not Defined'),
                ' '),
            React.createElement(Radio, { "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(Box, null,
                    React.createElement(Tag, { className: "twa:mb-3" }, "AND"))),
                React.createElement(EntityRulePredicateEditor, { data: props.data, predicate: predicate, onChange: props.onChange, predicateDefs: editorPredicateDefs, getPredicateDefsForColId: props.getPredicateDefsForColId, columnPredicateEnabled: columnPredicateEnabled })));
        }),
        showDocumentationLinks && (React.createElement(HelpBlock, { "data-name": "query-documentation", className: "twa:mt-3 twa:mb-2 twa:p-0 twa:text-3" },
            React.createElement(ButtonInfo, { className: "twa:mr-2", onClick: () => window.open(PredicateDocsLink, '_blank') }),
            "See Predicate documentation for more details and examples"))));
};
