import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["children", "type", "shareTo", "getConfig", "config", "originConfig"];

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, { useState } from 'react';
import Bridge from './../Bridge';
import ShareChoose from './ShareChoose';
import ShareTip from './ShareTip';
import ShareType from './ShareType';

function Share(_ref) {
  var children = _ref.children,
      type = _ref.type,
      _ref$shareTo = _ref.shareTo,
      shareTo = _ref$shareTo === void 0 ? ['wechat', 'wework', 'moments'] : _ref$shareTo,
      getConfig = _ref.getConfig,
      _ref$config = _ref.config,
      config = _ref$config === void 0 ? {
    title: '',
    // 分享标题
    desc: '',
    // 分享描述
    link: '',
    // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
    imgUrl: '' // 分享图标

  } : _ref$config,
      originConfig = _ref.originConfig,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  if (Bridge.platform !== 'wework' && Bridge.platform !== 'wechat' && Bridge.platform !== 'wq' && Bridge.platform !== 'dinghuo' && Bridge.platform !== 'waiqin') {
    console.log('Share: 此平台无法使用分享功能');
    return null;
  }

  if (!config || !config.link) {
    console.log('Share: 没有设置config');
    return null;
  } // 定义state


  var _useState = useState(false),
      show = _useState[0],
      setShow = _useState[1]; // 显隐弹窗


  function handlerClick(_x) {
    return _handlerClick.apply(this, arguments);
  }

  function _handlerClick() {
    _handlerClick = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(event) {
      var e, target;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              e = event.nativeEvent;

              if (!getConfig) {
                _context.next = 7;
                break;
              }

              _context.next = 4;
              return getConfig();

            case 4:
              config = _context.sent;

              if (!(typeof config !== 'object')) {
                _context.next = 7;
                break;
              }

              return _context.abrupt("return");

            case 7:
              if (type) {
                _context.next = 10;
                break;
              }

              if (Bridge.platform === 'dinghuo' || Bridge.platform === 'waiqin') {
                Bridge.shareText({
                  title: config.title,
                  desc: config.desc,
                  link: config.link,
                  text: "" + config.link
                });
              } else {
                setShow(true);
              }

              return _context.abrupt("return");

            case 10:
              // 单项显示
              target = e.target;

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

            case 12:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));
    return _handlerClick.apply(this, arguments);
  }

  return /*#__PURE__*/React.createElement("div", _extends({}, others, {
    onClick: handlerClick
  }), children, Bridge.platform === 'wechat' && /*#__PURE__*/React.createElement(ShareTip, {
    show: show,
    config: config,
    originConfig: originConfig,
    onHide: function onHide() {
      return setShow(false);
    }
  }), (Bridge.platform === 'wework' || Bridge.platform === 'wq') && !type && /*#__PURE__*/React.createElement(ShareChoose, {
    show: show,
    config: config,
    shareTo: shareTo,
    onHide: function onHide() {
      return setShow(false);
    }
  }), (Bridge.platform === 'wework' || Bridge.platform === 'wq') && type && /*#__PURE__*/React.createElement(ShareType, {
    type: type,
    getConfig: getConfig,
    config: config
  }));
}

export default Share;