import Vue from "vue";
import AGRCOMP from "./index.vue";

const MFAConstructor = Vue.extend(AGRCOMP);
let MFAINSTANCELIST = [];
const AGR = options => {
    const { Id, IsEn_Us, callBack, cancelBack } = options;
    if (!Id) throw `Id is requred.but ${Id}`
    const id = `AGR${new Date().getTime()}`;
    const instance = new MFAConstructor();
    MFAINSTANCELIST.push(instance)
    instance.id = id;
    instance.vm = instance.$mount();
    if (instance.vm.visible) return;
    document.body.appendChild(instance.vm.$el);
    instance.vm.open({ Id, IsEn_Us });
    instance.vm.$on("success", (Id) => {
        if (callBack) callBack(Id)
    });
    instance.vm.$on("closed", () => {
        if (cancelBack) cancelBack();
        document.body.removeChild(instance.vm.$el);
        instance.vm.$destroy();
        let index = MFAINSTANCELIST.findIndex(item => item.id === id);
        MFAINSTANCELIST.splice(index, 1)
    });
    return instance.vm;
}
AGR.close = () => {
    if (MFAINSTANCELIST.length <= 0) return;
    MFAINSTANCELIST.forEach(item => {
        document.body.removeChild(item.vm.$el);
        item.vm.$destroy();
    })
    MFAINSTANCELIST = [];
}
export default AGR;