import { apiUrl } from "./api-base";

/**
 * Server-side fetch of the public site settings (social links, titles, …).
 * Cached/revalidated so it doesn't hit the backend on every render.
 */
export async function fetchPublicSettings(): Promise<Record<string, string>> {
  try {
    const res = await fetch(apiUrl("/settings/public"), {
      next: { revalidate: 3600 },
    });
    if (!res.ok) return {};
    return (await res.json()) as Record<string, string>;
  } catch {
    return {};
  }
}

/** Setting keys that hold a public social/profile URL, in display order. */
const SOCIAL_SETTING_KEYS = [
  "telegram_link",
  "nostr_link",
  "x_link",
  "youtube_link",
  "discord_link",
  "linkedin_link",
] as const;

/**
 * Build the list of real social URLs for schema.org `sameAs` from settings.
 * Only includes entries that are actually configured (non-empty, http(s)).
 */
export function socialUrlsFromSettings(
  settings: Record<string, string>,
): string[] {
  return SOCIAL_SETTING_KEYS.map((key) => settings[key]?.trim())
    .filter((url): url is string => !!url && /^https?:\/\//i.test(url));
}
