import dayjs from '../../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.js';
import isSameOrAfter from '../../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/plugin/isSameOrAfter.js';
import isSameOrBefore from '../../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/plugin/isSameOrBefore.js';
import { EmptyFilterValue, FilterType } from '../../../types/filters.js';

dayjs.extend(isSameOrAfter);
dayjs.extend(isSameOrBefore);
const filterOperationsMap = {
    number: {
        eq: (row, columnId, filterValue, _addMeta) => {
            return Number(row.getValue(columnId)) === Number(filterValue.value);
        },
        neq: (row, columnId, filterValue, _addMeta) => {
            return Number(row.getValue(columnId)) !== Number(filterValue.value);
        },
        lt: (row, columnId, filterValue, _addMeta) => {
            return Number(row.getValue(columnId)) < Number(filterValue.value);
        },
        lte: (row, columnId, filterValue, _addMeta) => {
            return Number(row.getValue(columnId)) <= Number(filterValue.value);
        },
        gt: (row, columnId, filterValue, _addMeta) => {
            return Number(row.getValue(columnId)) > Number(filterValue.value);
        },
        gte: (row, columnId, filterValue, _addMeta) => {
            return Number(row.getValue(columnId)) >= Number(filterValue.value);
        }
    },
    string: {
        eq: (row, columnId, filterValue, _addMeta) => {
            return (String(row.getValue(columnId)).toLowerCase() ===
                String(filterValue.value).toLowerCase());
        },
        neq: (row, columnId, filterValue, _addMeta) => {
            return (String(row.getValue(columnId)).toLowerCase() !==
                String(filterValue.value).toLowerCase());
        },
        contains: (row, columnId, filterValue, _addMeta) => {
            const columnValue = row.getValue(columnId).toLowerCase();
            const filterStr = filterValue.value.toLowerCase();
            return columnValue.includes(filterStr);
        },
        starts_with: (row, columnId, filterValue, _addMeta) => {
            const columnValue = row.getValue(columnId).toLowerCase();
            const filterStr = filterValue.value.toLowerCase();
            return columnValue.startsWith(filterStr);
        },
        ends_with: (row, columnId, filterValue, _addMeta) => {
            const columnValue = row.getValue(columnId).toLowerCase();
            const filterStr = filterValue.value.toLowerCase();
            return columnValue.endsWith(filterStr);
        }
    },
    date: {
        eq: (row, columnId, filterValue, _addMeta) => {
            return dayjs(row.getValue(columnId)).isSame(dayjs(filterValue.date), 'day');
        },
        neq: (row, columnId, filterValue, _addMeta) => {
            return !dayjs(row.getValue(columnId)).isSame(dayjs(filterValue.date), 'day');
        },
        lt: (row, columnId, filterValue, _addMeta) => {
            return dayjs(row.getValue(columnId)).isBefore(dayjs(filterValue.date), 'day');
        },
        lte: (row, columnId, filterValue, _addMeta) => {
            return dayjs(row.getValue(columnId)).isSameOrBefore(dayjs(filterValue.date), 'day');
        },
        gt: (row, columnId, filterValue, _addMeta) => {
            return dayjs(row.getValue(columnId)).isAfter(dayjs(filterValue.date), 'day');
        },
        gte: (row, columnId, filterValue, _addMeta) => {
            return dayjs(row.getValue(columnId)).isSameOrAfter(dayjs(filterValue.date), 'day');
        }
    },
    select: {
        eq: (row, columnId, filterValue, _addMeta) => {
            if (String(filterValue.value) === EmptyFilterValue) {
                return row.getValue(columnId) === '';
            }
            // Select only supports string values
            return String(row.getValue(columnId)) === String(filterValue.value);
        },
        neq: (row, columnId, filterValue, _addMeta) => {
            if (String(filterValue.value) === EmptyFilterValue) {
                return row.getValue(columnId) !== '';
            }
            // Select only supports string values
            return String(row.getValue(columnId)) !== String(filterValue.value);
        }
    },
    multiselect: {
        in: (row, columnId, filterValue, _addMeta) => {
            if (!Array.isArray(filterValue.value))
                return false;
            return filterValue.value
                .map(value => (value === EmptyFilterValue ? '' : String(value)))
                .includes(String(row.getValue(columnId)));
        },
        notin: (row, columnId, filterValue, _addMeta) => {
            if (!Array.isArray(filterValue.value))
                return false;
            return !filterValue.value
                .map(value => (value === EmptyFilterValue ? '' : String(value)))
                .includes(String(row.getValue(columnId)));
        }
    }
};
function getFilterFn(type, operator) {
    // @ts-expect-error FilterOperatorTypes is union of all possible operators
    return filterOperationsMap[type][operator];
}
const handleStringBasedTypes = (filterType, value, operator) => {
    switch (filterType) {
        case FilterType.date:
            return {
                value,
                stringValue: value.toISOString()
            };
        case FilterType.select:
            return {
                stringValue: value === EmptyFilterValue ? '' : value,
                value
            };
        case FilterType.multiselect:
            return {
                value,
                stringValue: value
                    .map((value) => value === EmptyFilterValue ? '' : String(value))
                    .join()
            };
        case FilterType.string: {
            // Apply wildcards for ilike operations
            let processedValue = value;
            // Check if we need to apply wildcards (operator could be UI type or already converted to 'ilike')
            if (operator === 'contains') {
                processedValue = `%${value}%`;
            }
            else if (operator === 'starts_with') {
                processedValue = `${value}%`;
            }
            else if (operator === 'ends_with') {
                processedValue = `%${value}`;
            }
            else if (operator === 'ilike') {
                // If already converted to ilike, assume it needs contains-style wildcards
                // unless the value already has wildcards
                if (!value.includes('%')) {
                    processedValue = `%${value}%`;
                }
            }
            return {
                stringValue: processedValue,
                value
            };
        }
        default:
            return {
                stringValue: value,
                value
            };
    }
};
const getFilterOperator = ({ value, filterType, operator }) => {
    if (value === EmptyFilterValue && filterType === FilterType.select) {
        return 'empty';
    }
    // Map string filter operators to ilike for DataTableFilter
    if (filterType === FilterType.string &&
        (operator === 'contains' ||
            operator === 'starts_with' ||
            operator === 'ends_with')) {
        return 'ilike';
    }
    return operator;
};
const getFilterValue = ({ value, dataType = 'string', filterType = FilterType.string, operator }) => {
    if (dataType === 'boolean') {
        return { boolValue: value, value };
    }
    if (dataType === 'number') {
        return { numberValue: value, value };
    }
    // Handle string-based types
    return handleStringBasedTypes(filterType, value, operator);
};
const getDataType = ({ filterType = FilterType.string, dataType = 'string' }) => {
    switch (filterType) {
        case FilterType.multiselect:
        case FilterType.select:
            return dataType;
        case FilterType.date:
            return 'string';
        default:
            return filterType;
    }
};

export { filterOperationsMap, getDataType, getFilterFn, getFilterOperator, getFilterValue };
//# sourceMappingURL=filter-operations.js.map
