/*!
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project.
 */ import { SortDirection } from '@datashaper/schema';
import { useMemo } from 'react';
/**
 * Sort top level group headers if the table is grouped, there isn't any column sorted or
 * if the respective column is sorted
 * @param table -
 * @param column -
 * @param sort -
 * @param entries -
 * @returns RowObject[] | undefined
 */ export function useSortedGroups(table, column, sort = SortDirection.Ascending, entries) {
    return useMemo(()=>{
        if (!table.isGrouped()) return undefined;
        const firstColumn = table.groups().names[0];
        if (column && column !== firstColumn) return entries;
        return sort === SortDirection.Ascending ? entries?.sort() : entries?.sort().reverse();
    }, [
        column,
        entries,
        sort,
        table
    ]);
}
