export interface RecentRequest { id: number; title: string; year?: number; type?: string; statusLabel?: string; artwork?: { poster_url?: string; backdrop_url?: string }; createdAt?: string | null; } export interface RequestSearchResult { title: string; year?: number; type?: string; requestId?: number; statusLabel?: string; requestedBy?: string | null; accessible?: boolean; } const recordValue = (value: unknown): Record | null => value !== null && typeof value === "object" ? (value as Record) : null; const optionalString = (value: unknown) => (typeof value === "string" ? value : undefined); const optionalNumber = (value: unknown) => (typeof value === "number" && Number.isFinite(value) ? value : undefined); export const normalizeRecentResults = (items: unknown): RecentRequest[] => { if (!Array.isArray(items)) return []; return items.flatMap((value) => { const item = recordValue(value); const id = optionalNumber(item?.id); if (!item || id === undefined) return []; const rawTitle = optionalString(item.title); const placeholder = rawTitle?.trim().toLowerCase() === `request ${id}`; const rawArtwork = recordValue(item.artwork); const artwork = rawArtwork ? { poster_url: optionalString(rawArtwork.poster_url), backdrop_url: optionalString(rawArtwork.backdrop_url), } : undefined; return [ { id, title: !rawTitle || placeholder ? `Request #${id}` : rawTitle, year: optionalNumber(item.year), type: optionalString(item.type), statusLabel: optionalString(item.statusLabel), artwork, createdAt: item.createdAt === null ? null : optionalString(item.createdAt), }, ]; }); }; export const normalizeSearchResults = (items: unknown): RequestSearchResult[] => { if (!Array.isArray(items)) return []; return items.flatMap((value) => { const item = recordValue(value); const title = optionalString(item?.title); if (!item || !title) return []; return [ { title, year: optionalNumber(item.year), type: optionalString(item.type), requestId: optionalNumber(item.requestId), statusLabel: optionalString(item.statusLabel), requestedBy: item.requestedBy === null ? null : optionalString(item.requestedBy), accessible: Boolean(item.accessible), }, ]; }); };