import { fetchCms } from "./cmsApi"; export function slugify(text: string): string { return text .toLowerCase() .replace(/\s+/g, "-") .replace(/ü/g, "ue") .replace(/ä/g, "ae") .replace(/ö/g, "oe") .replace(/ß/g, "ss"); } /** * Resolves a cover slug or ID to the actual cover ID. * If the input is already a numeric ID, returns it as-is. * If it's a slug, looks up the cover by name and returns the ID. */ export async function resolveCoverId(idOrSlug: string): Promise { // If it's already a numeric ID, return it if (/^\d+$/.test(idOrSlug)) { return idOrSlug; } // Otherwise, fetch all covers and find by slug try { const allCovers = await fetchCms<{ data: any[] }>(`/product-covers`); const cover = allCovers.data?.find((c: any) => { const name = c.attributes?.name || c.name; const slug = slugify(name || ""); return slug === idOrSlug; }); return cover?.id?.toString() || null; } catch { return null; } }