"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useModal = exports.injectModal = exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _react = _interopRequireWildcard(require("react"));

var _noop = _interopRequireDefault(require("lodash/noop"));

var _ModalContainer = _interopRequireDefault(require("../modal-container/ModalContainer"));

var _Modal = _interopRequireDefault(require("../modal/Modal"));

var _ModalContext = _interopRequireDefault(require("./ModalContext"));

var ModalProvider = function ModalProvider(props) {
  var _useContext = (0, _react.useContext)(_ModalContext["default"]),
      contextLocation = _useContext.location;

  var _props$location = props.location,
      location = _props$location === void 0 ? contextLocation : _props$location,
      children = props.children,
      getContainer = props.getContainer;
  var ref = (0, _react.useRef)(null);
  var prepareToOpen = (0, _react.useMemo)(function () {
    return [];
  }, []);
  var open = (0, _react.useCallback)(function (modalProps) {
    var container = ref.current;

    if (container) {
      var close = /*#__PURE__*/function () {
        var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(destroy) {
          var _modalProps, _modalProps$onClose, onClose;

          return _regenerator["default"].wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _modalProps = modalProps, _modalProps$onClose = _modalProps.onClose, onClose = _modalProps$onClose === void 0 ? _noop["default"] : _modalProps$onClose;
                  _context.next = 3;
                  return onClose();

                case 3:
                  _context.t0 = _context.sent;

                  if (!(_context.t0 !== false)) {
                    _context.next = 6;
                    break;
                  }

                  if (destroy) {
                    container.close((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, modalProps), {}, {
                      destroyOnClose: true
                    }));
                  } else {
                    container.close(modalProps);
                  }

                case 6:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));

        return function close(_x) {
          return _ref.apply(this, arguments);
        };
      }();

      var show = function show() {
        container.open(modalProps);
      };

      var update = function update(newProps) {
        container.update((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, modalProps), newProps));
      };

      modalProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
        close: close,
        update: update
      }, _Modal["default"].defaultProps), modalProps);
      container.open(modalProps);
      return {
        close: close,
        open: show,
        update: update
      };
    }

    prepareToOpen.push(modalProps);
  }, []);
  (0, _react.useEffect)(function () {
    if (ref.current) {
      prepareToOpen.forEach(function (prepare) {
        return open(prepare);
      });
    }
  }, [ref, open]);
  var context = {
    open: open,
    location: location
  };
  return /*#__PURE__*/_react["default"].createElement(_ModalContext["default"].Provider, {
    value: context
  }, /*#__PURE__*/_react["default"].createElement(_ModalContainer["default"], {
    ref: ref,
    location: location,
    getContainer: getContainer
  }), children);
};

var useModal = function useModal() {
  return (0, _react.useContext)(_ModalContext["default"]);
};

exports.useModal = useModal;

var injectModal = function injectModal(Target) {
  var Hoc = function Hoc(props) {
    var modal = useModal();
    return /*#__PURE__*/_react["default"].createElement(Target, (0, _extends2["default"])({}, props, {
      Modal: modal
    }));
  };

  Hoc.displayName = "".concat(Target.displayName || Target.name || 'Anonymous', "-with-inject-modal");
  return Hoc;
};

exports.injectModal = injectModal;
ModalProvider.displayName = 'ModalProvider';
ModalProvider.useModal = useModal;
ModalProvider.injectModal = injectModal;
var _default = ModalProvider;
exports["default"] = _default;
//# sourceMappingURL=ModalProvider.js.map
