"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnValuesSelect = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const StringExtensions_1 = require("../../../Utilities/Extensions/StringExtensions");
const date_fns_1 = require("date-fns");
const DateHelper_1 = require("../../../Utilities/Helpers/DateHelper");
const join_1 = tslib_1.__importDefault(require("../../../components/utils/join"));
const Select_1 = require("../../../components/Select");
const baseClassName = 'ab-ListBoxFilterForm';
const ColumnValuesSelect = (props) => {
    const column = props.column;
    const selectedColumnValues = props.value || [];
    const value = [];
    const options = props.options.filter((distinctValue, index) => {
        let isActive = selectedColumnValues.indexOf(distinctValue.value) >= 0;
        // special case for date objects, need to check against string values
        if (!isActive && distinctValue.value && distinctValue.value instanceof Date) {
            isActive = selectedColumnValues.some((dateStr) => (0, date_fns_1.isEqual)((0, DateHelper_1.parseDateValue)(dateStr), (0, DateHelper_1.parseDateValue)(distinctValue.value)));
        }
        const columnLabel = distinctValue.label;
        if (StringExtensions_1.StringExtensions.IsNullOrEmpty(columnLabel)) {
            return false;
        }
        if (isActive) {
            value.push(distinctValue.value);
        }
        return true;
    });
    const menuStyle = React.useMemo(() => {
        return {
            minWidth: `var(--ab-cmp-select-column-menu-${column.columnId}__min-width, var(--ab-cmp-select-column-menu__min-width, 160px))`,
        };
    }, [column.columnId]);
    const component = (React.createElement(Select_1.Select, { isMulti: true, showHeaderSelectionCheckbox: true, searchable: true, closeMenuOnSelect: false, menuStyle: menuStyle, ...props.selectProps, options: options, value: value, isLoading: props.isLoading, onChange: props.onChange }));
    return (React.createElement("div", { className: (0, join_1.default)(baseClassName, props.isLoading && `${baseClassName}--loading`, !value.length && `${baseClassName}--empty`), onKeyDownCapture: (e) => {
            const event = e.nativeEvent || e;
            event.stopPropagation = () => {
                // see #ensure-keyboard-navigation-works-in-column-values-filter
                // we want to prevent ag grid from stopping the propagation of the event
                // so that the event can be handled by the select component and thus makes keyboard navigation work
                //
                // the solution is to reassign the stopPropagation method to a function that does nothing
            };
        } }, component));
};
exports.ColumnValuesSelect = ColumnValuesSelect;
