'use strict';

exports.__esModule = true;

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _instance = require('./../Context/instance.js');

var _instance2 = _interopRequireDefault(_instance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

var Dialog = (0, _react.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 = (0, _react.useContext)(_instance2.default) || {};
  // const locale = context.locale || {};

  // 点击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 (0, _reactDom.createPortal)(_react2.default.createElement(
    'div',
    _extends({
      ref: ref
    }, maskAttribute, {
      className: 'mask dialog-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : ''),
      style: Object.assign({}, durationStyle, maskAttribute.style || {})
    }),
    _react2.default.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);
});

exports.default = Dialog;
module.exports = exports['default'];