const ALL_LIBRARIES = "__all__";

function getToken(): string | null {
  return localStorage.getItem("token");
}

function setToken(token: string): void {
  localStorage.setItem("token", token);
}

function removeToken(): void {
  localStorage.removeItem("token");
}

function getRefreshToken(): string | null {
  return localStorage.getItem("refresh_token");
}

function setRefreshToken(token: string): void {
  localStorage.setItem("refresh_token", token);
}

function removeRefreshToken(): void {
  localStorage.removeItem("refresh_token");
}

function getTheme(): string {
  return localStorage.getItem("theme") || "tokyo-night";
}

function setTheme(theme: string): void {
  localStorage.setItem("theme", theme);
}

function getSelectedLibrary(): string {
  const stored = localStorage.getItem("selectedLibrary");
  if (stored === ALL_LIBRARIES) return "";
  if (stored) return stored;
  return "";
}

function setSelectedLibrary(libraryId: string): void {
  const value = libraryId === "" ? ALL_LIBRARIES : libraryId;
  localStorage.setItem("selectedLibrary", value);
  document.cookie = `selectedLibrary=${encodeURIComponent(value)};path=/;max-age=${365 * 24 * 60 * 60};samesite=lax`;
}

function clearAll(): void {
  localStorage.clear();
}

export {
  ALL_LIBRARIES,
  clearAll,
  getRefreshToken,
  getSelectedLibrary,
  getTheme,
  getToken,
  removeRefreshToken,
  removeToken,
  setRefreshToken,
  setSelectedLibrary,
  setTheme,
  setToken
};
