import {
  buildPostLinkCaptionHeader,
  SelectedPostLink,
} from "@/lib/postLinkCaption";
import type { AppLanguage } from "@/lib/i18n/registry";

/** هم‌راستا با maxLength مدل پست در بک‌اند */
export const POST_CAPTION_MAX_LENGTH = 2000;

export function buildFullPostCaption(
  description: string,
  taggedUsers: Array<{ user_name: string }>,
  packages: SelectedPostLink[],
  projects: SelectedPostLink[],
  billboards: SelectedPostLink[],
  lang?: AppLanguage,
  shops: SelectedPostLink[] = [],
  shopProducts: SelectedPostLink[] = [],
  bookings: SelectedPostLink[] = []
): string {
  const tagLine = taggedUsers.map((u) => `@${u.user_name}`).join(" ");
  const linkHeader = buildPostLinkCaptionHeader(
    [...packages, ...projects, ...billboards, ...shops, ...shopProducts, ...bookings],
    lang
  );
  return [description.trim(), tagLine, linkHeader].filter(Boolean).join("\n");
}

export function countPostCaptionLength(
  description: string,
  taggedUsers: Array<{ user_name: string }>,
  packages: SelectedPostLink[],
  projects: SelectedPostLink[],
  billboards: SelectedPostLink[],
  lang?: AppLanguage,
  shops: SelectedPostLink[] = [],
  shopProducts: SelectedPostLink[] = [],
  bookings: SelectedPostLink[] = []
): number {
  return buildFullPostCaption(
    description,
    taggedUsers,
    packages,
    projects,
    billboards,
    lang,
    shops,
    shopProducts,
    bookings
  ).length;
}
