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

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 () {
    // componentDidMount和componentDidUpdate
    return function () {
      // componentWillUnmount
      // 更新分享
      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(() => {
  //   // componentDidUpdate: 更新usernmae
  // }, [state.username]);

  // 更新分享
  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 });
    }
  }));
  // 点击
  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 || {};
  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;