import _Reflect$construct from "@babel/runtime-corejs3/core-js-stable/reflect/construct";
import _classCallCheck from "@babel/runtime-corejs3/helpers/classCallCheck";
import _createClass from "@babel/runtime-corejs3/helpers/createClass";
import _assertThisInitialized from "@babel/runtime-corejs3/helpers/assertThisInitialized";
import _inherits from "@babel/runtime-corejs3/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime-corejs3/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime-corejs3/helpers/getPrototypeOf";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { Component } from "react";
import ReactDOM from "react-dom"; // no tests because js-dom doesn't support MutationObserver
// https://github.com/jsdom/jsdom/issues/639

import { jsx as ___EmotionJSX } from "@emotion/react";

/**
 * Component that will listen to:
 * - any changes in the children tree (any DOM modification),
 * - any resize event happening on the current page.
 * Every time one of this event occur the `onUpdate` prop will be called with the new document height of the listened window and the event originating the update
 */
// explicitly use Component as for some reason using React.Component make props documentation disappearing
export var DomListener = /*#__PURE__*/function (_Component) {
  _inherits(DomListener, _Component);

  var _super = _createSuper(DomListener);

  function DomListener(props) {
    var _this;

    _classCallCheck(this, DomListener);

    _this = _super.call(this, props);
    _this.onResize = _this.onUpdateHeight("resize").bind(_assertThisInitialized(_this));
    _this.onDomChange = _this.onUpdateHeight("domChange").bind(_assertThisInitialized(_this));
    _this.observer = new MutationObserver(_this.onDomChange);
    return _this;
  }

  _createClass(DomListener, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      // eslint-disable-next-line react/no-find-dom-node
      var rootNode = ReactDOM.findDOMNode(this);

      if (rootNode) {
        this.observer.observe(rootNode, {
          attributes: true,
          childList: true,
          subtree: true,
          characterData: true
        }); // need an initial trigger, make it run on next tick :shrug:

        setTimeout(this.onUpdateHeight);
      }

      window.addEventListener("resize", this.onResize); // ResizeObserver not good enough
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.observer.disconnect();
      window.removeEventListener("resize", this.onResize);
    }
  }, {
    key: "onUpdateHeight",
    value: function onUpdateHeight(event) {
      var _this2 = this;

      return function () {
        return _this2.props.onUpdate(document.documentElement.offsetHeight, event);
      };
    }
  }, {
    key: "render",
    value: function render() {
      return ___EmotionJSX("div", null, this.props.children);
    }
  }]);

  return DomListener;
}(Component);
//# sourceMappingURL=DomListener.js.map