import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useCallback, useContext, useEffect, useMemo, useRef } from 'react';
import noop from 'lodash/noop';
import ModalContainer from '../modal-container/ModalContainer';
import Modal from '../modal/Modal';
import ModalContext from './ModalContext';

var ModalProvider = function ModalProvider(props) {
  var _useContext = useContext(ModalContext),
      contextLocation = _useContext.location;

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

    if (container) {
      var close = /*#__PURE__*/function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(destroy) {
          var _modalProps, _modalProps$onClose, onClose;

          return _regeneratorRuntime.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 : _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(_objectSpread(_objectSpread({}, 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(_objectSpread(_objectSpread({}, modalProps), newProps));
      };

      modalProps = _objectSpread(_objectSpread({
        close: close,
        update: update
      }, Modal.defaultProps), modalProps);
      container.open(modalProps);
      return {
        close: close,
        open: show,
        update: update
      };
    }

    prepareToOpen.push(modalProps);
  }, []);
  useEffect(function () {
    if (ref.current) {
      prepareToOpen.forEach(function (prepare) {
        return open(prepare);
      });
    }
  }, [ref, open]);
  var context = {
    open: open,
    location: location
  };
  return /*#__PURE__*/React.createElement(ModalContext.Provider, {
    value: context
  }, /*#__PURE__*/React.createElement(ModalContainer, {
    ref: ref,
    location: location,
    getContainer: getContainer
  }), children);
};

export var useModal = function useModal() {
  return useContext(ModalContext);
};
export var injectModal = function injectModal(Target) {
  var Hoc = function Hoc(props) {
    var modal = useModal();
    return /*#__PURE__*/React.createElement(Target, _extends({}, props, {
      Modal: modal
    }));
  };

  Hoc.displayName = "".concat(Target.displayName || Target.name || 'Anonymous', "-with-inject-modal");
  return Hoc;
};
ModalProvider.displayName = 'ModalProvider';
ModalProvider.useModal = useModal;
ModalProvider.injectModal = injectModal;
export default ModalProvider;
//# sourceMappingURL=ModalProvider.js.map
