import type { PrivateAlbum } from "@/data/schemas/private-zone";

export const PRIVATE_ALBUM_GRID_LIMIT = 9;

export interface PrivateAlbumDisplayImage {
  readonly sourceIndex: number;
  readonly url: string;
}

export type PrivateAlbumGridLayout =
  | "empty"
  | "single"
  | "double"
  | "triple";

export function getPrivateAlbumDisplayImages(
  album: PrivateAlbum,
): readonly PrivateAlbumDisplayImage[] {
  return album.images.flatMap((image, sourceIndex) => {
    const url = image.url.trim();
    if (image.locked || url.length === 0) return [];
    return [{ sourceIndex, url }];
  });
}

export function findPrivateAlbumDisplayImage(
  album: PrivateAlbum,
  sourceIndex: number,
): PrivateAlbumDisplayImage | null {
  return (
    getPrivateAlbumDisplayImages(album).find(
      (image) => image.sourceIndex === sourceIndex,
    ) ?? null
  );
}

export function getPrivateAlbumGridLayout(
  imageCount: number,
): PrivateAlbumGridLayout {
  if (imageCount <= 0) return "empty";
  if (imageCount === 1) return "single";
  if (imageCount === 2 || imageCount === 4) return "double";
  return "triple";
}
