import { getMediaUrl, getTmdbImageUrl } from "@/lib/api"; import type { Entry, Collection, Media } from "@/types"; const DEFAULT_POSTER = "/images/default-poster.png"; export function getPosterUrl(entry: Entry): string { // Not in library, use TMDB poster if (!entry.isInLibrary) { return entry.poster_path ? getTmdbImageUrl(entry.poster_path, "w342") : DEFAULT_POSTER; } // In library: try local selected poster if (entry.medias) { const selectedPoster = entry.medias.find( (m: Media) => m.type === "poster" && m.is_selected, ); if (selectedPoster?.path) { return getMediaUrl("entries", "posters", "preview", selectedPoster.path); } } // Fallback to TMDB poster if (entry.poster_path) { return getTmdbImageUrl(entry.poster_path, "w300"); } return DEFAULT_POSTER; } /** Same source as getPosterUrl, but at original resolution. */ export function getFullPosterUrl(entry: Entry): string { const selectedPoster = entry.isInLibrary ? entry.medias?.find((m: Media) => m.type === "poster" && m.is_selected) : undefined; if (selectedPoster?.path) { return getMediaUrl("entries", "posters", "full", selectedPoster.path); } if (entry.poster_path) { return getTmdbImageUrl(entry.poster_path, "original"); } return DEFAULT_POSTER; } export function getBackdropUrl(entry: Entry): string { if (entry.medias) { const selectedBackdrop = entry.medias.find( (m: Media) => m.type === "backdrop" && m.is_selected, ); if (selectedBackdrop?.path) { return getMediaUrl("entries", "backdrops", "full", selectedBackdrop.path); } } if (entry.banner_path) { return getTmdbImageUrl(entry.banner_path, "w1280"); } return ""; } export function getTmdbPosterUrl( posterPath: string | null, size = "w342", ): string { if (!posterPath) return DEFAULT_POSTER; return getTmdbImageUrl(posterPath, size); } export function getCollectionBackdropUrl(collection: Collection): string { if (collection.medias) { const selectedBackdrop = collection.medias.find( (m: Media) => m.type === "backdrop" && m.is_selected, ); if (selectedBackdrop?.path) { return getMediaUrl( "collections", "backdrops", "full", selectedBackdrop.path, ); } } if (collection.backdrop_path) { return getTmdbImageUrl(collection.backdrop_path, "w780"); } return getCollectionPosterUrl(collection); } export function getCollectionPosterUrl(collection: Collection): string { if (collection.medias) { const selectedPoster = collection.medias.find( (m: Media) => m.type === "poster" && m.is_selected, ); if (selectedPoster?.path) { return getMediaUrl( "collections", "posters", "preview", selectedPoster.path, ); } } if (collection.poster_path) { return getTmdbImageUrl(collection.poster_path, "w342"); } return DEFAULT_POSTER; } export function getProfileUrl(profilePath: string | null): string { if (!profilePath) return "/images/default-profile.png"; return getTmdbImageUrl(profilePath, "w185"); } export function formatRuntime(minutes: number | null): string { if (!minutes) return ""; const h = Math.floor(minutes / 60); const m = minutes % 60; return h > 0 ? `${h}h ${m}m` : `${m}m`; } export function formatDate(dateStr: string | null): string { if (!dateStr) return ""; return new Date(dateStr).toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric", }); } export function getYear(dateStr: string | null): string { if (!dateStr) return ""; return new Date(dateStr).getFullYear().toString(); }