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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

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

function Share(_ref) {
  // 显隐弹窗
  var handlerClick = function () {
    var _ref2 = _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 === 'undefined' ? 'undefined' : _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, this);
    }));

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

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

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

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

export default Share;