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

/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-08-11 11:44:48
 * @LastEditTime: 2021-09-06 09:06:41
 * @FilePath: \young\components\toast\index.tsx
 */
import React from 'react';
import classnames from 'classnames';
import Icon from '../icon';
import Notification from 'rmc-notification';
import { getPrefixCls } from '../common';
import { View } from 'remax/one';
import Loading from '../loading';
var prefixCls = getPrefixCls('toast');
var SHORT = 3;
var _config = {
  duration: SHORT,
  mask: true
};
var messageInstance;
var messageNeedHide;

function getMessageInstance(mask, callback) {
  var _classnames;

  Notification.newInstance({
    prefixCls: prefixCls,
    style: {},
    transitionName: 'am-fade',
    className: classnames((_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.createElement(View, {
        className: "".concat(prefixCls, "-text ").concat(prefixCls, "-text-icon"),
        role: "alert",
        "aria-live": "assertive"
      }, iconType === 'loading' ? /*#__PURE__*/React.createElement(Loading, {
        color: "#fff",
        textSize: "64px",
        style: {
          margin: ' 20px 0'
        }
      }) : /*#__PURE__*/React.createElement(Icon, {
        type: iconType,
        size: "48px",
        color: "#fff"
      }), /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-text-info")
      }, content)) : /*#__PURE__*/React.createElement(View, {
        className: "".concat(prefixCls, "-text"),
        role: "alert",
        "aria-live": "assertive"
      }, /*#__PURE__*/React.createElement(View, null, content)),
      closable: true,
      onClose: function onClose() {
        if (_onClose) {
          _onClose();
        }

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

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