'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');
var dataTable_types = require('../data-table.types.cjs');

dayjs_min.default.extend(isSameOrAfter.default);
dayjs_min.default.extend(isSameOrBefore.default);
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_min.default(row.getValue(columnId)).isSame(dayjs_min.default(filterValue.date), "day");
        },
        neq: (row, columnId, filterValue) => {
            return !dayjs_min.default(row.getValue(columnId)).isSame(dayjs_min.default(filterValue.date), "day");
        },
        lt: (row, columnId, filterValue) => {
            return dayjs_min.default(row.getValue(columnId)).isBefore(dayjs_min.default(filterValue.date), "day");
        },
        lte: (row, columnId, filterValue) => {
            return dayjs_min.default(row.getValue(columnId)).isSameOrBefore(dayjs_min.default(filterValue.date), "day");
        },
        gt: (row, columnId, filterValue) => {
            return dayjs_min.default(row.getValue(columnId)).isAfter(dayjs_min.default(filterValue.date), "day");
        },
        gte: (row, columnId, filterValue) => {
            return dayjs_min.default(row.getValue(columnId)).isSameOrAfter(dayjs_min.default(filterValue.date), "day");
        },
    },
    select: {
        eq: (row, columnId, filterValue) => {
            if (String(filterValue.value) === dataTable_types.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) === dataTable_types.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 filters.FilterType.date:
            return {
                value,
                stringValue: value.toISOString(),
            };
        case filters.FilterType.select:
            return {
                stringValue: value === dataTable_types.EmptyFilterValue ? "" : value,
                value,
            };
        default:
            return {
                stringValue: value,
                value,
            };
    }
};
const getFilterOperator = ({ value, filterType, operator, }) => {
    return value === dataTable_types.EmptyFilterValue && filterType === filters.FilterType.select
        ? "empty"
        : operator;
};
const getFilterValue = ({ value, dataType = "string", filterType = filters.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 = filters.FilterType.string, dataType = "string", }) => {
    switch (filterType) {
        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
