import { useTranslations } from "next-intl";
import { site } from "@/config/site";

/**
 * Every string rendered by `NewsletterForm`. The form is a Client Component
 * (`useActionState` has to sit at the root of the `<form>`), so reading them
 * with `useTranslations` *inside* it would drag the whole `Footer.newsletter`
 * namespace — and the un-scoped `NextIntlClientProvider` payload behind it —
 * into the client bundle. They are resolved on the server instead and passed
 * down as plain strings.
 */
export type NewsletterLabels = {
  emailLabel: string;
  submit: string;
  sending: string;
  consent: string;
  unavailable: string;
  /** Keyed by the `NewsletterError` codes the Server Action returns. */
  errors: {
    invalid: string;
    consent: string;
    rateLimited: string;
  };
};

/**
 * Server-side hook: both mounts of the form (`Footer`, `JournalNewsletterBand`)
 * resolve the same labels here rather than duplicating the `t()` calls.
 */
export function useNewsletterLabels(): NewsletterLabels {
  const t = useTranslations("Footer.newsletter");

  return {
    emailLabel: t("emailLabel"),
    submit: t("submit"),
    sending: t("sending"),
    consent: t("consent"),
    // The address lives in `site.ts` — never inline it in a message.
    unavailable: t("unavailable", { email: site.contact.email }),
    errors: {
      invalid: t("errors.invalid"),
      consent: t("errors.consent"),
      rateLimited: t("errors.rateLimited"),
    },
  };
}
