"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAdaptableFilterWrapper = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const react_redux_1 = require("react-redux");
const LayoutRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/LayoutRedux"));
const LayoutRedux_1 = require("../../../Redux/ActionsReducers/LayoutRedux");
const adaptableQlUtils_1 = require("../../../Utilities/adaptableQlUtils");
const AdaptableContext_1 = require("../../AdaptableContext");
const utils_1 = require("./utils");
const useAdaptableFilterWrapper = (columnId, filterComponent, handleOnChangeOverride) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const dispatch = (0, react_redux_1.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 = (0, adaptableQlUtils_1.mapColumnDataTypeToExpressionFunctionType)(columnType);
        return predicateDefs.map((predDef) => (0, utils_1.mapAdaptablePredicateDefToQlPredicateDef)(predDef, qlDataType));
    }, [columnId]);
    const currentLayout = (0, react_redux_1.useSelector)(LayoutRedux_1.getCurrentLayoutSelector);
    const columnFilter = (0, react_1.useMemo)(() => {
        return currentLayout?.ColumnFilters?.find((cf) => cf.ColumnId === columnId);
    }, [currentLayout?.ColumnFilters, columnId]);
    const qlPredicate = column
        ? (0, utils_1.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(utils_1.mapQlPredicateToAdaptablePredicate);
        handleOnChange(newAdaptablePredicates, newPredicate.operator);
    }, [columnId]);
    return {
        qlPredicate,
        qlPredicateDefs,
        columnFilter,
        handleClear,
        handlePredicateChange,
        handleOnChange,
        column,
    };
};
exports.useAdaptableFilterWrapper = useAdaptableFilterWrapper;
