import { REFRESH_TOKEN_NAME, TOKEN_NAME } from "@/config/const";

export const hasAuthToken = (): boolean => {
  if (typeof window === "undefined") return false;
  return !!localStorage.getItem(TOKEN_NAME);
};

export const getToken = async (): Promise<string | null> => {
  if (typeof window === "undefined") return null;
  return localStorage.getItem(TOKEN_NAME);
};

export const getRefreshToken = async (): Promise<string | null> => {
  if (typeof window === "undefined") return null;
  return localStorage.getItem(REFRESH_TOKEN_NAME);
};

export const setToken = async (token: string): Promise<void> => {
  if (typeof window === "undefined") return;
  localStorage.setItem(TOKEN_NAME, token);
};

export const setRefreshToken = async (token: string): Promise<void> => {
  if (typeof window === "undefined") return;
  localStorage.setItem(REFRESH_TOKEN_NAME, token);
};

export const removeToken = async (): Promise<void> => {
  if (typeof window === "undefined") return;
  localStorage.removeItem(TOKEN_NAME);
};

export const removeRefreshToken = async (): Promise<void> => {
  if (typeof window === "undefined") return;
  localStorage.removeItem(REFRESH_TOKEN_NAME);
};
