"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fetchSession = void 0;
var _dataSources = require("@mirai/data-sources");
const CACHE_LIMIT = 1000 * 60 * 60 * 24 * 7;
const SESSION_STORAGE_LEGACY_ID = 'CLUB';
const SESSION_STORAGE_ID = 'MIRAI:SERVICE:USER:session';
const fetchSession = function () {
  var _session;
  let urlParams = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  let storage = new _dataSources.Storage({
    adapter: _dataSources.LocalAdapter,
    cache: CACHE_LIMIT
  });
  let session = storage.get(SESSION_STORAGE_ID);

  //TODO: Remove when don`t exists legacy CLUB
  if (!session) {
    storage = new _dataSources.Storage({
      adapter: _dataSources.SessionAdapter
    });
    const {
      access_token: authorization,
      club_name: name,
      email,
      first_name: firstName,
      id_club: id,
      last_name: lastName,
      level,
      subscribed
    } = storage.get(SESSION_STORAGE_LEGACY_ID) || {};
    session = authorization ? {
      authorization,
      email,
      firstName,
      club: {
        id: id === null || id === void 0 ? void 0 : id.toString(),
        name
      },
      lastName,
      level,
      subscribed
    } : undefined;
  }
  if (!session && urlParams.session) {
    try {
      session = JSON.parse(urlParams.session);
    } catch (error) {
      // eslint-disable-next-line no-console
      console.error('', error);
    }
  }
  return (_session = session) !== null && _session !== void 0 && _session.authorization ? session : undefined;
};
exports.fetchSession = fetchSession;
//# sourceMappingURL=fetchSession.js.map