/**
 * ldesign v0.2.3
 * (c) 2022年9月24日 swimly
 * @license MIT
 */

'use strict';

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

var Vue = require('vue');
var components_message_list = require('./list.js');
var utils_dom = require('../../utils/dom.js');
require('./const.js');
require('./message.js');
require('@ldesign/icons-vue');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../../chunks/defineProperty-56324511.js');
require('../../chunks/set-style.js');
require('../../chunks/helper2.js');
require('../../config.js');
require('../../utils/render-node.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../utils/transfer-dom.js');
require('../loading/props.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('../../utils/with-install.js');
require('../loading/plugin.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('./props.js');
require('./animation.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" && !(params instanceof Array)) {
    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;
};

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