"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.embedHeaderHeight = exports.EmbedResizeMessageListener = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(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; })(); }
var embedHeaderHeight = exports.embedHeaderHeight = 32;
var EmbedResizeMessageListener = exports.EmbedResizeMessageListener = /*#__PURE__*/function (_React$Component) {
  function EmbedResizeMessageListener() {
    var _this;
    (0, _classCallCheck2.default)(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));
    (0, _defineProperty2.default)(_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) {}
      }
    });
    (0, _defineProperty2.default)(_this, "onEmbedHeightChange", function (data) {
      var onHeightUpdate = _this.props.onHeightUpdate;
      var height = data.height + embedHeaderHeight;
      onHeightUpdate(height);
    });
    return _this;
  }
  (0, _inherits2.default)(EmbedResizeMessageListener, _React$Component);
  return (0, _createClass2.default)(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.default.Component);