/** Max tags per article and max length per tag (API + form alignment). */
export const ARTICLE_TAG_MAX_COUNT = 30
export const ARTICLE_TAG_MAX_LENGTH = 48

/**
 * Normalizes tags from JSON body: non-empty trimmed strings, deduped (case-insensitive),
 * length-capped, order preserved by first occurrence.
 */
export function parseArticleTagsFromBody(value: unknown): string[] {
  if (!Array.isArray(value)) return []
  const out: string[] = []
  const seen = new Set<string>()
  for (const item of value) {
    if (typeof item !== "string") continue
    const t = item.trim().replace(/\s+/g, " ")
    if (!t || t.length > ARTICLE_TAG_MAX_LENGTH) continue
    const key = t.toLowerCase()
    if (seen.has(key)) continue
    seen.add(key)
    out.push(t)
    if (out.length >= ARTICLE_TAG_MAX_COUNT) break
  }
  return out
}
