import { useEffect } from 'react';
import { dissoc, assoc, prop } from 'ramda';
import useFuncDebounce from '../useFuncDebounce/useFuncDebounce.js';
import withImmutableActions from '../withImmutableActions/withImmutableActions.js';
import { create } from 'zustand';

/** @type {import("neetocommons/react-utils").ZustandStoreHook} */
var useScrollStore = create(withImmutableActions(function (set, get) {
  return {
    scrollPositions: {},
    setScrollPosition: function setScrollPosition(key, value) {
      return set(function (state) {
        return {
          scrollPositions: assoc(key, value, state.scrollPositions)
        };
      });
    },
    removeScrollPosition: function removeScrollPosition(key) {
      return set(function (state) {
        return {
          scrollPositions: dissoc(key, state.scrollPositions)
        };
      });
    },
    getScrollPosition: function getScrollPosition(key) {
      return get().scrollPositions[key];
    }
  };
}));
var useRestoreScrollPosition = function useRestoreScrollPosition(key, ref) {
  var setScrollPosition = useScrollStore(prop("setScrollPosition"));
  var getScrollPosition = useScrollStore(prop("getScrollPosition"));
  var handleScroll = useFuncDebounce(function () {
    if (!ref.current) return;
    setScrollPosition(key, ref.current.scrollTop);
  }, 500);
  useEffect(function () {
    if (ref.current) {
      var savedScrollPosition = getScrollPosition(key);
      if (savedScrollPosition !== undefined) {
        ref.current.scrollTop = savedScrollPosition;
      }
      ref.current.addEventListener("scroll", handleScroll);
    }
    return function () {
      if (ref.current) {
        ref.current.removeEventListener("scroll", handleScroll);
      }
    };
  }, [key]);
};
var useSetScrollPosition = function useSetScrollPosition() {
  return useScrollStore(prop("setScrollPosition"));
};
var useGetScrollPosition = function useGetScrollPosition() {
  return useScrollStore(prop("getScrollPosition"));
};
var useRemoveScrollPosition = function useRemoveScrollPosition() {
  return useScrollStore(prop("removeScrollPosition"));
};

export { useRestoreScrollPosition as default, useGetScrollPosition, useRemoveScrollPosition, useSetScrollPosition };
//# sourceMappingURL=useRestoreScrollPosition.js.map
