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

import Vue from 'vue';
import { MessageList, DEFAULT_Z_INDEX } from './list.js';
import { getAttach } from '../../utils/dom.js';
import './const.js';
import './message.js';
import '@ldesign/icons-vue';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../../_chunks/dep-1dd6d50e.js';
import '../../_chunks/dep-1aed710a.js';
import '../../_chunks/dep-d640115b.js';
import '../../config.js';
import '../../utils/render-node.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-b1584b65.js';
import '../../utils/mixins.js';
import '../../utils/with-install.js';
import '../loading/plugin.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import './props.js';
import './animation.js';

const instanceMap = /* @__PURE__ */new Map();
const defaultMessageOptions = {
  duration: 3e3,
  attach: "body",
  zIndex: 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 = getAttach(attach);

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

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

  if (!p) {
    const instance = new 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.prototype.$message = MessagePlugin;
};

export { MessagePlugin, MessagePlugin as default };
//# sourceMappingURL=plugin.js.map
