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 Dialog = forwardRef(function (_ref, ref) {
  var portal = _ref.portal,
      show = _ref.show,
      onClick = _ref.onClick,
      _ref$animation = _ref.animation,
      animation = _ref$animation === undefined ? 'fade' : _ref$animation,
      duration = _ref.duration,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === undefined ? {} : _ref$maskAttribute,
      children = _ref.children,
      others = _objectWithoutProperties(_ref, ['portal', 'show', 'onClick', 'animation', 'duration', 'maskAttribute', 'children']);

  // context
  var context = useContext(Context) || {};
  // const locale = context.locale || function (key) {return key || ''};

  // 点击dialog主体
  function click(e) {
    e.stopPropagation();
  }
  // 构建动画
  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;
    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 dialog-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : ''),
      style: Object.assign({}, durationStyle, maskAttribute.style || {})
    }),
    React.createElement(
      'div',
      _extends({}, others, {
        className: 'dialog' + (animationClassName ? ' ' + animationClassName : '') + (others.className ? ' ' + others.className : '') + (show ? ' active' : ''),
        style: Object.assign({}, durationStyle, others.style || {}),
        onClick: onClick ? onClick : click,
        'data-animation': animation
      }),
      children && children
    )
  ), portal || context.portal || document.getElementById('root') || document.body);
});

export default Dialog;