"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnFiltering = void 0;
exports.shouldAutoRemoveFilter = shouldAutoRemoveFilter;
const filterFns_1 = require("../filterFns");
const utils_1 = require("../utils");
//
exports.ColumnFiltering = {
    getDefaultColumnDef: () => {
        return {
            filterFn: 'auto',
        };
    },
    getInitialState: (state) => {
        return {
            columnFilters: [],
            ...state,
        };
    },
    getDefaultOptions: (table) => {
        const options = {
            onColumnFiltersChange: (0, utils_1.makeStateUpdater)('columnFilters', table),
            filterFromLeafRows: false,
            maxLeafRowFilterDepth: 100,
        };
        return options;
    },
    createColumn: (column, table) => {
        column.getAutoFilterFn = () => {
            const firstRow = table.getCoreRowModel().flatRows[0];
            const value = firstRow === null || firstRow === void 0 ? void 0 : firstRow.getValue(column.id);
            if (typeof value === 'string') {
                return filterFns_1.filterFns.includesString;
            }
            if (typeof value === 'number') {
                return filterFns_1.filterFns.inNumberRange;
            }
            if (typeof value === 'boolean') {
                return filterFns_1.filterFns.equals;
            }
            if (value !== null && typeof value === 'object') {
                return filterFns_1.filterFns.equals;
            }
            if (Array.isArray(value)) {
                return filterFns_1.filterFns.arrIncludes;
            }
            return filterFns_1.filterFns.weakEquals;
        };
        column.getFilterFn = () => {
            var _a, _b;
            if ((0, utils_1.isFunction)(column.columnDef.filterFn)) {
                return column.columnDef.filterFn;
            }
            if (column.columnDef.filterFn === 'auto') {
                return column.getAutoFilterFn();
            }
            // @ts-ignore 兼容 table.options.filterFns 与 filterFns 的联合类型
            return ((_b = (_a = table.options.filterFns) === null || _a === void 0 ? void 0 : _a[column.columnDef.filterFn]) !== null && _b !== void 0 ? _b : filterFns_1.filterFns[column.columnDef.filterFn]);
        };
        column.getCanFilter = () => {
            var _a, _b, _c;
            return (((_a = column.columnDef.enableColumnFilter) !== null && _a !== void 0 ? _a : true) &&
                ((_b = table.options.enableColumnFilters) !== null && _b !== void 0 ? _b : true) &&
                ((_c = table.options.enableFilters) !== null && _c !== void 0 ? _c : true) &&
                !!column.accessorFn);
        };
        column.getIsFiltered = () => column.getFilterIndex() > -1;
        column.getFilterValue = () => { var _a, _b; return (_b = (_a = table.getState().columnFilters) === null || _a === void 0 ? void 0 : _a.find((d) => d.id === column.id)) === null || _b === void 0 ? void 0 : _b.value; };
        column.getFilterIndex = () => { var _a, _b; return (_b = (_a = table.getState().columnFilters) === null || _a === void 0 ? void 0 : _a.findIndex((d) => d.id === column.id)) !== null && _b !== void 0 ? _b : -1; };
        column.setFilterValue = (value) => {
            table.setColumnFilters((old) => {
                var _a, _b;
                const filterFn = column.getFilterFn();
                const previousFilter = old === null || old === void 0 ? void 0 : old.find((d) => d.id === column.id);
                const newFilter = (0, utils_1.functionalUpdate)(value, previousFilter ? previousFilter.value : undefined);
                //
                if (shouldAutoRemoveFilter(filterFn, newFilter, column)) {
                    return (_a = old === null || old === void 0 ? void 0 : old.filter((d) => d.id !== column.id)) !== null && _a !== void 0 ? _a : [];
                }
                const newFilterObj = { id: column.id, value: newFilter };
                if (previousFilter) {
                    return ((_b = old === null || old === void 0 ? void 0 : old.map((d) => {
                        if (d.id === column.id) {
                            return newFilterObj;
                        }
                        return d;
                    })) !== null && _b !== void 0 ? _b : []);
                }
                if (old === null || old === void 0 ? void 0 : old.length) {
                    return [...old, newFilterObj];
                }
                return [newFilterObj];
            });
        };
    },
    // eslint-disable-next-line @typescript-eslint/no-unused-vars -- 接口约定保留 table 参数
    createRow: (row, _table) => {
        row.columnFilters = {};
        row.columnFiltersMeta = {};
    },
    createTable: (table) => {
        table.setColumnFilters = (updater) => {
            var _a, _b;
            const leafColumns = table.getAllLeafColumns();
            const updateFn = (old) => {
                var _a;
                return (_a = (0, utils_1.functionalUpdate)(updater, old)) === null || _a === void 0 ? void 0 : _a.filter((filter) => {
                    const column = leafColumns.find((d) => d.id === filter.id);
                    if (column) {
                        const filterFn = column.getFilterFn();
                        if (shouldAutoRemoveFilter(filterFn, filter.value, column)) {
                            return false;
                        }
                    }
                    return true;
                });
            };
            (_b = (_a = table.options).onColumnFiltersChange) === null || _b === void 0 ? void 0 : _b.call(_a, updateFn);
        };
        table.resetColumnFilters = (defaultState) => {
            var _a, _b;
            table.setColumnFilters(defaultState ? [] : ((_b = (_a = table.initialState) === null || _a === void 0 ? void 0 : _a.columnFilters) !== null && _b !== void 0 ? _b : []));
        };
        table.getPreFilteredRowModel = () => table.getCoreRowModel();
        table.getFilteredRowModel = () => {
            if (!table._getFilteredRowModel && table.options.getFilteredRowModel) {
                table._getFilteredRowModel = table.options.getFilteredRowModel(table);
            }
            if (table.options.manualFiltering || !table._getFilteredRowModel) {
                return table.getPreFilteredRowModel();
            }
            return table._getFilteredRowModel();
        };
    },
};
function shouldAutoRemoveFilter(filterFn, value, column) {
    return ((filterFn && filterFn.autoRemove ? filterFn.autoRemove(value, column) : false) ||
        typeof value === 'undefined' ||
        (typeof value === 'string' && !value));
}
