import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React from 'react';
export var embedHeaderHeight = 32;
export var EmbedResizeMessageListener = /*#__PURE__*/function (_React$Component) {
  function EmbedResizeMessageListener() {
    var _this;
    _classCallCheck(this, EmbedResizeMessageListener);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, EmbedResizeMessageListener, [].concat(args));
    _defineProperty(_this, "messageCallback", function (event) {
      var embedIframeRef = _this.props.embedIframeRef;
      var isFromExpectedIframe = embedIframeRef.current && event.source === embedIframeRef.current.contentWindow;
      var isStringData = typeof event.data === 'string';
      if (isFromExpectedIframe && isStringData) {
        try {
          var data = JSON.parse(event.data);
          if (data.method === 'resize' && typeof data.height === 'number') {
            _this.onEmbedHeightChange({
              height: data.height
            });
          }
        } catch (e) {}
      }
    });
    _defineProperty(_this, "onEmbedHeightChange", function (data) {
      var onHeightUpdate = _this.props.onHeightUpdate;
      var height = data.height + embedHeaderHeight;
      onHeightUpdate(height);
    });
    return _this;
  }
  _inherits(EmbedResizeMessageListener, _React$Component);
  return _createClass(EmbedResizeMessageListener, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      window.addEventListener('message', this.messageCallback);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      window.removeEventListener('message', this.messageCallback);
    }
  }, {
    key: "render",
    value: function render() {
      return this.props.children;
    }
  }]);
}(React.Component);