let dom = null

async function asyncConstructor (Vue, comp) {
  if (!comp) {
    let component = await import('./index.vue')
    comp = Vue.extend(component.default)

    return comp
  }

  return comp
}

let imgPreview = {}
imgPreview.install = function (Vue) {
  let instance

  Vue.prototype.$showImgPreview = async (imgUrl) => {
    let Constructor = await asyncConstructor(Vue, dom)

    if (instance) {
      document.body.removeChild(instance.$el)
      instance = null
    }

    instance = new Constructor({
      el: document.createElement('div'),
      data () {
        return {
          visible: true,
          imgUrl
        }
      }
    })

    document.body.appendChild(instance.$el)
  }
}

export default imgPreview
