"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FloatingFilter = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const OverlayTrigger_1 = tslib_1.__importDefault(require("../../../components/OverlayTrigger"));
const SimpleButton_1 = tslib_1.__importDefault(require("../../../components/SimpleButton"));
const AdaptableContext_1 = require("../../AdaptableContext");
const AdaptableIconComponent_1 = require("../AdaptableIconComponent");
const ColumnFilterMenu_1 = require("./components/ColumnFilterMenu");
const FloatingFilterInputList_1 = require("./components/FloatingFilterInputList");
const utils_1 = require("./utils");
const FloatingFilter = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const columnFilterOptions = adaptable.adaptableOptions.filterOptions.columnFilterOptions;
    /**
     * We always have a syntetic predicate with AND or OR, so the logic is easyer.
     * The wrapper is in charge of constructing the correct column filter.
     */
    const isMultiple = props.predicate?.args?.length > 1;
    const isManualApply = !columnFilterOptions.autoApplyColumnFilter;
    const isInlineEditable = !isMultiple && !isManualApply;
    // only used when there is only one filter selected
    let singleFilterPredicateDef = null;
    if (props.predicate.args.length === 1) {
        singleFilterPredicateDef = props.predicateDefs.find((predicateDef) => predicateDef.operator === props.predicate.args[0].operator);
    }
    const noInputs = !singleFilterPredicateDef?.inputs || singleFilterPredicateDef.inputs.length === 0;
    const showLabel = isMultiple || noInputs || isManualApply;
    let label = (0, utils_1.qlPredicateToString)(props.predicate, props.predicateDefs);
    if (isManualApply &&
        singleFilterPredicateDef &&
        (0, utils_1.isPredicateEmpty)(props.predicate.args[0], singleFilterPredicateDef)) {
        label = '';
    }
    const showQuickFilterDropdown = adaptable.api.filterApi.columnFilterApi.internalApi.showQuickFilterDropdown(props.columnId);
    const handleClear = () => props.onClear?.();
    const showEvent = 'click';
    const hideEvent = 'blur';
    let filterDropdown = null;
    let filterDropdownButton = (React.createElement(SimpleButton_1.default, { variant: "text", "data-name": "floating-filter-button", "data-value": props.predicate?.args[0]?.operator, onClick: () => {
            if (!isInlineEditable) {
                adaptable.api.filterApi.columnFilterApi.internalApi.openColumnFilterPopup(props.columnId);
            }
        }, style: {
            textAlign: 'left',
            marginRight: 1, // just so that the focus outline is not cut off
            ...(!isInlineEditable
                ? {
                    minWidth: 0,
                    flex: 1,
                }
                : {}),
        } },
        React.createElement(rebass_1.Box, null, !isManualApply && singleFilterPredicateDef?.icon ? (singleFilterPredicateDef?.icon) : (React.createElement(AdaptableIconComponent_1.AdaptableIconComponent, { icon: { name: 'filter' } }))),
        showLabel && (React.createElement(rebass_1.Box, { ml: 2, className: "ab-FloatingFilter-label", title: label, flex: 1 }, label))));
    if (isInlineEditable) {
        filterDropdown = showQuickFilterDropdown && (React.createElement(OverlayTrigger_1.default, { className: "ab-FloatingFilter-overlay", showEvent: showEvent, hideEvent: hideEvent, preventPortalEventPropagation: showEvent === 'click', targetOffset: 10, hideDelay: 50, "data-name": "floating-filter-overlay", render: () => {
                // we render this only for single filter
                return (React.createElement(ColumnFilterMenu_1.ColumnFilterMenu, { columnId: props.columnId, disabled: props.disabled, predicate: props.predicate.args[0], predicateDefs: props.predicateDefs, onPredicateChange: (predicate) => {
                        props.onPredicateChange({
                            operator: props.predicate.operator,
                            args: [predicate],
                        });
                    } }));
            } }, filterDropdownButton));
    }
    else {
        filterDropdown = filterDropdownButton;
    }
    return (React.createElement(rebass_1.Flex, { className: "ab-FloatingFilter", style: { width: '100%' } },
        filterDropdown,
        isInlineEditable && (React.createElement(rebass_1.Flex, { flex: 1, minWidth: 0 },
            React.createElement(FloatingFilterInputList_1.FloatingFilterInputList, { onKeyDown: props.onKeydown, columnId: props.columnId, disabled: props.disabled, 
                // It works only with a predicate
                predicate: props.predicate.args[0], predicateDefs: props.predicateDefs, onPredicateChange: (predicate) => {
                    props.onPredicateChange({
                        operator: props.predicate.operator,
                        args: [predicate],
                    });
                } }))),
        isInlineEditable && (React.createElement(SimpleButton_1.default, { px: 0, onClick: () => {
                adaptable.api.filterApi.columnFilterApi.internalApi.openColumnFilterPopup(props.columnId, {
                    onChange: props.onChange,
                });
            }, "data-name": "expand-filter", icon: "expand", variant: "text" })),
        (isMultiple ||
            !(0, utils_1.isPredicateEmpty)(props.predicate.args[0], singleFilterPredicateDef)) && (React.createElement(SimpleButton_1.default, { "data-name": "clear-filter", p: 0, variant: "text", onClick: handleClear, icon: "close" }))));
};
exports.FloatingFilter = FloatingFilter;
