"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 {
    config = {},
    cookies = {},
    session
  } = _ref;
  const {
    accomodation: {
      club: {
        authentication: {
          type
        } = {}
      } = {}
    } = {}
  } = config;
  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