"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

require("./thirdpart-auth-dialog.css");

var _base = require("../../utils/base");

var _vueNormalizeComponent = _interopRequireDefault(require("../../utils/vue-normalize-component.js"));

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _base.createComponentName)('thirdpart-auth-dialog'),
  mixins: [_base.componentMixin],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    authUrl: {
      type: Object,
      default: null
    },
    siteUrl: {
      type: String,
      default: ''
    },
    authProvider: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      defaultAuthUrl: {
        qq: '/sso/qq'
      }
    };
  },

  watch: {
    visible(newVal) {
      if (newVal) {
        this.createIframe();
      } else {
        this.hiddenIframe();
      }
    }

  },

  mounted() {
    window.addEventListener('message', this.messageHandler, false);
  },

  beforeDestroy() {
    window.removeEventListener('message', this.messageHandler, false);
    this.$emit('destroy');
    if (!this.iframeWrapEl) return;

    if (this.iframeWrapEl.parentNode) {
      document.body.removeChild(this.iframeWrapEl);
    }

    this.iframeEl = null;
    this.iframeWrapEl = null;
  },

  methods: {
    messageHandler(e) {
      var type = e.data.type;

      if (type === 'auth') {
        window.frames['gd-account-frame'].window.postMessage(e.data, '*');
        this.closeDialog();
      }
    },

    closeDialog() {
      this.$emit('update:visible', false);
    },

    createIframe() {
      var _this = this;

      if (!this.iframeWrapEl) {
        var iframeWrap = document.createElement('div');
        var iframeContainer = document.createElement('div');
        var close = document.createElement('div');
        var iframe = document.createElement('iframe');
        iframeWrap.className = 'gd-thirdpart-auth-iframe-wrap';
        close.className = 'gd-thirdpart-auth-close';
        iframeContainer.className = 'gd-thirdpart-auth-iframe-container';
        iframe.className = 'gd-thirdpart-auth-iframe';
        iframe.frameBorder = 0;
        iframeContainer.appendChild(iframe);
        iframeWrap.appendChild(close);
        iframeWrap.appendChild(iframeContainer);
        close.addEventListener('click', this.closeDialog, false);
        this.$on('destroy', function () {
          close.removeEventListener('click', _this.close, false);
        });
        this.iframeEl = iframe;
        this.iframeWrapEl = iframeWrap;
      }

      var authUrl = this.authUrl || this.defaultAuthUrl;
      document.body.appendChild(this.iframeWrapEl);
      this.iframeEl.src = this.siteUrl + authUrl[this.authProvider];
    },

    hiddenIframe() {
      if (!this.iframeWrapEl) return;
      document.body.removeChild(this.iframeWrapEl);
    }

  }
};
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("div");
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* component normalizer */

/* style inject */

/* style inject SSR */
var _default = (0, _vueNormalizeComponent.default)({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = _default;