'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _antDesignVue = require('ant-design-vue');

var _vcNotification = require('ant-design-vue/es/vc-notification');

var _vcNotification2 = _interopRequireDefault(_vcNotification);

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

/**
 * @文件说明: 地图内置组件 - 地图Message消息提示
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-13 16:38:03
 */

var Message = function () {
  function Message(id) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    (0, _classCallCheck3['default'])(this, Message);

    var opts = (0, _extends3['default'])({
      duration: 3,
      top: null,
      getContainer: function getContainer() {
        return document.body;
      },
      maxCount: 1
    }, options);

    this.id = id;
    this.messageInstance = null;
    this.key = 1;
    this.prefixCls = 'pjtools-map-message';
    this.transitionName = 'move-up';
    this.duration = opts.duration;
    this.top = opts.top;
    this.getContainer = opts.getContainer;
    this.maxCount = opts.maxCount;
  }

  (0, _createClass3['default'])(Message, [{
    key: 'open',
    value: function open(args) {
      var _this = this;

      var h = this.$createElement;

      var duration = args.duration !== undefined ? args.duration : this.duration;
      var iconType = {
        info: 'info-circle',
        success: 'check-circle',
        error: 'close-circle',
        warning: 'exclamation-circle',
        loading: 'loading'
      }[args.type];

      var target = this.id + '_' + this.key++;
      var closePromise = new Promise(function (resolve) {
        var callback = function callback() {
          if (typeof args.onClose === 'function') {
            args.onClose();
          }
          return resolve(true);
        };

        var openMessage = function openMessage(instance) {
          instance.notice({
            key: target,
            duration: duration,
            style: {},
            content: function content(h) {
              return h(
                'div',
                { 'class': _this.prefixCls + '-custom-content' + (args.type ? ' ' + _this.prefixCls + '-' + args.type : '') },
                [args.icon ? typeof args.icon === 'function' ? args.icon(h) : args.icon : iconType ? h(_antDesignVue.Icon, {
                  attrs: { type: iconType, theme: iconType === 'loading' ? 'outlined' : 'filled' }
                }) : '', h('span', [typeof args.content === 'function' ? args.content(h) : args.content])]
              );
            },
            onClose: callback
          });
        };

        if (_this.messageInstance) {
          openMessage(_this.messageInstance);
        } else {
          _vcNotification2['default'].newInstance({
            prefixCls: _this.prefixCls,
            transitionName: _this.transitionName,
            style: { top: _this.top },
            getContainer: _this.getContainer,
            maxCount: _this.maxCount
          }, function (instance) {
            if (_this.messageInstance) {
              openMessage(_this.messageInstance);
            } else {
              _this.messageInstance = instance;
              openMessage(instance);
            }
          });
        }
      });
      var result = function result() {
        if (_this.messageInstance) {
          _this.messageInstance.removeNotice(target);
        }
      };
      result.then = function (filled, rejected) {
        return closePromise.then(filled, rejected);
      };
      result.promise = closePromise;

      return result;
    }
  }, {
    key: 'config',
    value: function config() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      if (options.top !== undefined) {
        this.top = options.top;
        this.messageInstance = null;
      }
      if (options.duration !== undefined) {
        this.duration = options.duration;
      }
      if (options.getContainer !== undefined) {
        this.getContainer = options.getContainer;
      }
      if (options.maxCount !== undefined) {
        this.maxCount = options.maxCount;
        this.messageInstance = null;
      }
    }
  }, {
    key: 'success',
    value: function success(content, duration, onClose) {
      if (typeof duration === 'function') {
        onClose = duration;
        duration = undefined;
      }
      return this.open({ content: content, duration: duration, type: 'success', onClose: onClose });
    }
  }, {
    key: 'info',
    value: function info(content, duration, onClose) {
      if (typeof duration === 'function') {
        onClose = duration;
        duration = undefined;
      }
      return this.open({ content: content, duration: duration, type: 'info', onClose: onClose });
    }
  }, {
    key: 'warning',
    value: function warning(content, duration, onClose) {
      if (typeof duration === 'function') {
        onClose = duration;
        duration = undefined;
      }
      return this.open({ content: content, duration: duration, type: 'warning', onClose: onClose });
    }
  }, {
    key: 'warn',
    value: function warn(content, duration, onClose) {
      if (typeof duration === 'function') {
        onClose = duration;
        duration = undefined;
      }
      return this.open({ content: content, duration: duration, type: 'warning', onClose: onClose });
    }
  }, {
    key: 'error',
    value: function error(content, duration, onClose) {
      if (typeof duration === 'function') {
        onClose = duration;
        duration = undefined;
      }
      return this.open({ content: content, duration: duration, type: 'error', onClose: onClose });
    }
  }, {
    key: 'loading',
    value: function loading(content) {
      var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
      var onClose = arguments[2];

      if (typeof duration === 'function') {
        onClose = duration;
        duration = 0;
      }
      return this.open({ content: content, duration: duration, type: 'loading', onClose: onClose });
    }
  }, {
    key: 'destroy',
    value: function destroy() {
      if (this.messageInstance) {
        this.messageInstance.destroy();
        this.messageInstance = null;
      }
    }
  }]);
  return Message;
}();

exports['default'] = Message;