/**
 * BoostMedia AI Content Generator Admin - Expandable Summary Component
 *
 * Truncates text to N words with expand/collapse on click.
 *
 * @package BoostMedia_AI
 * @license GPL-2.0-or-later
 */

import { useState } from 'react'
import { t } from '../../lib/i18n'

interface ExpandableSummaryProps {
  text: string | null
  wordLimit?: number
  className?: string
}

export function ExpandableSummary({ text, wordLimit = 10, className = '' }: ExpandableSummaryProps) {
  const [expanded, setExpanded] = useState(false)

  if (!text || text.trim() === '' || text === '—') {
    return <span className="text-bc-gray-400">—</span>
  }

  const words = text.split(/\s+/)
  const isLong = words.length > wordLimit
  const displayText = expanded || !isLong
    ? text
    : words.slice(0, wordLimit).join(' ') + '…'

  return (
    <span
      className={`${className} ${isLong ? 'cursor-pointer hover:text-bc-primary transition-colors' : ''}`}
      onClick={() => isLong && setExpanded(!expanded)}
      title={isLong && !expanded ? t('Click to expand') : undefined}
    >
      {displayText}
    </span>
  )
}
