"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../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. */
function useTable(props) {
    const { defaultSort = { key: '', order: defaultSortOrder }, onSortChange: onSortChangeProp, sort: value } = props;
    const [sort, onChange, { isControlled }] = (0, utils_1.useControlled)({
        defaultValue: defaultSort,
        onChange: onSortChangeProp,
        value,
    });
    const onSortChange = (0, utils_1.useCallbackRef)((key) => {
        const order = sort.key === key ? getNextOrder(sort.order) : defaultSortOrder;
        onChange({ key, order });
    });
    return { isControlled, onSortChange, sort };
}
exports.default = useTable;
//# sourceMappingURL=useTable.js.map