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