import { h, render } from 'vue'
import Toast from './toast.vue'

class ToastCreator {
  constructor(options) {
    this.container = document.createElement('div')
    this.options = options
  }

  createToast() {
    const { message, type, duration } = this.options
    const toast = h(Toast, {
      message,
      type,
      duration,
    })
    document.body.insertBefore(this.container, document.body.firstChild)
    render(toast, this.container)
    setTimeout(() => {
      this.destroyToast()
    }, duration)
  }

  destroyToast() {
    if (this.container) {
      // this.container.classList.add('animate-toastAnimation')
      render(null, this.container)
      document.body.removeChild(this.container)
      this.container = null
    }
  }
}

export default ToastCreator
