import _regeneratorRuntime from 'babel-runtime/regenerator';

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 _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

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, { useContext } from 'react';
import { createPortal } from 'react-dom';
import Bridge from './../Bridge';
import Context from '../Context/instance.js';

function ShareChoose(_ref) {
  // 点击
  var handlerClick = function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(e) {
      var target;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              target = e.target;

              e.stopPropagation();
              if (target.classList.contains('wechat')) {
                // 微信
                Bridge.invoke('shareWechatMessage', config, function (res) {
                  if (res.errMsg === 'shareWechatMessage:ok') {
                    console.log('分享成功');
                  } else {
                    console.log('分享失败');
                  }
                });
              } else if (target.classList.contains('wework')) {
                // 企业微信
                Bridge.invoke('shareAppMessage', config, function (res) {
                  if (res.errMsg === 'shareAppMessage:ok') {
                    console.log('分享成功');
                  } else {
                    console.log('分享失败');
                  }
                });
              } else if (target.classList.contains('moments')) {
                // 朋友圈
                Bridge.invoke('shareTimeline', config, function (res) {
                  if (res.errMsg === 'shareTimeline:ok') {
                    console.log('分享成功');
                  } else {
                    console.log('分享失败');
                  }
                });
              } else {
                if (onHide) onHide(e);
              }

            case 3:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    return function handlerClick(_x) {
      return _ref2.apply(this, arguments);
    };
  }();
  // 构建动画


  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 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 = useContext(Context) || {};
  var locale = context.locale || function (key) {
    return key || '';
  };
  // 返回列表
  if (!shareTo || !shareTo.length) return null;
  return createPortal(React.createElement(
    'div',
    _extends({ className: 'mask share-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : '') }, maskAttribute, { onClick: handlerClick }),
    React.createElement(
      'div',
      _extends({
        'data-animation': animation
      }, others, {
        className: 'share-panel' + (animationClassName ? ' ' + animationClassName : '') + (others.className ? ' ' + others.className : '') + (show ? ' active' : '')
      }),
      shareTo.map(function (share, index) {
        return React.createElement(
          'div',
          { className: 'share-item', key: index },
          React.createElement('i', { className: 'share-icon ' + share }),
          React.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);
}

export default ShareChoose;