"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FloatingFilterInputList = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const React = tslib_1.__importStar(require("react"));
const utils_1 = require("../../../../components/ExpressionEditor/QueryBuilder/utils");
const adaptableQlUtils_1 = require("../../../../Utilities/adaptableQlUtils");
const AdaptableContext_1 = require("../../../AdaptableContext");
const hooks_1 = require("../hooks");
const ColumnFilterInput_1 = require("./ColumnFilterInput");
const FloatingFilterValues_1 = require("./FloatingFilterValues");
const FloatingFilterInputList = (props) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const columnType = React.useMemo(() => {
        return api.columnApi.getColumnDataTypeForColumnId(props.columnId);
    }, [props.columnId]);
    const predicateDef = (0, hooks_1.usePredicateDef)(props.predicate?.operator, props.predicateDefs);
    const fnTypeInput = (0, adaptableQlUtils_1.mapColumnDataTypeToExpressionFunctionType)(columnType);
    const matchingInputs = predicateDef
        ? (0, utils_1.getOperatorMatchingInputs)(fnTypeInput, predicateDef?.inputs, true)
        : [];
    const debouncedPredicateChange = React.useCallback((0, lodash_1.debounce)((predicate) => {
        props.onPredicateChange(predicate);
    }, 300), [props.predicate, props.columnId]);
    if (['date[]', 'number[]', 'text[]'].includes(matchingInputs[0])) {
        return (React.createElement(FloatingFilterValues_1.FloatingFilterValues, { inline: true, columnId: props.columnId, disabled: props.disabled, selectProps: {
                menuStyle: {
                    left: -20, // the size of the "in" filter icon
                },
            }, onClear: () => {
                props.onPredicateChange({ operator: props.predicate.operator, args: [] });
            }, onChange: (inputs) => {
                props.onPredicateChange({
                    operator: props.predicate.operator,
                    args: inputs,
                });
            }, value: props.predicate.args, 
            // are not known ql operators for now
            // should change when porting, it has knoladge about predicates
            type: props.predicate.operator === 'In' ? 'In' : 'NotIn' }));
    }
    return (React.createElement(React.Fragment, null, (matchingInputs ?? []).map((input, index) => {
        const value = props?.predicate?.args?.[index];
        return (React.createElement(ColumnFilterInput_1.ColumnFilterInput, { filterType: "floating", columnId: props.columnId, usePrimitiveInputs: true, key: index, disabled: props.disabled, type: input, value: value, onKeyDown: props.onKeyDown, onClear: () => {
                const args = [...props.predicate.args];
                args.splice(index, 1);
                const newPredicate = {
                    operator: props.predicate.operator,
                    args: args,
                };
                debouncedPredicateChange(newPredicate);
            }, onChange: (value) => {
                const newPredicate = {
                    operator: props.predicate.operator,
                    args: [...props.predicate.args],
                };
                newPredicate.args[index] = value;
                debouncedPredicateChange(newPredicate);
            } }));
    })));
};
exports.FloatingFilterInputList = FloatingFilterInputList;
