import DateUtils from './DateUtils';
const enter = (form, setFilter, event, onClose, emptyPageable) => {
    // 'keypress' event misbehaves on mobile so we track 'Enter' key via 'keydown' event
    if (event.key === 'Enter') {
        event.preventDefault();
        event.stopPropagation();
        apply(form, setFilter, onClose, emptyPageable);
    }
};
const apply = (form, setFilter, onClose, emptyPageable) => {
    setFilter({ ...form.getValues(), ...emptyPageable });
    onClose();
};
const clear = (form, setFilter, onClose, clearValues) => {
    reset(form, setFilter, clearValues);
    onClose();
};
const reset = (form, setFilter, clearValues) => {
    form.reset(clearValues);
    setFilter({
        ...clearValues,
        seed: DateUtils.millis(),
    });
};
const isSelected = (filter) => {
    const clone = JSON.parse(JSON.stringify(filter));
    delete clone.page;
    delete clone.size;
    delete clone.sort;
    delete clone.seed;
    return !isObjectEmpty(clone);
};
const isObjectEmpty = (filter) => {
    if (!filter || Object.keys(filter).length === 0)
        return true;
    let empty = false;
    Object.keys(filter).forEach((key) => {
        if (Array.isArray(filter[key])) {
            if (filter[key].length === 0)
                empty = true;
        }
        else {
            if (!filter[key])
                empty = true;
        }
    });
    return empty;
};
const FilterUtils = {
    apply,
    clear,
    isSelected,
    reset,
    enter,
};
export default FilterUtils;
