import type { CatalogPlaceholderGroupView } from "@batch-cooking/shared";

/**
 * Pure helpers for `CatalogPage` — kept out of the `.tsx` per repo
 * convention, unit-tested on their own.
 */

/** Which server-side list a UI tab maps to — `pending` sends no `reviewed` param (the working list), `reviewed` sends `reviewed=true` (the archive). */
export type CatalogTab = "pending" | "reviewed";

/** `CatalogTab` → the `reviewed` query value `adminApiClient.getPlaceholders` expects. */
export function reviewedParam(tab: CatalogTab): "true" | undefined {
  return tab === "reviewed" ? "true" : undefined;
}

/**
 * Splits a group's spellings into the one to show as the card title and the
 * rest to list as "aussi écrit : …". The API already sorts `displayNames`
 * alphabetically and none is more canonical than another, so the first is
 * as good a headline as any — the point of the group is that they're the
 * same missing ingredient.
 */
export function splitSpellings(group: CatalogPlaceholderGroupView): {
  headline: string;
  variants: string[];
} {
  const [headline = group.normalizedName, ...variants] = group.displayNames;
  return { headline, variants };
}

/** `"2026-08-28T09:00:00.000Z"` → `"28/08/2026"` for the "première fois le …" line. `null` → `null`. */
export function formatDate(iso: string | null): string | null {
  return iso === null ? null : new Date(iso).toLocaleDateString("fr-FR");
}
