var _excluded = ["portal", "show", "animation", "duration", "maskAttribute", "caption", "captionAttribute", "icon", "contentAttribute", "submitCaption", "submitAttribute", "cancelCaption", "cancelAttribute", "children"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { forwardRef, useContext } from 'react';
import { createPortal } from 'react-dom';
import Context from './../Context/instance.js';
var Alert = forwardRef(function (_ref, ref) {
  var portal = _ref.portal,
      show = _ref.show,
      _ref$animation = _ref.animation,
      animation = _ref$animation === void 0 ? 'zoom' : _ref$animation,
      duration = _ref.duration,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === void 0 ? {} : _ref$maskAttribute,
      caption = _ref.caption,
      _ref$captionAttribute = _ref.captionAttribute,
      captionAttribute = _ref$captionAttribute === void 0 ? {} : _ref$captionAttribute,
      icon = _ref.icon,
      _ref$contentAttribute = _ref.contentAttribute,
      contentAttribute = _ref$contentAttribute === void 0 ? {} : _ref$contentAttribute,
      submitCaption = _ref.submitCaption,
      _ref$submitAttribute = _ref.submitAttribute,
      submitAttribute = _ref$submitAttribute === void 0 ? {} : _ref$submitAttribute,
      cancelCaption = _ref.cancelCaption,
      _ref$cancelAttribute = _ref.cancelAttribute,
      cancelAttribute = _ref$cancelAttribute === void 0 ? {} : _ref$cancelAttribute,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  // context
  var context = useContext(Context) || {};

  var locale = context.locale || function (remark) {
    return remark || '';
  }; // 构建动画


  var animationClassName = '';

  switch (animation) {
    case 'slideLeft':
      animationClassName = 'popup-animation right-middle';
      break;

    case 'slideRight':
      animationClassName = 'popup-animation left-middle';
      break;

    case 'slideUp':
      animationClassName = 'popup-animation bottom-center';
      break;

    case 'slideDown':
      animationClassName = 'popup-animation top-center';
      break;

    case 'zoom':
      animationClassName = 'popup-animation middle';
      break;

    case 'fade':
      animationClassName = 'popup-animation middle';
      break;

    case 'none':
      animationClassName = '';
      break;

    default:
      animationClassName = 'popup-animation middle';
  } // 动画时长


  var durationStyle = {};

  if (typeof duration === 'number') {
    durationStyle = {
      WebkitTransitionDuration: duration + 'ms'
    };
  }

  return createPortal( /*#__PURE__*/React.createElement("div", _extends({
    ref: ref
  }, maskAttribute, {
    className: "mask alert-mask" + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : ''),
    style: Object.assign({}, durationStyle, maskAttribute.style || {})
  }), /*#__PURE__*/React.createElement("div", _extends({
    "data-animation": animation
  }, others, {
    className: "alert" + (animationClassName ? ' ' + animationClassName : '') + (others.className ? ' ' + others.className : '') + (show ? ' active' : ''),
    style: Object.assign({}, durationStyle, others.style || {})
  }), caption && /*#__PURE__*/React.createElement("h1", captionAttribute, caption), /*#__PURE__*/React.createElement("div", _extends({}, contentAttribute, {
    className: "alert-content" + (contentAttribute.className ? ' ' + contentAttribute.className : '')
  }), icon, children), /*#__PURE__*/React.createElement("div", {
    className: "alert-handler"
  }, cancelAttribute.onClick && /*#__PURE__*/React.createElement("a", _extends({}, cancelAttribute, {
    className: "alert-cancel button lg" + (cancelAttribute.className ? ' ' + cancelAttribute.className : '')
  }), cancelCaption || locale('取消', 'cancel')), submitAttribute.onClick && /*#__PURE__*/React.createElement("a", _extends({}, submitAttribute, {
    className: "alert-submit button lg" + (submitAttribute.className ? ' ' + submitAttribute.className : '')
  }), submitCaption || locale('确定', 'ok'))))), portal || context.portal || document.getElementById('root') || document.body);
});
export default Alert;