"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSmartLinkModal = exports.SmartLinkModalProvider = exports.SmartLinkModalContext = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _reactErrorBoundary = require("react-error-boundary");
var _stopPropagation = require("../../view/common/stop-propagation");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var FALLBACK_API = {
  open: function open() {},
  close: function close() {}
};
var SmartLinkModalContext = exports.SmartLinkModalContext = /*#__PURE__*/_react.default.createContext(FALLBACK_API);
var SmartLinkModalProvider = exports.SmartLinkModalProvider = function SmartLinkModalProvider(_ref) {
  var children = _ref.children;
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    element = _useState2[0],
    setElement = _useState2[1];
  var api = (0, _react.useMemo)(function () {
    return {
      open: function open(modal) {
        return setElement( /*#__PURE__*/_react.default.createElement(_react.Suspense, {
          fallback: null
        }, /*#__PURE__*/_react.default.createElement(_stopPropagation.StopPropagation, null, modal)));
      },
      close: function close() {
        return setElement(null);
      }
    };
  }, []);
  var fallbackRender = (0, _react.useCallback)(function (_ref2) {
    var resetErrorBoundary = _ref2.resetErrorBoundary;
    resetErrorBoundary();
    return null;
  }, []);
  var onReset = (0, _react.useCallback)(function () {
    return setElement(null);
  }, []);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(SmartLinkModalContext.Provider, {
    value: api
  }, children), /*#__PURE__*/_react.default.createElement(_reactErrorBoundary.ErrorBoundary, {
    fallbackRender: fallbackRender,
    onReset: onReset
  }, element));
};

/**
 * Open (lazy load) modal
 *
 * This hook injects the element below Card and standalone HoverCard component.
 * It is intended to solve the issue where modal triggered by the parent component
 * disappear when the parent component is unmounted.
 *
 * For example, clicking on hover card action to open a modal. Once the modal is opened,
 * hover card disappears.
 *
 * Usage:
 *   const modal = useSmartLinkModal()
 *   modal.open(<SomeLazyLoadModal isOpen={true} onClose={() => modal.close()} />);
 */
var useSmartLinkModal = exports.useSmartLinkModal = function useSmartLinkModal() {
  return (0, _react.useContext)(SmartLinkModalContext);
};