/*!
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project.
 */ import { SortDirection } from '@datashaper/schema';
import { useCallback, useState } from 'react';
export function useSortHandling(allowSorting, defaultSortColumn, defaultSortDirection = SortDirection.Ascending) {
    const [sortColumn, setSortColumn] = useState(defaultSortColumn);
    const [sortDirection, setSortDirection] = useState(defaultSortDirection);
    const onSort = useCallback((_evt, column)=>{
        if (allowSorting) {
            if (column?.isSorted || !sortDirection) {
                setSortDirection(sortDirection === SortDirection.Ascending ? SortDirection.Descending : sortDirection === SortDirection.Descending ? undefined : SortDirection.Ascending);
            }
            setSortColumn(column?.fieldName);
        }
    }, [
        allowSorting,
        setSortColumn,
        setSortDirection,
        sortDirection
    ]);
    return {
        sortColumn,
        sortDirection,
        onSort
    };
}
