import { ROUTES } from "@/router/routes";

export const PRIVATE_ALBUM_QUERY_PARAM = "album";
export const PRIVATE_ALBUM_IMAGE_QUERY_PARAM = "image";

export function getPrivateAlbumGalleryState(input: {
  get: (key: string) => string | null;
}): { albumId: string; imageIndex: number } | null {
  const albumId = input.get(PRIVATE_ALBUM_QUERY_PARAM)?.trim();
  const imageIndex = Number(input.get(PRIVATE_ALBUM_IMAGE_QUERY_PARAM) ?? "0");
  if (!albumId || !Number.isInteger(imageIndex) || imageIndex < 0) return null;
  return { albumId, imageIndex };
}

export function buildPrivateAlbumGalleryUrl(
  albumId: string,
  imageIndex: number,
  basePath: string = ROUTES.privateZone,
): string {
  const params = new URLSearchParams({
    [PRIVATE_ALBUM_QUERY_PARAM]: albumId,
    [PRIVATE_ALBUM_IMAGE_QUERY_PARAM]: String(imageIndex),
  });
  return `${basePath}?${params.toString()}`;
}

export function buildPrivateZoneWithoutGalleryUrl(
  input: { toString: () => string },
  basePath: string = ROUTES.privateZone,
): string {
  const params = new URLSearchParams(input.toString());
  params.delete(PRIVATE_ALBUM_QUERY_PARAM);
  params.delete(PRIVATE_ALBUM_IMAGE_QUERY_PARAM);
  const query = params.toString();
  return query ? `${basePath}?${query}` : basePath;
}
