import type { App } from "vue";
import type { ICommonRoot } from "./CommonRoot.vue";
import type { DialogAlertOptions } from "./DialogRoot.vue";
import type { ToastShowProps } from "../feedback/Toast.vue";
import type { ActionSheetOptions } from "./ActionSheetRoot.vue";

let currentRoot : ICommonRoot|null = null;

export function setCurrentRoot(root : ICommonRoot) {
  currentRoot = root;
}
export function NaDialogRoot() : ICommonRoot {
  if (!currentRoot)
    throw new Error("No dialog root found.");
  return currentRoot;
}

export function alert(options: DialogAlertOptions) {
  if (!currentRoot)
    throw new Error("No dialog root found.");
  return currentRoot.alert(options);
}
export function confirm(options: DialogAlertOptions) {
  if (!currentRoot)
    throw new Error("No dialog root found.");
  return currentRoot.confirm(options);
}
export function toast(options: ToastShowProps) {
  if (!currentRoot)
    throw new Error("No dialog root found.");
  return currentRoot.toast(options);
}
export function closeToast() {
  if (!currentRoot)
    throw new Error("No dialog root found.");
  return currentRoot.closeToast();
}
export function actionSheet(options: ActionSheetOptions) {
  if (!currentRoot)
    throw new Error("No dialog root found.");
  return currentRoot.actionSheet(options);
}
export function notify(options: ToastShowProps) {
  if (!currentRoot)
    throw new Error("No dialog root found.");
  return currentRoot.notify(options);
}

export default {
  install(app: App) {
    const na : ICommonRoot = {
      alert,
      confirm,
      toast,
      closeToast,
      actionSheet,
      notify,
    }
    app.config.globalProperties.$na = na;
  }
}
