import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/es.array.concat.js";
import "core-js/modules/es.array.for-each.js";
import "core-js/modules/es.array.map.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/web.dom-collections.for-each.js";
import React from 'react';
import useIsomorphicLayoutEffect from "../../hooks/useIsomorphicLayoutEffect";
var useUniformHeights = function useUniformHeights(itemsCount) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var _options$disabled = options.disabled,
    disabled = _options$disabled === void 0 ? false : _options$disabled;
  var _React$useState = React.useState(0),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    maxHeight = _React$useState2[0],
    setMaxHeight = _React$useState2[1];
  var refs = React.useRef([]);
  var observerRef = React.useRef(null);
  var maxHeightRef = React.useRef(maxHeight);
  var updateMaxHeight = React.useCallback(function () {
    if (disabled) {
      return;
    }
    var heights = refs.current.map(function (ref) {
      return (ref === null || ref === void 0 ? void 0 : ref.offsetHeight) || 0;
    });
    var newMaxHeight = Math.ceil(Math.max.apply(Math, _toConsumableArray(heights).concat([0])));
    if (newMaxHeight !== maxHeightRef.current) {
      maxHeightRef.current = newMaxHeight;
      setMaxHeight(newMaxHeight);
    }
  }, [disabled]);
  useIsomorphicLayoutEffect(function () {
    if (disabled) {
      return undefined;
    }
    if (!observerRef.current) {
      observerRef.current = new ResizeObserver(function () {
        requestAnimationFrame(function () {
          updateMaxHeight();
        });
      });
    }
    return function () {
      var _a;
      (_a = observerRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
    };
  }, [disabled, updateMaxHeight]);
  useIsomorphicLayoutEffect(function () {
    var _a;
    if (disabled) {
      (_a = observerRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
      maxHeightRef.current = 0;
      setMaxHeight(0);
      return;
    }
    var observer = observerRef.current;
    if (!observer) {
      return;
    }
    observer.disconnect();
    refs.current.forEach(function (ref) {
      if (ref) {
        observer.observe(ref);
      }
    });
    updateMaxHeight();
  }, [itemsCount, disabled, updateMaxHeight]);
  var getRef = React.useCallback(function (index) {
    return function (element) {
      var observer = observerRef.current;
      var oldElement = refs.current[index];
      if (oldElement && observer) {
        observer.unobserve(oldElement);
      }
      refs.current[index] = element;
      if (element && observer && !disabled) {
        observer.observe(element);
      }
    };
  }, [disabled]);
  return {
    maxHeight: maxHeight,
    getRef: getRef
  };
};
export default useUniformHeights;