'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 _Bridge = require('./../Bridge');

var _Bridge2 = _interopRequireDefault(_Bridge);

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

function ShareChoose(_ref) {
  var portal = _ref.portal,
      show = _ref.show,
      _ref$config = _ref.config,
      config = _ref$config === undefined ? {} : _ref$config,
      shareTo = _ref.shareTo,
      _ref$animation = _ref.animation,
      animation = _ref$animation === undefined ? 'slideUp' : _ref$animation,
      onHide = _ref.onHide,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === undefined ? {} : _ref$maskAttribute,
      others = _objectWithoutProperties(_ref, ['portal', 'show', 'config', 'shareTo', 'animation', 'onHide', 'maskAttribute']);

  // 点击
  var handlerClick = function handlerClick(e) {
    var target = e.target;
    e.stopPropagation();
    if (target.classList.contains('wechat')) {
      // 微信
      _Bridge2.default.invoke('shareWechatMessage', config, function (res) {
        if (res.errMsg === 'shareWechatMessage:ok') {
          console.log('分享成功');
        } else {
          console.log('分享失败');
        }
      });
    } else if (target.classList.contains('wework')) {
      // 企业微信
      _Bridge2.default.invoke('shareAppMessage', config, function (res) {
        if (res.errMsg === 'shareAppMessage:ok') {
          console.log('分享成功');
        } else {
          console.log('分享失败');
        }
      });
    } else if (target.classList.contains('moments')) {
      // 朋友圈
      _Bridge2.default.invoke('shareTimeline', config, function (res) {
        if (res.errMsg === 'shareTimeline:ok') {
          console.log('分享成功');
        } else {
          console.log('分享失败');
        }
      });
    } else {
      if (onHide) onHide(e);
    }
  };
  // 构建动画
  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';
  }
  // context
  var context = (0, _react.useContext)(_instance2.default) || {};
  var locale = context.locale || {};
  // 返回列表
  if (!shareTo || !shareTo.length) return null;
  return (0, _reactDom.createPortal)(_react2.default.createElement(
    'div',
    _extends({ className: 'mask share-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : '') }, maskAttribute, { onClick: handlerClick }),
    _react2.default.createElement(
      'div',
      _extends({
        'data-animation': animation
      }, others, {
        className: 'share-panel' + (animationClassName ? ' ' + animationClassName : '') + (others.className ? ' ' + others.className : '') + (show ? ' active' : '')
      }),
      shareTo.map(function (share, index) {
        return _react2.default.createElement(
          'div',
          { className: 'share-item', key: index },
          _react2.default.createElement('i', { className: 'share-icon ' + share }),
          _react2.default.createElement(
            'p',
            { className: 'share-caption' },
            share === 'wechat' && (locale['wechat_friend'] || '微信好友'),
            share === 'wework' && (locale['wework_friend'] || '企业微信好友'),
            share === 'moments' && (locale['wechat_moments'] || '微信朋友圈')
          )
        );
      })
    )
  ), portal || context.portal || document.getElementById('root') || document.body);
}

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