'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var react = require('react');
var ramda = require('ramda');
var reactUtils_useFuncDebounce_useFuncDebounce = require('../useFuncDebounce/useFuncDebounce.js');
var reactUtils_withImmutableActions_withImmutableActions = require('../withImmutableActions/withImmutableActions.js');
var zustand = require('zustand');

/** @type {import("neetocommons/react-utils").ZustandStoreHook} */
var useScrollStore = zustand.create(reactUtils_withImmutableActions_withImmutableActions(function (set, get) {
  return {
    scrollPositions: {},
    setScrollPosition: function setScrollPosition(key, value) {
      return set(function (state) {
        return {
          scrollPositions: ramda.assoc(key, value, state.scrollPositions)
        };
      });
    },
    removeScrollPosition: function removeScrollPosition(key) {
      return set(function (state) {
        return {
          scrollPositions: ramda.dissoc(key, state.scrollPositions)
        };
      });
    },
    getScrollPosition: function getScrollPosition(key) {
      return get().scrollPositions[key];
    }
  };
}));
var useRestoreScrollPosition = function useRestoreScrollPosition(key, ref) {
  var setScrollPosition = useScrollStore(ramda.prop("setScrollPosition"));
  var getScrollPosition = useScrollStore(ramda.prop("getScrollPosition"));
  var handleScroll = reactUtils_useFuncDebounce_useFuncDebounce(function () {
    if (!ref.current) return;
    setScrollPosition(key, ref.current.scrollTop);
  }, 500);
  react.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(ramda.prop("setScrollPosition"));
};
var useGetScrollPosition = function useGetScrollPosition() {
  return useScrollStore(ramda.prop("getScrollPosition"));
};
var useRemoveScrollPosition = function useRemoveScrollPosition() {
  return useScrollStore(ramda.prop("removeScrollPosition"));
};

exports.default = useRestoreScrollPosition;
exports.useGetScrollPosition = useGetScrollPosition;
exports.useRemoveScrollPosition = useRemoveScrollPosition;
exports.useSetScrollPosition = useSetScrollPosition;
//# sourceMappingURL=useRestoreScrollPosition.js.map
