"use strict";

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

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

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

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

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

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

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

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

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

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

var _configProvider = require("../config-provider");

var _popup = _interopRequireDefault(require("../popup"));

var _utils = require("../utils");

var _ModalAction = _interopRequireDefault(require("./ModalAction"));

var Modal = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var className = props.className,
      title = props.title,
      shape = props.shape,
      closable = props.closable,
      onClose = props.onClose,
      children = props.children,
      footer = props.footer,
      maskClosable = props.maskClosable,
      actions = props.actions,
      onAction = props.onAction,
      rest = (0, _objectWithoutProperties2.default)(props, ["className", "title", "shape", "closable", "onClose", "children", "footer", "maskClosable", "actions", "onAction"]);

  var _React$useContext = React.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('modal', {
    prefixCls: prefixCls
  });
  var cls = bem([(0, _defineProperty2.default)({}, "".concat(shape), !!shape), className]);
  var showHeader = title || closable;
  var hasActions = actions && actions.length > 0;
  var showFooter = !!footer || hasActions;
  var actionsRender = actions.map(function (action, i) {
    var currentAction = Array.isArray(action) ? action : [action];
    return /*#__PURE__*/React.createElement("div", {
      key: +i,
      className: bem('action')
    }, currentAction.map(function (child, j) {
      return /*#__PURE__*/React.createElement(_ModalAction.default, (0, _extends2.default)({}, child, {
        key: +j,
        onClick: /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
          var _child$onClick;

          return _regenerator.default.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return (_child$onClick = child.onClick) === null || _child$onClick === void 0 ? void 0 : _child$onClick.call(child);

                case 2:
                  _context.next = 4;
                  return onAction === null || onAction === void 0 ? void 0 : onAction(child);

                case 4:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }))
      }));
    }));
  });
  return /*#__PURE__*/React.createElement(_popup.default, (0, _extends2.default)({}, rest, {
    ref: ref,
    className: cls,
    direction: "center",
    onMaskClick: maskClosable ? onClose : _utils.noop
  }), /*#__PURE__*/React.createElement("div", {
    className: bem('dialog')
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('body')
  }, showHeader && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: bem('title')
  }, title), closable && /*#__PURE__*/React.createElement(_icons.Close, {
    size: "sm",
    className: bem('close'),
    onClick: onClose
  })), children), showFooter && /*#__PURE__*/React.createElement("div", {
    className: bem('footer')
  }, hasActions ? actionsRender : footer)));
});
Modal.defaultProps = {
  visible: false,
  animationType: 'fade',
  width: '70%',
  mask: true,
  shape: 'radius',
  closable: false,
  maskClosable: false,
  destroy: true,
  actions: []
};
var _default = Modal;
exports.default = _default;