"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fetchExternalSession = void 0;
var _dataSources = require("@mirai/data-sources");
var _services = require("@mirai/services");
var _logout = require("./logout");
var _Core = require("../Core.constants");
const fetchExternalSession = async _ref => {
  let _ref$config = _ref.config,
    config = _ref$config === void 0 ? {} : _ref$config,
    _ref$cookies = _ref.cookies,
    cookies = _ref$cookies === void 0 ? {} : _ref$cookies,
    session = _ref.session;
  const _config$accomodation = config.accomodation,
    _config$accomodation2 = _config$accomodation === void 0 ? {} : _config$accomodation,
    _config$accomodation3 = _config$accomodation2.club,
    _config$accomodation4 = _config$accomodation3 === void 0 ? {} : _config$accomodation3,
    _config$accomodation5 = _config$accomodation4.authentication,
    _config$accomodation6 = _config$accomodation5 === void 0 ? {} : _config$accomodation5,
    type = _config$accomodation6.type;
  if (type === _Core.AUTH_TYPE.MIRAI) return session;
  let token;
  if (type === _Core.AUTH_TYPE.PESTANA) token = cookies.pestana;else if (type === _Core.AUTH_TYPE.SENATOR) {
    const tokenData = new _dataSources.Storage({
      adapter: _dataSources.LocalAdapter
    }).get(_Core.SENATOR_CLUB_TOKEN);
    if (tokenData) token = window.btoa(encodeURIComponent(JSON.stringify(tokenData)));
  }
  let value;
  if (session && !token) (0, _logout.logout)();else if (!session && token) value = await _services.ServiceUser.externalLogin(token, type).catch();else if (session && token) value = session;
  return value;
};
exports.fetchExternalSession = fetchExternalSession;
//# sourceMappingURL=fetchExternalSession.js.map