import * as React from 'react';
import FormLayout, { FormRow } from '../../../../components/FormLayout';
import SimpleButton from '../../../../components/SimpleButton';
import { useAdaptable } from '../../../AdaptableContext';
import { PredicateEditor } from '../../PredicateEditor/PredicateEditor';
import { Box, Flex } from '../../../../components/Flex';
import { Select } from '../../../../components/Select';
export const EntityRulePredicateEditor = (props) => {
    const { api } = useAdaptable();
    const { predicate } = props;
    const allColumns = React.useMemo(() => {
        return api.columnApi.getColumns().map((column) => ({
            label: column.friendlyName,
            value: column.columnId,
        }));
    }, []);
    const handleClear = () => {
        const newPredicates = props.data.Rule?.Predicates?.filter((prevPredicate) => prevPredicate !== predicate);
        props.onChange({
            ...props.data,
            Rule: {
                Predicates: newPredicates,
            },
        });
    };
    const handlePredicateChange = (newPredicateDef) => {
        let newPredicates = [];
        if (predicate) {
            newPredicates = props.data.Rule?.Predicates?.map((prevPredicate) => {
                if (prevPredicate === predicate) {
                    return newPredicateDef;
                }
                return prevPredicate;
            });
        }
        else {
            // no previous predicate, e.g. it was cleared
            newPredicates = [newPredicateDef];
        }
        props.onChange({
            ...props.data,
            Rule: {
                Predicates: newPredicates,
            },
        });
    };
    const predicateDefs = React.useMemo(() => {
        let preparedPredicates = [];
        if (props.getPredicateDefsForColId && props?.predicate?.ColumnId) {
            preparedPredicates = props.getPredicateDefsForColId(props.predicate.ColumnId);
        }
        else {
            preparedPredicates = props.predicateDefs;
        }
        return preparedPredicates;
    }, [props.predicate]);
    const columnIdPredicateScopeEnabled = Boolean(props.columnPredicateEnabled);
    const selectedColumn = allColumns.find((col) => col.value === predicate?.ColumnId);
    const columnOptions = allColumns.map((col) => ({
        label: col.label,
        value: col.value,
    }));
    // use global scope or custom scope
    let predicateColumnId = null;
    if (predicate?.ColumnId && predicate.ColumnId !== '') {
        predicateColumnId = predicate.ColumnId;
    }
    else if ('ColumnIds' in props.data.Scope && props.data.Scope?.ColumnIds?.[0]) {
        predicateColumnId = props.data.Scope?.ColumnIds?.[0];
    }
    return (React.createElement(Flex, { className: "ab-EntityRulePredicateEditor twa:p-3 twa:mb-3" },
        React.createElement(Box, { className: "twa:flex-1" },
            React.createElement(FormLayout, null,
                columnIdPredicateScopeEnabled && (React.createElement(FormRow, { label: "Column" },
                    React.createElement(Select, { placeholder: "Select Column", options: columnOptions, value: selectedColumn?.value, onChange: (value) => {
                            handlePredicateChange({ PredicateId: null, ColumnId: value, Inputs: [] });
                        } }))),
                React.createElement(FormRow, { label: "Predicate" },
                    React.createElement(PredicateEditor, { columnId: predicateColumnId, predicateDefs: predicateDefs, predicate: predicate, onChange: handlePredicateChange, onClear: handleClear })))),
        React.createElement(Box, null,
            React.createElement(SimpleButton, { disabled: !predicate, className: "twa:ml-2 twa:h-input", onClick: handleClear, icon: "delete" }))));
};
