"use strict";

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

var _react = _interopRequireDefault(require("react"));

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

var _icon = _interopRequireDefault(require("../icon"));

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

var _common = require("../common");

var _one = require("remax/one");

var _loading = _interopRequireDefault(require("../loading"));

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 prefixCls = (0, _common.getPrefixCls)('toast');
var SHORT = 3;
var _config = {
  duration: SHORT,
  mask: true
};
var messageInstance;
var messageNeedHide;

function getMessageInstance(mask, callback) {
  var _classnames;

  _rmcNotification.default.newInstance({
    prefixCls: prefixCls,
    style: {},
    transitionName: 'am-fade',
    className: (0, _classnames2.default)((_classnames = {}, _defineProperty(_classnames, "".concat(prefixCls, "-mask"), mask), _defineProperty(_classnames, "".concat(prefixCls, "-nomask"), !mask), _classnames))
  }, function (notification) {
    return callback && callback(notification);
  });
}

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

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

  var mask = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : _config.mask;
  var iconTypes = {
    info: '',
    success: 'roundcheck',
    fail: 'roundclose',
    offline: 'question',
    loading: 'loading'
  };
  var iconType = iconTypes[type];
  messageNeedHide = false;
  getMessageInstance(mask, function (notification) {
    if (!notification) {
      return;
    }

    if (messageInstance) {
      messageInstance.destroy();
      messageInstance = null;
    }

    if (messageNeedHide) {
      notification.destroy();
      messageNeedHide = false;
      return;
    }

    messageInstance = notification;
    notification.notice({
      duration: duration,
      style: {},
      content: !!iconType ? /*#__PURE__*/_react.default.createElement(_one.View, {
        className: "".concat(prefixCls, "-text ").concat(prefixCls, "-text-icon"),
        role: "alert",
        "aria-live": "assertive"
      }, iconType === 'loading' ? /*#__PURE__*/_react.default.createElement(_loading.default, {
        color: "#fff",
        textSize: "64px",
        style: {
          margin: ' 20px 0'
        }
      }) : /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: iconType,
        size: "48px",
        color: "#fff"
      }), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(prefixCls, "-text-info")
      }, content)) : /*#__PURE__*/_react.default.createElement(_one.View, {
        className: "".concat(prefixCls, "-text"),
        role: "alert",
        "aria-live": "assertive"
      }, /*#__PURE__*/_react.default.createElement(_one.View, null, content)),
      closable: true,
      onClose: function onClose() {
        if (_onClose) {
          _onClose();
        }

        notification.destroy();
        notification = null;
        messageInstance = null;
      }
    });
  });
}

var _default = {
  SHORT: SHORT,
  LONG: 8,
  show: function show(content, duration, mask) {
    return notice(content, 'info', duration, function () {}, mask);
  },
  info: function info(content, duration, onClose, mask) {
    return notice(content, 'info', duration, onClose, mask);
  },
  success: function success(content, duration, onClose, mask) {
    return notice(content, 'success', duration, onClose, mask);
  },
  fail: function fail(content, duration, onClose, mask) {
    return notice(content, 'fail', duration, onClose, mask);
  },
  offline: function offline(content, duration, onClose, mask) {
    return notice(content, 'offline', duration, onClose, mask);
  },
  loading: function loading(content, duration, onClose, mask) {
    return notice(content, 'loading', duration, onClose, mask);
  },
  hide: function hide() {
    if (messageInstance) {
      messageInstance.destroy();
      messageInstance = null;
    } else {
      messageNeedHide = true;
    }
  },
  config: function config() {
    var conf = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var _conf$duration = conf.duration,
        duration = _conf$duration === void 0 ? SHORT : _conf$duration,
        mask = conf.mask;
    _config.duration = duration;

    if (mask === false) {
      _config.mask = false;
    }
  }
};
exports.default = _default;