import vue from 'vue'
import toastComponent from './toast.vue'

const ToastConstructor = vue.extend(toastComponent)

let toastDom

function showToast (text, isToast = true, duration = 2500) {
  if (!toastDom) {
    toastDom = new ToastConstructor({
      el: document.createElement('div'),
      data () {
        return {
          text,
          show: true
        }
      }
    })
    document.body.appendChild(toastDom.$el)
  } else {
    toastDom.show = true
    toastDom.text = text
  }

  if (isToast) {
    setTimeout(() => { toastDom.show = false }, duration)
  }
}

function hideToast () {
  if (toastDom) {
    toastDom.show = false
  }
}

function registryToast () {
  vue.prototype.$showTips = showToast
  vue.prototype.$hideTips = hideToast
}

export default registryToast
