"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Vue = require("vue");
const mountComponent = require("../utils/mount-component.js");
const Toast = require("./Toast.js");
const basic = require("../utils/basic.js");
const defaultOptions = {
  icon: "",
  type: "text",
  message: "",
  className: "",
  overlay: false,
  onClose: void 0,
  onOpened: void 0,
  duration: 2e3,
  teleport: "body",
  iconSize: void 0,
  iconPrefix: void 0,
  position: "middle",
  transition: "van-fade",
  forbidClick: false,
  loadingType: void 0,
  overlayClass: "",
  overlayStyle: void 0,
  closeOnClick: false,
  closeOnClickOverlay: false
};
let queue = [];
let allowMultiple = false;
let currentOptions = basic.extend({}, defaultOptions);
const defaultOptionsMap = /* @__PURE__ */ new Map();
function parseOptions(message) {
  if (basic.isObject(message)) {
    return message;
  }
  return {
    message
  };
}
function createInstance() {
  const {
    instance,
    unmount
  } = mountComponent.mountComponent({
    setup() {
      const message = Vue.ref("");
      const {
        open,
        state,
        close,
        toggle
      } = mountComponent.usePopupState();
      const onClosed = () => {
        if (allowMultiple) {
          queue = queue.filter((item) => item !== instance);
          unmount();
        }
      };
      const render = () => {
        const attrs = {
          onClosed,
          "onUpdate:show": toggle
        };
        return Vue.createVNode(Toast.default, Vue.mergeProps(state, attrs), null);
      };
      Vue.watch(message, (val) => {
        state.message = val;
      });
      Vue.getCurrentInstance().render = render;
      return {
        open,
        close,
        message
      };
    }
  });
  return instance;
}
function getInstance() {
  if (!queue.length || allowMultiple) {
    const instance = createInstance();
    queue.push(instance);
  }
  return queue[queue.length - 1];
}
function showToast(options = {}) {
  if (!basic.inBrowser) {
    return {};
  }
  const toast = getInstance();
  const parsedOptions = parseOptions(options);
  toast.open(basic.extend({}, currentOptions, defaultOptionsMap.get(parsedOptions.type || currentOptions.type), parsedOptions));
  return toast;
}
const createMethod = (type) => (options) => showToast(basic.extend({
  type
}, parseOptions(options)));
const showLoadingToast = createMethod("loading");
const showSuccessToast = createMethod("success");
const showFailToast = createMethod("fail");
const closeToast = (all) => {
  var _a;
  if (queue.length) {
    if (all) {
      queue.forEach((toast) => {
        toast.close();
      });
      queue = [];
    } else if (!allowMultiple) {
      queue[0].close();
    } else {
      (_a = queue.shift()) == null ? void 0 : _a.close();
    }
  }
};
const allowMultipleToast = (value = true) => {
  allowMultiple = value;
};
exports.allowMultipleToast = allowMultipleToast;
exports.closeToast = closeToast;
exports.showFailToast = showFailToast;
exports.showLoadingToast = showLoadingToast;
exports.showSuccessToast = showSuccessToast;
exports.showToast = showToast;
