import Vue from "vue";
// import bus from '../../util/bus.js'
// import book1 from './children/book-1.vue'
// import book2 from './children/book-2.vue'
// import chapter1 from './children/chapter-1.vue'
// import chapter2 from './children/chapter-2.vue'
// import server from './children/server.vue'
// import signActive from './children/sign-active.vue'

// const Book1 = Vue.extend(book1)
// const Book2 = Vue.extend(book2)
// const Chapter1 = Vue.extend(chapter1)
// const Chapter2 = Vue.extend(chapter2)
// const Server = Vue.extend(server)
// const SignActive = Vue.extend(signActive)

// // 全本书币购买
// bus.$on('close:book1', function () {
//   showBook1.status = false
// })
// export function showBook1(data, methods) {
//   if (showBook1.status) return
//   showBook1.status = true
//   const instance = new Book1({
//     el: document.createElement('div'),
//     data,
//     methods,
//   })
//   document.body.appendChild(instance.$el)
// }

// // 全本rmb购买
// bus.$on('close:book2', function () {
//   showBook2.status = false
// })
// export function showBook2(data, methods) {
//   if (showBook2.status) return
//   showBook2.status = true
//   const instance = new Book2({
//     el: document.createElement('div'),
//     data,
//     methods,
//   })
//   document.body.appendChild(instance.$el)
// }

// // 章节订购余额不足提示充值
// bus.$on('close:chapter1', function () {
//   showChapter1.status = false
// })
// export function showChapter1(data) {
//   if (showChapter1.status) return
//   showChapter1.status = true
//   const instance = new Chapter1({
//     el: document.createElement('div'),
//     data,
//   })
//   document.body.appendChild(instance.$el)
// }

// // 章节订购(书币)
// bus.$on('close:chapter2', function () {
//   showChapter2.status = false
// })
// export function showChapter2(data, methods) {
//   if (showChapter2.status) return
//   showChapter2.status = true
//   const instance = new Chapter2({
//     el: document.createElement('div'),
//     data,
//     methods
//   })
//   document.body.appendChild(instance.$el)
// }

// // 客服
// bus.$on('close:server', function () {
//   showServer.status = false
// })
// export function showServer() {
//   if (showServer.status) return
//   showServer.status = true
//   const instance = new Server({
//     el: document.createElement('div'),
//   })
//   document.body.appendChild(instance.$el)
// }
// // 签到活动
// bus.$on('close:sign-active', function () {
//   showSingActive.status = false
// })
// export function showSingActive() {
//   if (showSingActive.status) return
//   showSingActive.status = true
//   const instance = new SignActive({
//     el: document.createElement('div'),
//   })
//   document.body.appendChild(instance.$el)
// }

import book1 from "./children/book-1.vue";
import book2 from "./children/book-2.vue";
import book3 from "./children/book-3.vue";
import book4 from "./children/book-4.vue";
import chapter1 from "./children/chapter-1.vue";
import chapter2 from "./children/chapter-2.vue";
import server from "./children/server.vue";
import singActive from "./children/sign-active.vue";
import weakSubscribe from "./children/weakSubscribe.vue";
import sign from "./children/sign.vue";
import payForGift from "./children/pay-for-gift.vue";
import phoneBinding from "./children/phone-binding.vue";

let instance = null;
function promptboxConstructor(component) {
  const Component = Vue.extend(component);
  return function(params = {}) {
    if (instance) return;
    return new Promise((resolve, reject) => {
      instance = new Component({
        el: document.createElement("div"),
        data: Object.assign({ visible: true }, params),
        methods: {
          resolve,
          reject,
          confirm() {
            this.visible = false;
            resolve(this.$data);
          }
        },
        destroyed() {
          this.$el.parentNode.removeChild(this.$el);
          instance = null;
          window.removeEventListener("popstate", function() {
            console.log("移除监听器");
          });
        }
      });
      document.body.appendChild(instance.$el);
    });
  };
}

export const Book1 = promptboxConstructor(book1);
export const Book2 = promptboxConstructor(book2);
export const Book3 = promptboxConstructor(book3);
export const Book4 = promptboxConstructor(book4);
export const Chapter1 = promptboxConstructor(chapter1);
export const Chapter2 = promptboxConstructor(chapter2);
export const Server = promptboxConstructor(server);
export const SingActive = promptboxConstructor(singActive);
export const WeakSubscribe = promptboxConstructor(weakSubscribe);
export const Sign = promptboxConstructor(sign);

export const PayForGift = promptboxConstructor(payForGift);

export const PhoneBinding = promptboxConstructor(phoneBinding);
