"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _react = _interopRequireWildcard(require("react"));

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

var _Bridge = _interopRequireDefault(require("./../Bridge"));

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

var _excluded = ["portal", "show", "config", "shareTo", "animation", "onHide", "maskAttribute"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

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

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__*/_regenerator["default"].mark(function _callee(e) {
      var target;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              target = e.target;
              e.stopPropagation();

              if (target.classList.contains('wechat')) {
                // 微信
                _Bridge["default"].invoke('shareWechatMessage', config, function (res) {
                  if (res.errMsg === 'shareWechatMessage:ok') {
                    console.log('分享成功');
                  } else {
                    console.log('分享失败');
                  }
                });
              } else if (target.classList.contains('wework')) {
                // 企业微信
                _Bridge["default"].invoke('shareAppMessage', config, function (res) {
                  if (res.errMsg === 'shareAppMessage:ok') {
                    console.log('分享成功');
                  } else {
                    console.log('分享失败');
                  }
                });
              } else if (target.classList.contains('moments')) {
                // 朋友圈
                _Bridge["default"].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 = (0, _react.useContext)(_instance["default"]) || {};

  var locale = context.locale || function (remark) {
    return remark || '';
  }; // 返回列表


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

var _default = ShareChoose;
exports["default"] = _default;
module.exports = exports.default;