import Vue from "vue";

import giftPopup from "./children/gift.vue";

let instance = null;
function popupBoxConstructor(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,
          close() {
            this.visible = false;
            resolve(this.$data);
          }
        },
        destroyed() {
          this.$el.parentNode.removeChild(this.$el);
          instance = null;
        }
      });
      document.body.appendChild(instance.$el);
    });
  };
}

export const popupGift = popupBoxConstructor(giftPopup);
