import _regeneratorRuntime from "@babel/runtime/regenerator";

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

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 === void 0 ? {} : _ref$config,
      originConfig = _ref.originConfig,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === void 0 ? {} : _ref$maskAttribute,
      onHide = _ref.onHide;
  // 生命周期
  useEffect(function () {
    return function () {
      // 更新分享
      if (originConfig) {
        wx.updateAppMessageShareData(_extends({}, originConfig, {
          success: function success() {
            console.log('还原好友分享成功');
          },
          fail: function fail(res) {
            if (res === void 0) {
              res = {};
            }

            console.log(res.errMsg || '还原好友分享失败, 请稍后再试', {
              mask: false
            });
          }
        }));
        wx.updateTimelineShareData(_extends({}, originConfig, {
          success: function success() {
            console.log('还原朋友圈分享成功');
          },
          fail: function fail(res) {
            if (res === void 0) {
              res = {};
            }

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

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

    function _shareConfig() {
      _shareConfig = _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(res) {
                    if (res === void 0) {
                      res = {};
                    }

                    Bridge.showToast(res.errMsg || '更新好友分享失败, 请稍后再试', {
                      mask: false
                    });
                  }
                }));
                wx.updateTimelineShareData(_extends({}, config, {
                  success: function success() {
                    console.log('更新朋友圈分享成功');
                  },
                  fail: function fail(res) {
                    if (res === void 0) {
                      res = {};
                    }

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

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

    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 (remark) {
    return remark || '';
  };

  return createPortal( /*#__PURE__*/React.createElement("div", _extends({
    className: "mask share-mask" + (maskAttribute.className ? ' ' + maskAttribute.className : '') + (show ? ' active' : '')
  }, maskAttribute, {
    onClick: handlerClick
  }), /*#__PURE__*/React.createElement("div", {
    className: "share-tip-arrow"
  }), /*#__PURE__*/React.createElement("div", {
    className: "share-tip"
  }, /*#__PURE__*/React.createElement("p", null, "1.", locale('点击右上角', 'sharetip_click_on_the_top_right')), /*#__PURE__*/React.createElement("p", null, "2.", locale('点击', 'sharetip_click'), /*#__PURE__*/React.createElement("img", {
    alt: "",
    src: "//res.waiqin365.com/d/seedsui/share/tip_friend.png"
  }), locale('发送给朋友或', 'sharetip_sent_to_friend_or'), /*#__PURE__*/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;