"use strict";

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

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

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

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

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _ramdaExtension = require("ramda-extension");

var _Flex = _interopRequireDefault(require("../Flex"));

var _PortalToRoot = _interopRequireDefault(require("./PortalToRoot"));

var _Backdrop = _interopRequireDefault(require("./Backdrop"));

var _contexts = require("./contexts");

var initialState = {
  modalProps: null,
  component: null,
  isOpened: false
};
var ActionTypes = (0, _ramdaExtension.valueMirror)(['OPEN_MODAL', 'CLOSE_MODAL']);

var reducer = function reducer(state, action) {
  switch (action.type) {
    case ActionTypes.OPEN_MODAL:
      {
        var _action$payload = action.payload,
            component = _action$payload.component,
            modalProps = _action$payload.modalProps;
        return {
          component: component,
          modalProps: modalProps,
          isOpened: true
        };
      }

    case ActionTypes.CLOSE_MODAL:
      {
        return {
          component: null,
          modalProps: null,
          isOpened: false
        };
      }
  }

  return state;
};

var actions = {
  openModal: function openModal(payload) {
    return {
      type: ActionTypes.OPEN_MODAL,
      payload: payload
    };
  },
  closeModal: function closeModal(payload) {
    return {
      type: ActionTypes.CLOSE_MODAL,
      payload: payload
    };
  }
};

var toggleBodyClassname = function toggleBodyClassname(className, toggler) {
  if (typeof window === 'undefined') {
    return;
  }

  var classList = document.body.classList;
  return toggler ? classList.add(className) : classList.remove(className);
};

var ModalProvider = function ModalProvider(_ref) {
  var children = _ref.children,
      _ref$bodyModalClassna = _ref.bodyModalClassname,
      bodyModalClassname = _ref$bodyModalClassna === void 0 ? 'modal' : _ref$bodyModalClassna;

  var _useReducer = (0, _react.useReducer)(reducer, initialState),
      _useReducer2 = (0, _slicedToArray2["default"])(_useReducer, 2),
      _useReducer2$ = _useReducer2[0],
      isOpened = _useReducer2$.isOpened,
      modalProps = _useReducer2$.modalProps,
      ModalComponent = _useReducer2$.component,
      dispatch = _useReducer2[1];

  var api = (0, _react.useMemo)(function () {
    return {
      openModal: function openModal(component, modalProps) {
        dispatch(actions.openModal({
          component: component,
          modalProps: modalProps
        }));
        toggleBodyClassname("".concat(bodyModalClassname, "--opened"), true);
      },
      closeModal: function closeModal(component) {
        dispatch(actions.closeModal({
          component: component
        }));
        toggleBodyClassname("".concat(bodyModalClassname, "--opened"), false);
      }
    };
  }, [bodyModalClassname]);
  return /*#__PURE__*/_react["default"].createElement(_contexts.ModalApiContext.Provider, {
    value: api
  }, children, isOpened && ModalComponent && /*#__PURE__*/_react["default"].createElement(_PortalToRoot["default"], null, /*#__PURE__*/_react["default"].createElement(_Backdrop["default"], {
    onClick: function onClick() {
      return api.closeModal(ModalComponent);
    }
  }), /*#__PURE__*/_react["default"].createElement(_Flex["default"], {
    justifyContent: "center",
    alignItems: "center",
    flexDirection: "column",
    sx: {
      zIndex: 1001,
      position: 'fixed',
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
      p: [0, 4]
    }
  }, /*#__PURE__*/_react["default"].createElement(ModalComponent, (0, _extends2["default"])({}, modalProps, api)))));
};

exports.ModalProvider = ModalProvider;
process.env.NODE_ENV !== "production" ? ModalProvider.propTypes = {
  bodyModalClassname: _propTypes["default"].string,
  children: _propTypes["default"].node
} : void 0;
var _default = ModalProvider;
exports["default"] = _default;