import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["portal", "show", "config", "shareTo", "animation", "onHide", "maskAttribute"];

function _extends() { _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; }; return _extends.apply(this, arguments); }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } 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 portal = _ref.portal,
      show = _ref.show,
      _ref$config = _ref.config,
      config = _ref$config === void 0 ? {} : _ref$config,
      shareTo = _ref.shareTo,
      _ref$animation = _ref.animation,
      animation = _ref$animation === void 0 ? 'slideUp' : _ref$animation,
      onHide = _ref.onHide,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === void 0 ? {} : _ref$maskAttribute,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

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


  function _handlerClick() {
    _handlerClick = _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);
    }));
    return _handlerClick.apply(this, arguments);
  }

  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 (remark) {
    return remark || '';
  }; // 返回列表


  if (!shareTo || !shareTo.length) return null;
  return createPortal( /*#__PURE__*/React.createElement("div", _extends({
    className: "mask share-mask" + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : '')
  }, maskAttribute, {
    onClick: handlerClick
  }), /*#__PURE__*/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 /*#__PURE__*/React.createElement("div", {
      className: "share-item",
      key: index
    }, /*#__PURE__*/React.createElement("i", {
      className: "share-icon " + share
    }), /*#__PURE__*/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;