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

import React, { useEffect, useContext } from 'react';
import { createPortal } from 'react-dom';
import Bridge from './../Bridge';
import Context from '../Context/instance.js';

function ShareTip(_ref) {
  var portal = _ref.portal,
      show = _ref.show,
      _ref$config = _ref.config,
      config = _ref$config === undefined ? {} : _ref$config,
      originConfig = _ref.originConfig,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === undefined ? {} : _ref$maskAttribute,
      onHide = _ref.onHide;

  // 生命周期
  useEffect(function () {
    return function () {
      // 更新分享
      if (originConfig) {
        wx.updateAppMessageShareData(_extends({}, originConfig, {
          success: function success() {
            console.log('还原好友分享成功');
          },
          fail: function fail() {
            var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

            console.log(res.errMsg || '还原好友分享失败, 请稍后再试', { mask: false });
          }
        }));
        wx.updateTimelineShareData(_extends({}, originConfig, {
          success: function success() {
            console.log('还原朋友圈分享成功');
          },
          fail: function fail() {
            var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

            console.log(res.errMsg || '还原朋友圈分享失败, 请稍后再试', { mask: false });
          }
        }));
      }
    };
  });

  useEffect(function () {
    var shareConfig = function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                // 更新分享
                wx.updateAppMessageShareData(_extends({}, config, {
                  success: function success() {
                    console.log('更新好友分享成功');
                  },
                  fail: function fail() {
                    var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

                    Bridge.showToast(res.errMsg || '更新好友分享失败, 请稍后再试', { mask: false });
                  }
                }));
                wx.updateTimelineShareData(_extends({}, config, {
                  success: function success() {
                    console.log('更新朋友圈分享成功');
                  },
                  fail: function fail() {
                    var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

                    Bridge.showToast(res.errMsg || '更新朋友圈分享失败, 请稍后再试', { mask: false });
                  }
                }));

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

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

    if (!show) return;

    shareConfig();
  }, [show]);

  // 点击
  var handlerClick = function handlerClick(e) {
    var target = e.target;
    e.stopPropagation();
    if (target.classList.contains('share-mask')) {
      if (onHide) onHide(e);
    }
  };
  // context
  var context = useContext(Context) || {};
  var locale = context.locale || function (key) {
    return key || '';
  };
  return createPortal(React.createElement(
    'div',
    _extends({ className: 'mask share-mask' + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : '') }, maskAttribute, { onClick: handlerClick }),
    React.createElement('div', { className: 'share-tip-arrow' }),
    React.createElement(
      'div',
      { className: 'share-tip' },
      React.createElement(
        'p',
        null,
        '1.',
        locale('点击右上角', 'sharetip_click_on_the_top_right')
      ),
      React.createElement(
        'p',
        null,
        '2.',
        locale('点击', 'sharetip_click'),
        React.createElement('img', { alt: '', src: '//res.waiqin365.com/d/seedsui/share/tip_friend.png' }),
        locale('发送给朋友或', 'sharetip_sent_to_friend_or'),
        React.createElement('img', { alt: '', src: '//res.waiqin365.com/d/seedsui/share/tip_moments.png' }),
        locale('分享给朋友圈', 'sharetip_share_to_moments')
      )
    )
  ), portal || context.portal || document.getElementById('root') || document.body);
}

export default ShareTip;