"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 _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

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

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

var _safeArea = _interopRequireDefault(require("../safe-area"));

var _ActionSheetItem = _interopRequireDefault(require("./ActionSheetItem"));

var ActionSheet = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var className = props.className,
      style = props.style,
      spacing = props.spacing,
      actions = props.actions,
      cancelText = props.cancelText,
      onCancel = props.onCancel,
      onAction = props.onAction,
      safeArea = props.safeArea,
      restProps = (0, _objectWithoutProperties2.default)(props, ["className", "style", "spacing", "actions", "cancelText", "onCancel", "onAction", "safeArea"]);

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls,
      globalSafeArea = _useContext.safeArea,
      locale = _useContext.locale;

  var bem = (0, _bem.createBEM)('action-sheet', {
    prefixCls: prefixCls
  });
  var cls = bem([{
    spacing: spacing
  }, className]);
  var actionsRender = actions.map(function (action, index) {
    return /*#__PURE__*/_react.default.createElement(_ActionSheetItem.default, (0, _extends2.default)({}, action, {
      key: +index,
      onClick: /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var _action$onClick;

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

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

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

  var renderCancel = function renderCancel() {
    var _locale$ActionSheet;

    if (typeof onCancel !== 'function') return;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: bem('cancel')
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: bem('item', [{
        bold: true
      }]),
      onClick: onCancel
    }, cancelText || (locale === null || locale === void 0 ? void 0 : (_locale$ActionSheet = locale.ActionSheet) === null || _locale$ActionSheet === void 0 ? void 0 : _locale$ActionSheet.cancelText)));
  };

  return /*#__PURE__*/_react.default.createElement(_popup.default, restProps, /*#__PURE__*/_react.default.createElement("div", {
    ref: ref,
    className: cls,
    style: style
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('actions')
  }, actionsRender), renderCancel(), (safeArea !== null && safeArea !== void 0 ? safeArea : globalSafeArea) && /*#__PURE__*/_react.default.createElement(_safeArea.default, {
    position: "bottom"
  })));
});

ActionSheet.displayName = 'ActionSheet';
ActionSheet.defaultProps = {
  spacing: false,
  visible: false,
  actions: [],
  destroy: true
};
var _default = ActionSheet;
exports.default = _default;