"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useEmbedResolvePostMessageListener = void 0;
var _react = require("react");
var _useResolve = _interopRequireDefault(require("../../state/hooks/use-resolve"));
var useEmbedResolvePostMessageListener = exports.useEmbedResolvePostMessageListener = function useEmbedResolvePostMessageListener(_ref) {
  var url = _ref.url,
    embedIframeRef = _ref.embedIframeRef;
  var resolve = (0, _useResolve.default)();
  (0, _react.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, true);
      }
    };
    window.addEventListener('message', messageCallback);
    return function () {
      window.removeEventListener('message', messageCallback);
    };
  }, [resolve, url, embedIframeRef]);
};