import Vue from 'vue'
import ConfirmLayer from './index.vue'

let $vm = null
const ConfirmLayerPlugin = Vue.extend(ConfirmLayer)

const callback = action => {
  if (action === 'confirm') {
    $vm.reslove(action)
  } else {
    $vm.reject(action)
  }
  $vm = null
}

const confirm = (content, title, options) =>
  new Promise((reslove, reject) => {
    const propsData = { title, content, ...options }
    $vm = new ConfirmLayerPlugin({ propsData }).$mount()
    $vm.reject = reject
    $vm.reslove = reslove
    $vm.callback = callback
    $vm.visible = true
    document.body.appendChild($vm.$el)
  })

export default {
  install(Vue) {
    Vue.prototype.$confirm = confirm
  }
}
