import { IMPERSONATED, TOKEN, TOKEN_COOKIE_NAME, TOKEN_COOKIE_VALUE, URL } from '../constants';
import {
  ACTIVATION_GUEST_PAGE,
  CREATE_PASSWORD_PAGE,
  FORGOT_PASSWORD_PAGE,
  LOGIN_PAGE,
  PASSWORD_LESS_LOGIN_CODE_PAGE,
  PASSWORD_LESS_LOGIN_PAGE,
  RESET_PASSWORD_PAGE,
  SSO_LOGIN_PAGE,
  USER_AUTH_ERROR_PAGE,
} from '@/modules/@common/router/constants';

import {
  getBrowserStorageItem,
  getStorageItem,
  removeStorageItem,
  setBrowserStorageItem,
  setStorageItem,
} from '@/modules/@common/storage/helpers';

export const setToken = (data: string) => setStorageItem(TOKEN, data);
export const getToken = () => getStorageItem(TOKEN);

export const setImpersonated = (impersonated: string) => setStorageItem(IMPERSONATED, impersonated);
// todo: remove getBrowserStorage & replace input types
export const getImpersonated = () => getStorageItem(IMPERSONATED) === '1';

export const setTokenCookieName = (data: string) => setBrowserStorageItem(TOKEN_COOKIE_NAME, data);
export const getTokenCookieName = () => getBrowserStorageItem(TOKEN_COOKIE_NAME);
export const setTokenCookieValue = (data: string) => setBrowserStorageItem(TOKEN_COOKIE_VALUE, data);
export const getTokenCookieValue = () => getBrowserStorageItem(TOKEN_COOKIE_VALUE);

export const removeToken = () => removeStorageItem(TOKEN);

export const getUrl = () => getStorageItem(URL);

export const removeUrl = () => removeStorageItem(URL);

export const isLoginPage = (pathname: string) =>
  [
    LOGIN_PAGE.path,
    SSO_LOGIN_PAGE.path,
    RESET_PASSWORD_PAGE.path,
    CREATE_PASSWORD_PAGE.path,
    FORGOT_PASSWORD_PAGE.path,
    ACTIVATION_GUEST_PAGE.path,
    PASSWORD_LESS_LOGIN_PAGE.path,
    PASSWORD_LESS_LOGIN_CODE_PAGE.path,
    USER_AUTH_ERROR_PAGE.path,
  ].some((path) => pathname.includes(path));
