import * as React from 'react';
import { useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import * as LayoutRedux from '../../../Redux/ActionsReducers/LayoutRedux';
import { getCurrentLayoutSelector } from '../../../Redux/ActionsReducers/LayoutRedux';
import { mapColumnDataTypeToExpressionFunctionType } from '../../../Utilities/adaptableQlUtils';
import { useAdaptable } from '../../AdaptableContext';
import { mapAdaptablePredicateDefToQlPredicateDef, mapColumnFilterToQlPredicate, mapQlPredicateToAdaptablePredicate, } from './utils';
export const useAdaptableFilterWrapper = (columnId, filterComponent, handleOnChangeOverride) => {
    const adaptable = useAdaptable();
    const dispatch = useDispatch();
    const api = adaptable.api;
    const columnType = api.columnApi.getColumnDataTypeForColumnId(columnId);
    const column = api.columnApi.getColumnWithColumnId(columnId);
    const columnFilterOptions = api.optionsApi.getFilterOptions().columnFilterOptions;
    const qlPredicateDefs = React.useMemo(() => {
        const predicateDefs = api.filterApi.columnFilterApi.getFilterPredicateDefsForColumn(column);
        const qlDataType = mapColumnDataTypeToExpressionFunctionType(columnType);
        return predicateDefs.map((predDef) => mapAdaptablePredicateDefToQlPredicateDef(predDef, qlDataType));
    }, [columnId]);
    const currentLayout = useSelector(getCurrentLayoutSelector);
    const columnFilter = useMemo(() => {
        return currentLayout?.ColumnFilters?.find((cf) => cf.ColumnId === columnId);
    }, [currentLayout?.ColumnFilters, columnId]);
    const qlPredicate = column
        ? mapColumnFilterToQlPredicate(columnFilter, column, qlPredicateDefs, columnFilterOptions, filterComponent, api)
        : null;
    const handleOnChange = (adaptablePredicate, logic = 'AND') => {
        // make sure we always have the latest columnFiler
        const columnFilter = adaptable.api.filterApi.columnFilterApi.getColumnFilterForColumn(columnId);
        const newFilter = {
            ...columnFilter,
            ColumnId: columnId,
            Predicates: adaptablePredicate,
            PredicatesOperator: logic,
        };
        if (handleOnChangeOverride) {
            handleOnChangeOverride(newFilter);
        }
        else {
            dispatch(columnFilter?.Uuid
                ? LayoutRedux.LayoutColumnFilterEdit(newFilter)
                : LayoutRedux.LayoutColumnFilterAdd(newFilter));
        }
    };
    const handleClear = React.useCallback(() => dispatch(LayoutRedux.LayoutColumnFilterClear(columnId)), [columnId]);
    /**
     * Converts a QlPredicate to an AdaptablePredicate and dispatches the change.
     */
    const handlePredicateChange = React.useCallback((newPredicate) => {
        if (!newPredicate) {
            handleClear();
            return;
        }
        const newAdaptablePredicates = newPredicate.args.map(mapQlPredicateToAdaptablePredicate);
        handleOnChange(newAdaptablePredicates, newPredicate.operator);
    }, [columnId]);
    return {
        qlPredicate,
        qlPredicateDefs,
        columnFilter,
        handleClear,
        handlePredicateChange,
        handleOnChange,
        column,
    };
};
