import { localizationKey } from "../../../i18nLanguages";
import type { Post } from "../../../types/post";

export function postDisplayText(post: Post, lang: string): string {
  const key = localizationKey(lang);
  return (
    post.localizations?.[
      key as keyof typeof post.localizations
    ]?.text?.trim() ||
    post.text?.trim() ||
    ""
  );
}

export function postTitleText(post: Post, lang: string): string {
  const key = localizationKey(lang);
  const localized =
    post.localizations?.[key as keyof typeof post.localizations];
  const explicitTitle = localized?.title?.trim() || post.title?.trim();
  if (explicitTitle) return explicitTitle;

  const text = postDisplayText(post, lang);
  return (
    text
      .split(/\n{1,}/)
      .map((line) => line.trim())
      .find(Boolean) || ""
  );
}
