import { useCallbackRef, useControlled } from '../utils';
const sortOrders = ['asc', 'desc', null];
const defaultSortOrder = sortOrders[0];
function getNextOrder(order) {
    const next = sortOrders.indexOf(order) + 1;
    return sortOrders[next] !== undefined ? sortOrders[next] : defaultSortOrder;
}
/** Handles controlled/uncontrolled sort of the Table rows. */
export default function useTable(props) {
    const { defaultSort = { key: '', order: defaultSortOrder }, onSortChange: onSortChangeProp, sort: value } = props;
    const [sort, onChange, { isControlled }] = useControlled({
        defaultValue: defaultSort,
        onChange: onSortChangeProp,
        value,
    });
    const onSortChange = useCallbackRef((key) => {
        const order = sort.key === key ? getNextOrder(sort.order) : defaultSortOrder;
        onChange({ key, order });
    });
    return { isControlled, onSortChange, sort };
}
//# sourceMappingURL=useTable.js.map