/**
 * Broker intake slug generation.
 * Rules from the intake-page requirements doc: lowercase, hyphens for spaces,
 * strip special characters and business suffixes, collapse/trim hyphens, cap length.
 */

const BUSINESS_SUFFIXES = new Set(['llc', 'inc', 'corp', 'corporation', 'co', 'ltd'])
const MAX_SLUG_LENGTH = 48

export function slugify(companyName: string): string {
  const words = companyName
    .toLowerCase()
    .replace(/&/g, ' ')
    .replace(/[^a-z0-9\s-]/g, '')
    .split(/[\s-]+/)
    .filter(Boolean)
    .filter((w) => !BUSINESS_SUFFIXES.has(w))

  const slug = words.join('-').replace(/-+/g, '-').replace(/^-|-$/g, '')
  return slug.slice(0, MAX_SLUG_LENGTH).replace(/-$/, '')
}

/** Candidate alternatives when the base slug is taken. */
export function slugAlternatives(base: string): string[] {
  return [1, 2, 3, 4, 5].map((n) => `${base}-${n}`)
}
