import PropTypes from 'prop-types';
import { SoftObserver } from '../SoftObserver';
class SoftResizeObserver extends SoftObserver {
    constructor(...args) {
        super(...args);
        this.onResize = () => {
            if (this.childNode != null) {
                // Eventually use `clientRect` on the `entries[]` returned natively
                // @ts-ignore
                const { height, width } = this.childNode.getBoundingClientRect();
                // @ts-ignore
                this.props.onResize({
                    height,
                    width
                });
            }
        };
        // @ts-ignore
        this.beginObserve = () => {
            let observerOptions;
            if (this.hasResizeObserver) {
                // @ts-ignore
                this.observer = new ResizeObserver(this.onResize);
            }
            else {
                // MutationObserver fallback
                observerOptions = {
                    attributes: true,
                    characterData: true,
                    childList: true,
                    subtree: true // Account for deep child nodes
                };
                this.observer = new MutationObserver(this.onResize);
                requestAnimationFrame(this.onResize); // Mimic ResizeObserver behavior of triggering a resize event on init
            }
            this.observer.observe(this.childNode, observerOptions);
        };
        this.name = 'SoftResizeObserver';
        // Only Chrome and Opera support the `ResizeObserver` API at the time of writing
        // @ts-ignore
        this.hasResizeObserver = typeof ResizeObserver !== 'undefined';
    }
}
SoftResizeObserver.propTypes = {
    children: PropTypes.func.isRequired,
    onResize: PropTypes.func.isRequired,
};
export { SoftResizeObserver };
//# sourceMappingURL=SoftResizeObserver.js.map