import { orderBy } from "natural-orderby";
import { useCallback, useEffect, useMemo, useState } from "react";
/**
 * Takes a list of items and returns a sorted list of items and a sort descriptor.
 * @param items - The list of items to sort.
 * @returns An object containing the sorted items and a sort handler func
 */
export function useSortTableItems(items) {
    const [sortedItems, setSortedItems] = useState(items);
    const [column, setColumn] = useState();
    const [direction, setDirection] = useState("asc");
    const sortHandler = useCallback((column, direction) => {
        setColumn(column);
        setDirection(direction);
    }, []);
    useEffect(() => {
        if (!items)
            return;
        if (!column) {
            setSortedItems(items);
            return;
        }
        setSortedItems(orderBy(items, [column], [direction]));
    }, [column, direction, items, sortHandler]);
    return useMemo(() => ({ sortedItems, sortHandler }), [sortedItems, sortHandler]);
}
//# sourceMappingURL=useSortTableItems.js.map