import { useEffect } from 'react';
import useResolve from '../../state/hooks/use-resolve';
export const useEmbedResolvePostMessageListener = ({
  url,
  embedIframeRef
}) => {
  const resolve = useResolve();
  useEffect(() => {
    const messageCallback = event => {
      var _embedIframeRef$curre;
      if (typeof embedIframeRef === 'function') {
        return;
      }
      const 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,
          isReloading: true
        });
      }
    };
    window.addEventListener('message', messageCallback);
    return () => {
      window.removeEventListener('message', messageCallback);
    };
  }, [resolve, url, embedIframeRef]);
};