var _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; };

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } 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 === undefined ? 'zoom' : _ref$animation,
      duration = _ref.duration,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === undefined ? {} : _ref$maskAttribute,
      caption = _ref.caption,
      _ref$captionAttribute = _ref.captionAttribute,
      captionAttribute = _ref$captionAttribute === undefined ? {} : _ref$captionAttribute,
      icon = _ref.icon,
      _ref$contentAttribute = _ref.contentAttribute,
      contentAttribute = _ref$contentAttribute === undefined ? {} : _ref$contentAttribute,
      submitCaption = _ref.submitCaption,
      _ref$submitAttribute = _ref.submitAttribute,
      submitAttribute = _ref$submitAttribute === undefined ? {} : _ref$submitAttribute,
      cancelCaption = _ref.cancelCaption,
      _ref$cancelAttribute = _ref.cancelAttribute,
      cancelAttribute = _ref$cancelAttribute === undefined ? {} : _ref$cancelAttribute,
      children = _ref.children,
      others = _objectWithoutProperties(_ref, ['portal', 'show', 'animation', 'duration', 'maskAttribute', 'caption', 'captionAttribute', 'icon', 'contentAttribute', 'submitCaption', 'submitAttribute', 'cancelCaption', 'cancelAttribute', 'children']);

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

  // 构建动画
  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(React.createElement(
    'div',
    _extends({
      ref: ref
    }, maskAttribute, {
      className: 'mask alert-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : ''),
      style: Object.assign({}, durationStyle, maskAttribute.style || {})
    }),
    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 && React.createElement(
        'h1',
        captionAttribute,
        caption
      ),
      React.createElement(
        'div',
        _extends({}, contentAttribute, { className: 'alert-content' + (contentAttribute.className ? ' ' + contentAttribute.className : '') }),
        icon,
        children
      ),
      React.createElement(
        'div',
        { className: 'alert-handler' },
        cancelAttribute.onClick && React.createElement(
          'a',
          _extends({}, cancelAttribute, { className: 'alert-cancel button lg' + (cancelAttribute.className ? ' ' + cancelAttribute.className : '') }),
          cancelCaption || locale['cancel'] || '取消'
        ),
        submitAttribute.onClick && 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;