'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
var PurePanel = require('./PurePanel.cjs');
var holder = require('./holder.cjs');
var util = require('../../../utils/vue/util.cjs');
var _class = require('../../../utils/dom/class.cjs');

let keyIndex = 0;
function useInternalMessage(messageConfig) {
  const holderRef = vue.shallowRef(null);
  const holderKey = Symbol("messageHolderKey");
  const close = (key) => {
    var _a;
    (_a = holderRef.value) == null ? void 0 : _a.close(key);
  };
  const open = (config) => {
    if (!holderRef.value) {
      const fakeResult = () => {
      };
      fakeResult.then = () => {
      };
      return fakeResult;
    }
    const {
      open: originOpen,
      prefixCls,
      hashId
    } = holderRef.value;
    const noticePrefixCls = `${prefixCls}-notice`;
    const {
      content,
      icon,
      type,
      key,
      class: className,
      onClose,
      ...restConfig
    } = config;
    let mergedKey = key;
    if (mergedKey === void 0 || mergedKey === null) {
      keyIndex += 1;
      mergedKey = `antd-message-${keyIndex}`;
    }
    return util.wrapPromiseFn((resolve) => {
      originOpen({
        ...restConfig,
        key: mergedKey,
        content: () => vue.createVNode(PurePanel.PureContent, {
          "prefixCls": prefixCls,
          "type": type,
          "icon": typeof icon === "function" ? icon() : icon
        }, {
          default: () => [typeof content === "function" ? content() : content]
        }),
        placement: "top",
        // @ts-expect-error
        class: _class.classNames(type && `${noticePrefixCls}-${type}`, hashId, className),
        onClose: () => {
          onClose == null ? void 0 : onClose();
          resolve();
        }
      });
      return () => {
        close(mergedKey);
      };
    });
  };
  const destroy = (key) => {
    var _a;
    if (key !== void 0)
      close(key);
    else
      (_a = holderRef.value) == null ? void 0 : _a.destroy();
  };
  const wrapAPI = {
    open,
    destroy
  };
  const keys = ["info", "success", "warning", "error", "loading"];
  keys.forEach((type) => {
    const typeOpen = (jointContent, duration, onClose) => {
      let config;
      if (jointContent && typeof jointContent === "object" && "content" in jointContent) {
        config = jointContent;
      } else {
        config = {
          content: jointContent
        };
      }
      let mergedDuration;
      let mergedOnClose;
      if (typeof duration === "function") {
        mergedOnClose = duration;
      } else {
        mergedDuration = duration;
        mergedOnClose = onClose;
      }
      const mergedConfig = {
        onClose: mergedOnClose,
        duration: mergedDuration,
        ...config,
        type
      };
      return open(mergedConfig);
    };
    wrapAPI[type] = typeOpen;
  });
  return [wrapAPI, () => vue.createVNode(holder.default, {
    "key": holderKey,
    ...messageConfig,
    "ref": holderRef
  }, null)];
}
function useMessage(messageConfig) {
  return useInternalMessage(messageConfig);
}

exports.default = useMessage;
exports.useInternalMessage = useInternalMessage;
//# sourceMappingURL=useMessage.cjs.map
