'use strict';

var dataTable_types = require('../data-table.types.cjs');
var filters = require('../../../types/filters.cjs');
var filterOperations = require('./filter-operations.cjs');

function queryToTableState(query) {
    const columnFilters = query.filters
        ?.filter((data) => data.value !== "")
        ?.map((data) => {
        const valueObject = data._type === filters.FilterType.date
            ? { date: data.value }
            : { value: data.value };
        return {
            value: valueObject,
            id: data?.name,
        };
    }) || [];
    const sorting = query.sort?.map((data) => ({
        id: data?.name,
        desc: data?.order === dataTable_types.SortOrders.DESC,
    }));
    return {
        columnFilters: columnFilters,
        sorting: sorting,
        globalFilter: query.search,
    };
}
function getColumnsWithFilterFn(columns = [], filters$1 = []) {
    return columns.map((column) => {
        const colFilter = filters$1?.find((filter) => filter.name === column.accessorKey);
        const filterFn = colFilter?.operator
            ? filterOperations.getFilterFn(column.filterType || filters.FilterType.string, colFilter.operator)
            : undefined;
        return {
            ...column,
            filterFn,
        };
    });
}
function groupData(data, group_by, columns = []) {
    if (!data)
        return [];
    if (!group_by || group_by === dataTable_types.defaultGroupOption.id)
        return data;
    const groupMap = new Map();
    data.forEach((currentData) => {
        const item = currentData;
        const keyValue = item[group_by];
        if (!groupMap.has(keyValue)) {
            groupMap.set(keyValue, []);
        }
        groupMap.get(keyValue)?.push(item);
    });
    const columnDef = columns.find((col) => col.accessorKey === group_by);
    const showGroupCount = columnDef?.showGroupCount || false;
    const groupLablesMap = columnDef?.groupLabelsMap || {};
    const groupCountMap = columnDef?.groupCountMap || {};
    const groupedData = [];
    groupMap.forEach((value, key) => {
        groupedData.push({
            label: groupLablesMap[key] || key,
            group_key: key,
            subRows: value,
            count: groupCountMap[key] ?? value.length,
            showGroupCount,
        });
    });
    return groupedData;
}
const generateFilterMap = (filters$1 = []) => {
    return new Map(filters$1
        ?.filter((data) => data._type === filters.FilterType.select || data.value !== "")
        .map(({ name, operator, value }) => [`${name}-${operator}`, value]));
};
const generateSortMap = (sort = []) => {
    return new Map(sort.map(({ name, order }) => [name, order]));
};
const isFilterChanged = (oldFilters = [], newFilters = []) => {
    const oldFilterMap = generateFilterMap(oldFilters);
    const newFilterMap = generateFilterMap(newFilters);
    if (oldFilterMap.size !== newFilterMap.size)
        return true;
    return [...newFilterMap].some(([key, value]) => oldFilterMap.get(key) !== value);
};
const isSortChanged = (oldSort = [], newSort = []) => {
    if (oldSort.length !== newSort.length)
        return true;
    const oldSortMap = generateSortMap(oldSort);
    const newSortMap = generateSortMap(newSort);
    return [...newSortMap].some(([key, order]) => oldSortMap.get(key) !== order);
};
const isGroupChanged = (oldGroupBy = [], newGroupBy = []) => {
    if (oldGroupBy.length !== newGroupBy.length)
        return true;
    const oldGroupSet = new Set(oldGroupBy);
    return newGroupBy.some((item) => !oldGroupSet.has(item));
};
const isSearchChanged = (oldSearch, newSearch) => {
    return oldSearch !== newSearch;
};
const hasQueryChanged = (oldQuery, newQuery) => {
    if (!oldQuery)
        return true;
    return (isFilterChanged(oldQuery.filters, newQuery.filters) ||
        isSortChanged(oldQuery.sort, newQuery.sort) ||
        isGroupChanged(oldQuery.group_by, newQuery.group_by) ||
        isSearchChanged(oldQuery.search, newQuery.search));
};
function getInitialColumnVisibility(columns = []) {
    return columns.reduce((acc, col) => {
        return {
            ...acc,
            [col.accessorKey]: col.defaultHidden ? false : true,
        };
    }, {});
}
function sanitizeTableQuery(query) {
    const { group_by = [], filters: filters$1 = [], sort = [], ...rest } = query;
    const sanitizedGroupBy = group_by?.filter((key) => key !== dataTable_types.defaultGroupOption.id);
    const sanitizedFilters = filters$1
        ?.filter((data) => data._type === filters.FilterType.select || data.value !== "")
        ?.map((data) => ({
        name: data.name,
        operator: filterOperations.getFilterOperator({
            operator: data.operator,
            value: data.value,
            filterType: data._type,
        }),
        ...filterOperations.getFilterValue({
            value: data.value,
            filterType: data._type,
            dataType: data._dataType,
        }),
    })) || [];
    return {
        ...rest,
        sort: sort,
        group_by: sanitizedGroupBy,
        filters: sanitizedFilters,
    };
}
function getDefaultTableQuery(defaultSort, oldQuery = {}) {
    return {
        sort: [defaultSort],
        group_by: [dataTable_types.defaultGroupOption.id],
        ...oldQuery,
    };
}

exports.getColumnsWithFilterFn = getColumnsWithFilterFn;
exports.getDefaultTableQuery = getDefaultTableQuery;
exports.getInitialColumnVisibility = getInitialColumnVisibility;
exports.groupData = groupData;
exports.hasQueryChanged = hasQueryChanged;
exports.queryToTableState = queryToTableState;
exports.sanitizeTableQuery = sanitizeTableQuery;
//# sourceMappingURL=index.cjs.map
