"use strict";

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

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

var _getIterator2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/get-iterator"));

var _isArray2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/array/is-array"));

require("./main.css");

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

var _thirdpartAuthDialog = _interopRequireDefault(require("./thirdpart-auth-dialog"));

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

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _base.createComponentName)('account-main'),
  components: {
    ThirdpartAuthDialog: _thirdpartAuthDialog.default
  },
  mixins: [_base.componentMixin],
  props: {
    thirdpartLogin: {
      type: Array,

      default() {
        return [];
      }

    },
    authUrl: {
      type: Object,
      default: null
    },
    tab: {
      type: String,

      default() {
        return 'login';
      }

    },
    appStage: {
      type: String,

      default() {
        return 'production';
      }

    },
    styleHref: {
      type: String,

      default() {
        return '';
      }

    },
    withoutTips: {
      type: Boolean,

      default() {
        return false;
      }

    }
  },

  data() {
    return {
      accountPageUrl: '',
      showThirdpartAuth: false,
      authProvider: ''
    };
  },

  computed: {
    accountTab: {
      get() {
        return this.tab;
      },

      set(tab) {
        this.$emit('update:tab', tab);
      }

    },

    accountPageRoute() {
      return this.siteUrl + '/account';
    },

    siteUrl() {
      var appStage = this.appStage;

      if (appStage === 'development') {
        return 'http://www.gaoding.localhost';
      }

      if (appStage === 'test') {
        return 'http://www.dev.gaoding.com';
      }

      if (appStage === 'stage') {
        return 'https://stage.gaoding.com';
      }

      return 'https://www.gaoding.com';
    }

  },

  mounted() {
    this.init();
  },

  methods: {
    initExtImporter() {
      var _this = this;

      var handlers = {
        'login.success': function loginSuccess(data) {
          _this.$emit('login-success', data);
        },
        'login.error': function loginError(error) {
          _this.$emit('login-error', error);
        },
        'update:tab': function updateTab(data) {
          _this.accountTab = data.tab;
        },
        'ui.error': function uiError(data) {
          _this.$emit('ui-error', data);
        },
        'ui.tips': function uiTips(data) {
          _this.$emit('ui-tips', data);
        },
        'login.thirdpart': function loginThirdpart(data) {
          _this.authProvider = data;
          _this.showThirdpartAuth = true;

          _this.$emit('login-thirdpart', data);
        },
        'register.success': function registerSuccess(data) {
          _this.$emit('register-success', data);
        }
      };

      var evtHandler = function evtHandler(evt) {
        var data = evt.data || {};
        var action = data.action;

        if (handlers[action]) {
          handlers[action].call(_this, data.data);
          return;
        }

        if (_this.appStage === 'development') {
          console.error('Not Supported Action', action, data);
        }
      };

      window.addEventListener('message', evtHandler, false);
      this.$on('destroy', function () {
        window.removeEventListener('message', evtHandler, false);
      });
    },

    getAccountPageUrl() {
      var url = this.accountPageRoute;
      var thirdpartQuery = '';
      var thirdpartLogin = this.thirdpartLogin;
      var params = [this.tab];
      var queryArray = [];
      var styleHref = this.styleHref;

      if (styleHref) {
        queryArray.push(`style_href=${styleHref}`);
      }

      for (var _iterator = thirdpartLogin, _isArray = (0, _isArray2.default)(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator2.default)(_iterator);;) {
        var _ref;

        if (_isArray) {
          if (_i >= _iterator.length) break;
          _ref = _iterator[_i++];
        } else {
          _i = _iterator.next();
          if (_i.done) break;
          _ref = _i.value;
        }

        var provider = _ref;

        if (provider) {
          thirdpartQuery += provider + ',';
        }
      }

      if (thirdpartQuery.length > 0) {
        thirdpartQuery = 'thirdpart=' + thirdpartQuery.slice(0, -1);
        queryArray.push(thirdpartQuery);
      }

      var withoutTips = this.withoutTips;

      if (withoutTips) {
        queryArray.push('without_tips=1');
      }

      if (params.length) {
        url += `/${params.join('/')}`;
      }

      if (queryArray.length) {
        url += `?${queryArray.join('&')}`;
      }

      this.accountPageUrl = url;
      return url;
    },

    init() {
      this.getAccountPageUrl();
      this.initExtImporter();
    }

  }
};
/* template */

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

  var _h = _vm.$createElement;

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

  return _c("div", {
    staticClass: "gd-account-main"
  }, [_vm._t("header"), _vm._v(" "), _c("iframe", {
    staticClass: "gd-account-main-iframe",
    attrs: {
      name: "gd-account-frame",
      src: _vm.accountPageUrl,
      frameborder: "0"
    }
  }), _vm._v(" "), _c("ThirdpartAuthDialog", {
    attrs: {
      visible: _vm.showThirdpartAuth,
      authUrl: _vm.authUrl,
      authProvider: _vm.authProvider,
      siteUrl: _vm.siteUrl
    },
    on: {
      "update:visible": function updateVisible($event) {
        _vm.showThirdpartAuth = $event;
      }
    }
  }), _vm._v(" "), _vm._t("footer")], 2);
};

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;