import { useCallback } from "react";
import { normalizeInitialFilters } from "./normalizeInitialFilters.js";
import { useSessionStorageState } from "@contember/react-utils";
const useFilters = (columns, updatePaging, dataGridKey) => {
  const [filters, setFilters] = useSessionStorageState(`${dataGridKey}-filters`, (val) => normalizeInitialFilters(val, columns));
  return [
    filters,
    useCallback((columnKey, columnFilter) => {
      const column = columns.get(columnKey);
      if (column === void 0 || column.enableFiltering === false) {
        return;
      }
      let didBailOut = false;
      setFilters((filters2) => {
        const { [columnKey]: existingValue, ...otherFilters } = filters2;
        if (existingValue === columnFilter) {
          didBailOut = true;
          return filters2;
        }
        if (columnFilter === void 0) {
          return otherFilters;
        } else {
          return { ...otherFilters, [columnKey]: columnFilter };
        }
      });
      if (!didBailOut) {
        updatePaging({
          type: "goToFirstPage"
        });
      }
    }, [columns, setFilters, updatePaging])
  ];
};
export { useFilters };
//# sourceMappingURL=useFilters.js.map
