import { createApp } from 'vue'
import ToastMessage from './message.vue'
import vuetify from '@/plugins/vuetify'

// var timeId
const toastMessage  = (options)  => {
  const rootNode = document.createElement("div")
  document.querySelector('.v-application').appendChild(rootNode)
  const app = createApp(ToastMessage, options)
  app.use(vuetify)
  app.mount(rootNode)
  const { timeout } = options || {}
  // clearTimeout(timeId)
  setTimeout(() => {
      app.unmount()
      rootNode.remove()
  }, timeout || 3000)
}
toastMessage.success = (message, variant, timeout) => {
  toastMessage({ message, color: 'success', variant, timeout })
}
toastMessage.error = (message, variant, timeout) => {
  toastMessage({ message, color: 'error', variant, timeout })
}
toastMessage.info = (message, variant, timeout) => {
  toastMessage({ message, color: 'info', variant, timeout })
}
toastMessage.warning = (message, variant, timeout) => {
  toastMessage({ message, color: 'warning', variant, timeout })
}
toastMessage.point = (message, variant, timeout) => {
  toastMessage({ message, color: 'point', variant, timeout })
}
// 注册插件app.use()会自动执行install函数
toastMessage.install = (app) => {
  app.config.globalProperties.Snackbar = toastMessage
}

export default toastMessage