import { useCallback } from "react";
import { normalizeInitialOrderBys } from "./normalizeInitialOrderBys.js";
import { useSessionStorageState } from "@contember/react-utils";
const useOrderBys = (columns, updatePaging, dataGridKey) => {
  const [orderBys, setOrderBys] = useSessionStorageState(`${dataGridKey}-orderBy`, (val) => normalizeInitialOrderBys(val, columns));
  return [
    orderBys,
    useCallback((columnKey, columnOrderBy, append) => {
      const column = columns.get(columnKey);
      if (column === void 0 || column.enableOrdering === false) {
        return;
      }
      let didBailOut = false;
      setOrderBys((orderBys2) => {
        const existingValue = orderBys2[columnKey];
        if (existingValue === columnOrderBy) {
          didBailOut = true;
          return orderBys2;
        }
        if (columnOrderBy === null) {
          const { [columnKey]: _, ...rest } = orderBys2;
          return rest;
        }
        return append ? { ...orderBys2, [columnKey]: columnOrderBy } : { [columnKey]: columnOrderBy };
      });
      if (!didBailOut) {
        updatePaging({
          type: "goToFirstPage"
        });
      }
    }, [columns, setOrderBys, updatePaging])
  ];
};
export { useOrderBys };
//# sourceMappingURL=useOrderBys.js.map
