import { ref } from 'vue'
import { eventBus, EventNames } from '@/utils/eventBus'

// 模拟 Toast 接口
const toastImpl = {
  show: (options: any) => eventBus.emit(EventNames.SHOW_TOAST, { type: 'show', options }),
  success: (options: any) => eventBus.emit(EventNames.SHOW_TOAST, { type: 'success', options }),
  error: (options: any) => eventBus.emit(EventNames.SHOW_TOAST, { type: 'error', options }),
  warning: (options: any) => eventBus.emit(EventNames.SHOW_TOAST, { type: 'warning', options }),
  info: (options: any) => eventBus.emit(EventNames.SHOW_TOAST, { type: 'info', options }),
  loading: (options: any) => eventBus.emit(EventNames.SHOW_TOAST, { type: 'loading', options }),
  close: () => eventBus.emit(EventNames.SHOW_TOAST, { type: 'close' })
}

// 模拟 Message 接口
const messageImpl = {
  alert: (options: any) => {
    return new Promise((resolve, reject) => {
      eventBus.emit(EventNames.SHOW_MESSAGE, {
        type: 'alert',
        options,
        resolve,
        reject
      })
    })
  },
  confirm: (options: any) => {
    return new Promise((resolve, reject) => {
      eventBus.emit(EventNames.SHOW_MESSAGE, {
        type: 'confirm',
        options,
        resolve,
        reject
      })
    })
  },
  prompt: (options: any) => {
    return new Promise((resolve, reject) => {
      eventBus.emit(EventNames.SHOW_MESSAGE, {
        type: 'prompt',
        options,
        resolve,
        reject
      })
    })
  },
  close: () => eventBus.emit(EventNames.SHOW_MESSAGE, { type: 'close' })
}

// Auth 授权登录
const authImpl = {
  exec: () => {
    return new Promise((resolve, reject) => {
      eventBus.emit(EventNames.AUTH, { resolve, reject })
    })
  }
}



// 保持 ref 结构以兼容现有代码调用方式 (message.value.alert)
const toast = ref(toastImpl)
const message = ref(messageImpl)
const auth = ref(authImpl)

export const useGlobal = () => {

  return {
    toast,
    message,
    auth,
  }
}
