/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

'use strict';

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

var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var message_messageList = require('./messageList.js');
var utils_dom = require('../utils/dom.js');
require('./const.js');
require('./message.js');
require('tdesign-icons-vue');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../config.js');
require('../utils/set-style.js');
require('../utils/classnames.js');
require('../utils/render-tnode.js');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('./props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var instanceMap = new Map();

function handleParams(params) {
  var options = _objectSpread({
    duration: 3e3,
    attach: "body",
    zIndex: message_messageList.DEFAULT_Z_INDEX,
    placement: "top"
  }, params);

  options.content = params.content;
  return options;
}

var MessageFunction = function MessageFunction(props) {
  var options = handleParams(props);
  var attach = options.attach,
      placement = options.placement;
  var attachDom = utils_dom.getAttach(attach);

  if (!instanceMap.get(attachDom)) {
    instanceMap.set(attachDom, {});
  }

  var p = instanceMap.get(attachDom)[placement];

  if (!p) {
    var instance = new message_messageList.MessageList({
      propsData: {
        zIndex: options.zIndex,
        placement: options.placement
      }
    }).$mount();
    instance.add(options);
    instanceMap.get(attachDom)[placement] = instance;
    attachDom.appendChild(instance.$el);
  } else {
    p.add(options);
  }

  return new Promise(function (resolve) {
    var ins = instanceMap.get(attachDom)[placement];
    ins.$nextTick(function () {
      var msg = ins.$children;
      resolve(msg[msg.length - 1]);
    });
  });
};

var showThemeMessage = function showThemeMessage(theme, params, duration) {
  var options = {
    theme: theme
  };

  if (typeof params === "string") {
    options.content = params;
  } else if (_typeof__default["default"](params) === "object" && !(params instanceof Array)) {
    options = _objectSpread(_objectSpread({}, options), params);
  }

  (duration || duration === 0) && (options.duration = duration);
  return MessageFunction(options);
};

var extraApi = {
  info: function info(params, duration) {
    return showThemeMessage("info", params, duration);
  },
  success: function success(params, duration) {
    return showThemeMessage("success", params, duration);
  },
  warning: function warning(params, duration) {
    return showThemeMessage("warning", params, duration);
  },
  error: function error(params, duration) {
    return showThemeMessage("error", params, duration);
  },
  question: function question(params, duration) {
    return showThemeMessage("question", params, duration);
  },
  loading: function loading(params, duration) {
    return showThemeMessage("loading", params, duration);
  },
  close: function close(promise) {
    promise.then(function (instance) {
      return instance.close();
    });
  },
  closeAll: function closeAll() {
    if (instanceMap instanceof Map) {
      instanceMap.forEach(function (attach) {
        Object.keys(attach).forEach(function (placement) {
          var instance = attach[placement];
          instance.list = [];
        });
      });
    }
  }
};
var MessagePlugin = showThemeMessage;
Object.keys(extraApi).forEach(function (funcName) {
  MessagePlugin[funcName] = extraApi[funcName];
});

MessagePlugin.install = function () {
  Vue__default["default"].prototype.$message = MessagePlugin;
};

exports.MessagePlugin = MessagePlugin;
exports["default"] = MessagePlugin;
//# sourceMappingURL=plugin.js.map
