import { createApp } from "vue";
import { createEventBus } from "@grozav/utils";
import { InklineKey, InklineToastKey, IToastContainer, IconsPlugin } from "@inkline/inkline";
export const toastEventBus = createEventBus();
export const createToastService = () => ({
  show: (options) => {
    toastEventBus.emit("show", options);
  },
  hide: (options) => {
    toastEventBus.emit("hide", options);
  },
  hideAll: () => {
    toastEventBus.emit("hideAll", {});
  }
});
export const ToastPlugin = {
  install: (app, { inkline }) => {
    const toastService = createToastService();
    const toastApp = createApp(IToastContainer);
    toastApp.provide(InklineKey, inkline);
    toastApp.use(IconsPlugin);
    if (typeof window !== "undefined") {
      const containerId = "inkline-toast-container";
      let container = document.querySelector(`#${containerId}`);
      if (!container) {
        container = document.createElement("div");
        container.id = containerId;
        document.body.appendChild(container);
      }
      toastApp.mount(container);
    }
    app.config.globalProperties.$toast = toastService;
    app.provide(InklineToastKey, toastService);
  }
};
