import { shallowRef, createVNode } from 'vue';
import '../../../utils/index.mjs';
import { PureContent } from './PurePanel.mjs';
import Holder from './holder.mjs';
import { wrapPromiseFn } from '../../../utils/vue/util.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

let keyIndex = 0;
function useInternalMessage(messageConfig) {
  const holderRef = 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 wrapPromiseFn((resolve) => {
      originOpen({
        ...restConfig,
        key: mergedKey,
        content: () => createVNode(PureContent, {
          "prefixCls": prefixCls,
          "type": type,
          "icon": typeof icon === "function" ? icon() : icon
        }, {
          default: () => [typeof content === "function" ? content() : content]
        }),
        placement: "top",
        // @ts-expect-error
        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, () => createVNode(Holder, {
    "key": holderKey,
    ...messageConfig,
    "ref": holderRef
  }, null)];
}
function useMessage(messageConfig) {
  return useInternalMessage(messageConfig);
}

export { useMessage as default, useInternalMessage };
//# sourceMappingURL=useMessage.mjs.map
