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 { FilterType } from '../../../types/filters.js';
import { EmptyFilterValue } from '../data-table.types.js';

dayjs.extend(isSameOrAfter);
dayjs.extend(isSameOrBefore);
const filterOperationsMap = {
    number: {
        eq: (row, columnId, filterValue) => {
            return Number(row.getValue(columnId)) === Number(filterValue.value);
        },
        neq: (row, columnId, filterValue) => {
            return Number(row.getValue(columnId)) !== Number(filterValue.value);
        },
        lt: (row, columnId, filterValue) => {
            return Number(row.getValue(columnId)) < Number(filterValue.value);
        },
        lte: (row, columnId, filterValue) => {
            return Number(row.getValue(columnId)) <= Number(filterValue.value);
        },
        gt: (row, columnId, filterValue) => {
            return Number(row.getValue(columnId)) > Number(filterValue.value);
        },
        gte: (row, columnId, filterValue) => {
            return Number(row.getValue(columnId)) >= Number(filterValue.value);
        },
    },
    string: {
        eq: (row, columnId, filterValue) => {
            return (String(row.getValue(columnId)).toLowerCase() ===
                String(filterValue.value).toLowerCase());
        },
        neq: (row, columnId, filterValue) => {
            return (String(row.getValue(columnId)).toLowerCase() !==
                String(filterValue.value).toLowerCase());
        },
        like: (row, columnId, filterValue) => {
            const columnValue = row.getValue(columnId).toLowerCase();
            const filterStr = filterValue.value.toLowerCase();
            return columnValue.includes(filterStr);
        },
    },
    date: {
        eq: (row, columnId, filterValue) => {
            return dayjs(row.getValue(columnId)).isSame(dayjs(filterValue.date), "day");
        },
        neq: (row, columnId, filterValue) => {
            return !dayjs(row.getValue(columnId)).isSame(dayjs(filterValue.date), "day");
        },
        lt: (row, columnId, filterValue) => {
            return dayjs(row.getValue(columnId)).isBefore(dayjs(filterValue.date), "day");
        },
        lte: (row, columnId, filterValue) => {
            return dayjs(row.getValue(columnId)).isSameOrBefore(dayjs(filterValue.date), "day");
        },
        gt: (row, columnId, filterValue) => {
            return dayjs(row.getValue(columnId)).isAfter(dayjs(filterValue.date), "day");
        },
        gte: (row, columnId, filterValue) => {
            return dayjs(row.getValue(columnId)).isSameOrAfter(dayjs(filterValue.date), "day");
        },
    },
    select: {
        eq: (row, columnId, filterValue) => {
            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) => {
            if (String(filterValue.value) === EmptyFilterValue) {
                return row.getValue(columnId) !== "";
            }
            // Select only supports string values
            return String(row.getValue(columnId)) !== String(filterValue.value);
        },
    },
};
function getFilterFn(type, operator) {
    // @ts-expect-error FilterOperatorTypes is union of all possible operators
    return filterOperationsMap[type][operator];
}
const handleStringBasedTypes = (filterType, value) => {
    switch (filterType) {
        case FilterType.date:
            return {
                value,
                stringValue: value.toISOString(),
            };
        case FilterType.select:
            return {
                stringValue: value === EmptyFilterValue ? "" : value,
                value,
            };
        default:
            return {
                stringValue: value,
                value,
            };
    }
};
const getFilterOperator = ({ value, filterType, operator, }) => {
    return value === EmptyFilterValue && filterType === FilterType.select
        ? "empty"
        : operator;
};
const getFilterValue = ({ value, dataType = "string", filterType = FilterType.string, }) => {
    if (dataType === "boolean") {
        return { boolValue: value, value };
    }
    if (dataType === "number") {
        return { numberValue: value, value };
    }
    // Handle string-based types
    return handleStringBasedTypes(filterType, value);
};
const getDataType = ({ filterType = FilterType.string, dataType = "string", }) => {
    switch (filterType) {
        case FilterType.select:
            return dataType;
        case FilterType.date:
            return "string";
        default:
            return filterType;
    }
};

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