"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _classnames2 = _interopRequireDefault(require("classnames"));

var React = _interopRequireWildcard(require("react"));

var _rmcNotification = _interopRequireDefault(require("rmc-notification"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var messageInstance;
var touchTime;
var touchClientY;
var t1;
var prefixCls = 'eh-toptip'; // 显示弹窗

function showNotice() {
  var notices = document.getElementsByClassName("".concat(prefixCls, "-notice"));

  if (notices && notices.length) {
    var noticeElement = notices[0];
    noticeElement.className += " ".concat(prefixCls, "-notice-show");
  }
} // 隐藏弹窗


function hideNotice() {
  var notices = document.getElementsByClassName("".concat(prefixCls, "-notice"));

  if (notices && notices.length) {
    var noticeElement = notices[0];
    noticeElement.className = noticeElement.className.replace("".concat(prefixCls, "-notice-show"), "".concat(prefixCls, "-notice-hide"));
  }
} // 销毁弹窗


function destroy() {
  if (t1) {
    clearTimeout(t1);
    t1 = undefined;
  }

  if (messageInstance) {
    var instanceBak = messageInstance;
    messageInstance = null;
    hideNotice();
    setTimeout(function () {
      instanceBak.destroy();
    }, 175);
  }
}

function onTouchEnd(e) {
  e.stopPropagation();
  e.preventDefault();
  var currentClientY = e.changedTouches[0].clientY;
  var currentTime = new Date().getTime(); // 时间小于300ms时，当做点击处理

  if (touchTime && currentTime - touchTime <= 300) {
    destroy();
  } else if (touchClientY && currentClientY <= touchClientY) {
    // 向上滑动时，隐藏
    destroy();
  }
}

function onTouchStart(e) {
  touchTime = new Date().getTime();
  touchClientY = e.touches[0].clientY;
}

function onClick(e) {
  e.stopPropagation();
}

function getMessageInstance(type, callback) {
  var _classnames;

  destroy();

  _rmcNotification["default"].newInstance({
    prefixCls: prefixCls,
    style: {},
    // clear rmc-notification default style
    transitionName: 'eh-fade',
    className: (0, _classnames2["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefixCls, "-wrap"), 1), _defineProperty(_classnames, "".concat(prefixCls, "-info"), type === 'info'), _defineProperty(_classnames, "".concat(prefixCls, "-error"), type === 'error'), _defineProperty(_classnames, "".concat(prefixCls, "-tooltip"), type === 'tooltip'), _classnames))
  }, function (notification) {
    return callback && callback(notification);
  });
}

function notice() {
  var content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var type = arguments.length > 1 ? arguments[1] : undefined;
  var duration = arguments.length > 2 ? arguments[2] : undefined;

  var _onClose = arguments.length > 3 ? arguments[3] : undefined;

  var maxLength; // content的最大长度

  switch (type) {
    case 'tooltip':
      duration = duration || 99999;
      maxLength = 30;
      break;

    case 'info':
    case 'error':
    default:
      duration = duration || 1.5;
      maxLength = 14;
      break;
  }

  content = content.substring(0, maxLength);
  getMessageInstance(type, function (notification) {
    messageInstance = notification;
    notification.notice({
      style: {},
      duration: 99999,
      content: React.createElement("div", {
        className: "".concat(prefixCls, "-text"),
        role: "alert",
        onTouchStart: type === 'tooltip' ? onTouchStart : undefined,
        onTouchEnd: type === 'tooltip' ? onTouchEnd : undefined,
        onClick: onClick
      }, React.createElement("div", null, content), type === 'tooltip' ? React.createElement("div", {
        className: "".concat(prefixCls, "-bar")
      }) : null),
      closable: true,
      onClose: function onClose() {
        if (_onClose) {
          _onClose();
        }
      }
    });

    function show() {
      showNotice();
      if (t1) clearTimeout(t1);
      t1 = setTimeout(destroy, duration * 1000);
    }

    if (messageInstance) {
      setTimeout(show, 175);
    } else {
      show(true);
    }
  });
}

var _default = {
  info: function info(content, duration, onClose) {
    return notice(content, 'info', duration, onClose);
  },
  error: function error(content, duration, onClose) {
    return notice(content, 'error', duration, onClose);
  },
  tooltip: function tooltip(content, duration, onClose) {
    return notice(content, 'tooltip', duration, onClose);
  },
  hide: function hide() {
    destroy();
  }
};
exports["default"] = _default;
module.exports = exports.default;