/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var Vue = require('vue');
var utils_dom = require('../../utils/dom.js');
var components_message_list = require('./list.js');
require('raf');
require('lodash/isString');
require('../../chunks/log.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../utils/easing.js');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../../utils/mixins.js');
require('./const.js');
require('./message.js');
require('@ldesign/icons-vue');
require('../loading/index.js');
require('../../utils/with-install.js');
require('../../config.js');
require('../loading/loading.js');
require('../../utils/render-node.js');
require('../../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../../chunks/defineProperty-838e4192.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.js');
require('../loading/plugin.js');
require('./animation.js');
require('./props.js');

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

var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

const instanceMap = /* @__PURE__ */new Map();
const defaultMessageOptions = {
  duration: 3e3,
  attach: "body",
  zIndex: components_message_list.DEFAULT_Z_INDEX,
  placement: "top"
};
function handleParams(params) {
  const options = {
    ...defaultMessageOptions,
    ...params
  };
  options.content = params.content;
  return options;
}
const MessageFunction = props => {
  const options = handleParams(props);
  const {
    attach,
    placement
  } = options;
  const attachDom = utils_dom.getAttach(attach);
  if (!instanceMap.get(attachDom)) instanceMap.set(attachDom, {});
  const p = instanceMap.get(attachDom)[placement];
  if (!p) {
    const instance = new components_message_list.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(resolve => {
    const ins = instanceMap.get(attachDom)[placement];
    ins.$nextTick(() => {
      const msg = ins.$children;
      resolve(msg[msg.length - 1]);
    });
  });
};
const showThemeMessage = (theme, params, duration) => {
  let options = {
    theme
  };
  if (typeof params === "string") options.content = params;else if (typeof params === "object" && !Array.isArray(params)) options = {
    ...options,
    ...params
  };
  (duration || duration === 0) && (options.duration = duration);
  return MessageFunction(options);
};
const setMessageConfig = _ref => {
  let {
    placement,
    attach,
    offset,
    zIndex
  } = _ref;
  attach && (defaultMessageOptions.attach = attach);
  offset && (defaultMessageOptions.offset = offset);
  placement && (defaultMessageOptions.placement = placement);
  zIndex && (defaultMessageOptions.zIndex = zIndex);
};
const extraApi = {
  info: (params, duration) => showThemeMessage("info", params, duration),
  success: (params, duration) => showThemeMessage("success", params, duration),
  warning: (params, duration) => showThemeMessage("warning", params, duration),
  error: (params, duration) => showThemeMessage("error", params, duration),
  question: (params, duration) => showThemeMessage("question", params, duration),
  loading: (params, duration) => showThemeMessage("loading", params, duration),
  close: promise => {
    promise.then(instance => instance.close());
  },
  closeAll: () => {
    if (instanceMap instanceof Map) {
      instanceMap.forEach(attach => {
        Object.keys(attach).forEach(placement => {
          const instance = attach[placement];
          instance.list = [];
        });
      });
    }
  },
  config: setMessageConfig
};
const MessagePlugin = showThemeMessage;
Object.keys(extraApi).forEach(funcName => {
  MessagePlugin[funcName] = extraApi[funcName];
});
MessagePlugin.install = () => {
  Vue__default["default"].prototype.$message = MessagePlugin;
};
const message = MessagePlugin;

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