'use strict';

var dayjs_min = require('../../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.cjs');
var isSameOrAfter = require('../../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/plugin/isSameOrAfter.cjs');
var isSameOrBefore = require('../../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/plugin/isSameOrBefore.cjs');
var filters = require('../../../types/filters.cjs');

dayjs_min.default.extend(isSameOrAfter.default);
dayjs_min.default.extend(isSameOrBefore.default);
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_min.default(row.getValue(columnId)).isSame(dayjs_min.default(filterValue.date), 'day');
        },
        neq: (row, columnId, filterValue, _addMeta) => {
            return !dayjs_min.default(row.getValue(columnId)).isSame(dayjs_min.default(filterValue.date), 'day');
        },
        lt: (row, columnId, filterValue, _addMeta) => {
            return dayjs_min.default(row.getValue(columnId)).isBefore(dayjs_min.default(filterValue.date), 'day');
        },
        lte: (row, columnId, filterValue, _addMeta) => {
            return dayjs_min.default(row.getValue(columnId)).isSameOrBefore(dayjs_min.default(filterValue.date), 'day');
        },
        gt: (row, columnId, filterValue, _addMeta) => {
            return dayjs_min.default(row.getValue(columnId)).isAfter(dayjs_min.default(filterValue.date), 'day');
        },
        gte: (row, columnId, filterValue, _addMeta) => {
            return dayjs_min.default(row.getValue(columnId)).isSameOrAfter(dayjs_min.default(filterValue.date), 'day');
        }
    },
    select: {
        eq: (row, columnId, filterValue, _addMeta) => {
            if (String(filterValue.value) === filters.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) === filters.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 === filters.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 === filters.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 filters.FilterType.date:
            return {
                value,
                stringValue: value.toISOString()
            };
        case filters.FilterType.select:
            return {
                stringValue: value === filters.EmptyFilterValue ? '' : value,
                value
            };
        case filters.FilterType.multiselect:
            return {
                value,
                stringValue: value
                    .map((value) => value === filters.EmptyFilterValue ? '' : String(value))
                    .join()
            };
        case filters.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 === filters.EmptyFilterValue && filterType === filters.FilterType.select) {
        return 'empty';
    }
    // Map string filter operators to ilike for DataTableFilter
    if (filterType === filters.FilterType.string &&
        (operator === 'contains' ||
            operator === 'starts_with' ||
            operator === 'ends_with')) {
        return 'ilike';
    }
    return operator;
};
const getFilterValue = ({ value, dataType = 'string', filterType = filters.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 = filters.FilterType.string, dataType = 'string' }) => {
    switch (filterType) {
        case filters.FilterType.multiselect:
        case filters.FilterType.select:
            return dataType;
        case filters.FilterType.date:
            return 'string';
        default:
            return filterType;
    }
};

exports.filterOperationsMap = filterOperationsMap;
exports.getDataType = getDataType;
exports.getFilterFn = getFilterFn;
exports.getFilterOperator = getFilterOperator;
exports.getFilterValue = getFilterValue;
//# sourceMappingURL=filter-operations.cjs.map
