'use strict';

var _ = require('lodash');
var React = require('react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var ___default = /*#__PURE__*/_interopDefault(_);

const transformToArray = (stringOrArray) => {
  return stringOrArray instanceof Array ? stringOrArray : [stringOrArray];
};
const onResizeElem = (element, callback) => {
  const els = transformToArray(element);
  const data = {
    els,
    offsetWidth: els.map((el) => el.offsetWidth),
    offsetHeight: els.map((el) => el.offsetHeight),
    callback
  };
  const checkForChanges = () => {
    const nextOffsetWidth = data.els.map((el) => el.offsetWidth);
    const nextOffsetHeight = data.els.map((el) => el.offsetWidth);
    if (!___default.default.isEqual(nextOffsetWidth, data.offsetWidth) || ___default.default.isEqual(nextOffsetHeight, data.offsetHeight)) {
      data.offsetWidth = nextOffsetWidth;
      data.offsetHeight = nextOffsetHeight;
      data.callback();
    }
  };
  const mutationObserver = new MutationObserver(checkForChanges);
  mutationObserver.observe(document.body, {
    attributes: true,
    subtree: true
  });
  window.addEventListener("resize", checkForChanges);
  const disconnect = () => {
    window.removeEventListener("resize", checkForChanges);
    mutationObserver.disconnect();
  };
  return disconnect;
};
const useElementResize = (klass, cb, depend) => {
  const klasses = transformToArray(klass);
  React.useEffect(() => {
    let els = [];
    if (typeof klass === "object" && "current" in klass) {
      els = klass.current ? [klass.current] : [];
    } else {
      klasses.forEach((klass2) => {
        const arr = Array.from(
          document.querySelectorAll(klass2)
        );
        if (arr.length > 0) {
          els = [...els, ...arr];
        }
      });
    }
    if (!els.length)
      return;
    if (typeof ResizeObserver === "function") {
      const resizeObserver = new ResizeObserver(cb);
      els.forEach((el) => {
        resizeObserver.observe(el);
      });
      return () => resizeObserver.disconnect();
    } else {
      return onResizeElem(Array.from(els), cb);
    }
  }, [cb, ...depend || []]);
};
var useElementResize$1 = useElementResize;

module.exports = useElementResize$1;
