"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const types = require("../../../utils/types.js");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const error = require("../../../utils/error.js");
const configProvider = require("../../config-provider/src/config-provider.js");
const message$1 = require("./message.js");
const instance = require("./instance.js");
const message2 = require("./message2.js");
const vue = require("vue");
const index = require("../../../../node_modules/@vueuse/shared/index.js");
let seed = 1;
const normalizeAppendTo = (normalized) => {
  if (!normalized.appendTo) normalized.appendTo = document.body;
  else if (shared_esmBundler.isString(normalized.appendTo)) {
    let appendTo = document.querySelector(normalized.appendTo);
    if (!types.isElement(appendTo)) {
      error.debugWarn("ElMessage", "the appendTo option is not an HTMLElement. Falling back to document.body.");
      appendTo = document.body;
    }
    normalized.appendTo = appendTo;
  }
};
const normalizePlacement = (normalized) => {
  if (!normalized.placement && shared_esmBundler.isString(configProvider.messageConfig.placement) && configProvider.messageConfig.placement) normalized.placement = configProvider.messageConfig.placement;
  if (!normalized.placement) normalized.placement = message$1.MESSAGE_DEFAULT_PLACEMENT;
  if (!message$1.messagePlacement.includes(normalized.placement)) {
    error.debugWarn("ElMessage", `Invalid placement: ${normalized.placement}. Falling back to '${message$1.MESSAGE_DEFAULT_PLACEMENT}'.`);
    normalized.placement = message$1.MESSAGE_DEFAULT_PLACEMENT;
  }
};
const normalizeOptions = (params) => {
  const options = !params || shared_esmBundler.isString(params) || vue.isVNode(params) || shared_esmBundler.isFunction(params) ? { message: params } : params;
  const normalized = {
    ...message$1.messageDefaults,
    ...options
  };
  normalizeAppendTo(normalized);
  normalizePlacement(normalized);
  if (types.isBoolean(configProvider.messageConfig.grouping) && !normalized.grouping) normalized.grouping = configProvider.messageConfig.grouping;
  if (types.isNumber(configProvider.messageConfig.duration) && normalized.duration === 3e3) normalized.duration = configProvider.messageConfig.duration;
  if (types.isNumber(configProvider.messageConfig.offset) && normalized.offset === 16) normalized.offset = configProvider.messageConfig.offset;
  if (types.isBoolean(configProvider.messageConfig.showClose) && !normalized.showClose) normalized.showClose = configProvider.messageConfig.showClose;
  if (types.isBoolean(configProvider.messageConfig.plain) && !normalized.plain) normalized.plain = configProvider.messageConfig.plain;
  return normalized;
};
const closeMessage = (instance$1) => {
  const instances = instance.placementInstances[instance$1.props.placement || message$1.MESSAGE_DEFAULT_PLACEMENT];
  const idx = instances.indexOf(instance$1);
  if (idx === -1) return;
  instances.splice(idx, 1);
  const { handler } = instance$1;
  handler.close();
};
const createMessage = ({ appendTo, ...options }, context) => {
  const id = `message_${seed++}`;
  const userOnClose = options.onClose;
  const container = document.createElement("div");
  const props = {
    ...options,
    id,
    onClose: () => {
      userOnClose == null ? void 0 : userOnClose();
      closeMessage(instance2);
    },
    onDestroy: () => {
      vue.render(null, container);
    }
  };
  const vnode = vue.createVNode(message2.default, props, shared_esmBundler.isFunction(props.message) || vue.isVNode(props.message) ? { default: shared_esmBundler.isFunction(props.message) ? props.message : () => props.message } : null);
  vnode.appContext = context || message._context;
  vue.render(vnode, container);
  appendTo.appendChild(container.firstElementChild);
  const vm = vnode.component;
  const instance2 = {
    id,
    vnode,
    vm,
    handler: { close: () => {
      vm.exposed.close();
    } },
    props: vnode.component.props
  };
  return instance2;
};
const message = (options = {}, context) => {
  if (!index.isClient) return { close: () => void 0 };
  const normalized = normalizeOptions(options);
  const instances = instance.getOrCreatePlacementInstances(normalized.placement || message$1.MESSAGE_DEFAULT_PLACEMENT);
  if (normalized.grouping && instances.length) {
    const instance2 = instances.find(({ vnode: vm }) => {
      var _a;
      return ((_a = vm.props) == null ? void 0 : _a.message) === normalized.message;
    });
    if (instance2) {
      instance2.props.repeatNum += 1;
      instance2.props.type = normalized.type;
      return instance2.handler;
    }
  }
  if (types.isNumber(configProvider.messageConfig.max) && instances.length >= configProvider.messageConfig.max) return { close: () => void 0 };
  const instance$1 = createMessage(normalized, context);
  instances.push(instance$1);
  return instance$1.handler;
};
message$1.messageTypes.forEach((type) => {
  message[type] = (options = {}, appContext) => {
    return message({
      ...normalizeOptions(options),
      type
    }, appContext);
  };
});
function closeAll(type) {
  for (const placement in instance.placementInstances) if (shared_esmBundler.hasOwn(instance.placementInstances, placement)) {
    const instances = [...instance.placementInstances[placement]];
    for (const instance2 of instances) if (!type || type === instance2.props.type) instance2.handler.close();
  }
}
function closeAllByPlacement(placement) {
  if (!instance.placementInstances[placement]) return;
  [...instance.placementInstances[placement]].forEach((instance2) => instance2.handler.close());
}
message.closeAll = closeAll;
message.closeAllByPlacement = closeAllByPlacement;
message._context = null;
exports.closeAll = closeAll;
exports.closeAllByPlacement = closeAllByPlacement;
exports.default = message;
