import i18n from '@/util/lang/lang'

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
}

const Invite = {}
Invite.install = function (Vue, store, router) {
  let instance

  Vue.prototype.$showInvite = async (type, ext) => {
    let Constructor = await asyncConstructor(Vue, dom)

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

    instance = new Constructor({
      el: document.createElement('div'),
      router,
      computed: {
        $store () { return store }
      },
      i18n,
      data () {
        return {
          visible: true,
          inviteType: type, // 弹框邀请类型{1:建群,2:邀请好友进群,3:删除群好友,4:添加群管理,5:转让群主}
          ext // 额外数据
        }
      }
    })
    document.body.appendChild(instance.$el)
  }
}

export default Invite
