import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
export const embedHeaderHeight = 32;
export class EmbedResizeMessageListener extends React.Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "messageCallback", event => {
      const {
        embedIframeRef
      } = this.props;
      const isFromExpectedIframe = embedIframeRef.current && event.source === embedIframeRef.current.contentWindow;
      const isStringData = typeof event.data === 'string';
      if (isFromExpectedIframe && isStringData) {
        try {
          const data = JSON.parse(event.data);
          if (data.method === 'resize' && typeof data.height === 'number') {
            this.onEmbedHeightChange({
              height: data.height
            });
          }
        } catch (e) {}
      }
    });
    _defineProperty(this, "onEmbedHeightChange", data => {
      const {
        onHeightUpdate
      } = this.props;
      const height = data.height + embedHeaderHeight;
      onHeightUpdate(height);
    });
  }
  componentDidMount() {
    window.addEventListener('message', this.messageCallback);
  }
  componentWillUnmount() {
    window.removeEventListener('message', this.messageCallback);
  }
  render() {
    return this.props.children;
  }
}