import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { isUndefined } from 'lodash';

export const useDialogStore = defineStore('dialog', () => {
  type Title = "Error" | "Info" | "Warn";
  const isDialogOpen = ref(false);
  const dialogTitle = ref<Title>("Error");
  const dialogContent = ref("这是一个错误");

  function openDialog(title: Title, content?: string) {
    if (!isUndefined(content) && content.length != 0)
      dialogContent.value = content;
    dialogTitle.value = title;
    isDialogOpen.value = true;
  }

  function closeDialog() {
    isDialogOpen.value = false;
  }

  function info(content?: string) {
    openDialog("Info", content);
  }

  function error(content?: string) {
    openDialog("Error", content);
  }

  function warn(content?: string) {
    openDialog("Warn", content);
  }

  return {
    isDialogOpen,
    dialogTitle,
    dialogContent,
    openDialog,
    closeDialog,
    info,
    error,
    warn
  }
})

