import { STORAGE_KEY_KIMLIK_TOKEN, STORAGE_KEY_KIMLIK_USER_ID } from '@dvn/kimlik/api/kimlikApi';
import { getCookieStorage } from './storage';
/**
 * Returns the session from the storage.
 */
export const getSessionData = (storage = getCookieStorage()) => ({
  token: storage.get(STORAGE_KEY_KIMLIK_TOKEN),
  uid: storage.get(STORAGE_KEY_KIMLIK_USER_ID),
});
/**
 * Returns true if there is user data saved in the browser storage.
 */
export function isUserLoggedIn(storage, logger) {
  const hasToken = !!getSessionData(storage).token;
  logger === null || logger === void 0 ? void 0 : logger.new('KimlikUtilFunctions').debugGroup('isUserLoggedIn', {
    result: hasToken,
    storage: getSessionData(storage),
  });
  return hasToken;
}
/**
 * Sends the logout request and clears the storage.
 */
export function logout(redirectTo = '/', kimlikApi, storage = getCookieStorage(), navigate = (config) => (window.location = config.path)) {
  if (kimlikApi) {
    kimlikApi === null || kimlikApi === void 0 ? void 0 : kimlikApi.logout().then(() => {
      storage.clear();
      navigate({ path: redirectTo });
    });
  }
  else {
    storage.clear();
    navigate({ path: redirectTo });
  }
}
