import { useEffect } from 'react';
import useResolve from '../../state/hooks/use-resolve';
export var useEmbedResolvePostMessageListener = function useEmbedResolvePostMessageListener(_ref) {
  var url = _ref.url,
    embedIframeRef = _ref.embedIframeRef;
  var resolve = useResolve();
  useEffect(function () {
    var messageCallback = function messageCallback(event) {
      var _embedIframeRef$curre;
      if (typeof embedIframeRef === 'function') {
        return;
      }
      var isFromExpectedIframe = embedIframeRef && event.source === ((_embedIframeRef$curre = embedIframeRef.current) === null || _embedIframeRef$curre === void 0 ? void 0 : _embedIframeRef$curre.contentWindow);
      if (event.data === 'force-resolve-smart-link' && isFromExpectedIframe) {
        resolve({
          url: url,
          isReloading: true
        });
      }
    };
    window.addEventListener('message', messageCallback);
    return function () {
      window.removeEventListener('message', messageCallback);
    };
  }, [resolve, url, embedIframeRef]);
};