var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import React, { Component } from "react";
import ReactDOM from "react-dom";
/**
 * 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
var DomListener = /** @class */ (function (_super) {
    __extends(DomListener, _super);
    function DomListener(props) {
        var _this = _super.call(this, props) || this;
        _this.onResize = _this.onUpdateSize("resize").bind(_this);
        _this.onDomChange = _this.onUpdateSize("domChange").bind(_this);
        _this.observer = new MutationObserver(_this.onDomChange);
        return _this;
    }
    DomListener.prototype.componentDidMount = function () {
        // 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.onUpdateSize);
        }
        window.addEventListener("resize", this.onResize); // ResizeObserver not good enough
    };
    DomListener.prototype.componentWillUnmount = function () {
        this.observer.disconnect();
        window.removeEventListener("resize", this.onResize);
    };
    DomListener.prototype.onUpdateSize = function (event) {
        var _this = this;
        return function () {
            return _this.props.onUpdate(document.documentElement.offsetHeight, document.documentElement.offsetWidth, event);
        };
    };
    DomListener.prototype.render = function () {
        return React.createElement("div", null, this.props.children);
    };
    return DomListener;
}(Component));
export { DomListener };
//# sourceMappingURL=DomListener.js.map