/**
 * lang.tsx — language-direction helpers (rtl-hebrew-ui spec, wave 4).
 *
 * Provides:
 *   - `isHebrew(str)` — detects whether a string is majority Hebrew by Unicode
 *     range.  Used for mixed-language entity names from the database where the
 *     language is uncertain at render time.
 *   - `<AutoText>` — wraps user-generated strings (customer/vendor/project names)
 *     in a <span dir="auto" lang={…}> so browsers auto-detect direction and
 *     screen readers announce the text in the correct language.
 *
 * WHEN TO USE AutoText:
 *   ✓ Customer names, vendor names, project names, any user-supplied entity name
 *     whose language is not guaranteed.
 *
 * DO NOT use AutoText for:
 *   ✗ Fixed translated strings (status labels, category names, formatted dates)
 *     — these are always translated and carry the document language.
 *   ✗ Structured numeric data (invoice numbers, tax IDs, amounts)
 *     — use dir="ltr" explicitly on those elements instead.
 */
import * as React from 'react'

/**
 * Returns true when a string is predominantly Hebrew.
 *
 * Matches Unicode ranges:
 *   U+0590–U+05FF  Hebrew block (letters, points, punctuation)
 *   U+FB1D–U+FB4F  Hebrew Presentation Forms
 *
 * A string is considered Hebrew when Hebrew characters make up more than 50%
 * of the total character count (ignoring zero-length strings).
 *
 * @example
 * isHebrew('דוד כהן')  // true
 * isHebrew('Acme Ltd') // false
 * isHebrew('')         // false
 */
export function isHebrew(str: string): boolean {
  if (!str || str.length === 0) return false
  // U+0590–U+05FF Hebrew block + U+FB1D–U+FB4F Hebrew Presentation Forms
  const heChars = str.match(/[֐-׿יִ-ﭏ]/g)?.length ?? 0
  return heChars / str.length > 0.5
}

export interface AutoTextProps {
  /** User-generated string whose language is uncertain at render time. */
  children: string
  className?: string
  /**
   * HTML element to render. Defaults to 'span' for inline entity names.
   * Use 'div' for block-level display contexts.
   */
  as?: 'span' | 'div'
}

/**
 * Wraps a user-generated entity name (customer/vendor/project/etc.) in an
 * element with `dir="auto"` and a detected `lang` attribute.
 *
 * - `dir="auto"` lets the browser determine reading direction from the first
 *   strong Unicode directional character — works even when `isHebrew` is
 *   uncertain (e.g. short strings with mixed content).
 * - `lang` enables correct screen-reader pronunciation.
 *
 * **Do NOT use** for:
 * - Fixed translated strings (status labels, category names, formatted dates) —
 *   those carry the document language automatically.
 * - Structured numeric data (invoice numbers, tax IDs, amounts) —
 *   use `dir="ltr"` explicitly on those elements instead.
 *
 * @example
 * <AutoText>דוד כהן</AutoText>
 * // renders: <span dir="auto" lang="he">דוד כהן</span>
 *
 * <AutoText>Acme Ltd</AutoText>
 * // renders: <span dir="auto" lang="en">Acme Ltd</span>
 */
export function AutoText({ children, className, as = 'span' }: AutoTextProps) {
  const Tag = as
  return (
    <Tag dir="auto" lang={isHebrew(children) ? 'he' : 'en'} className={className}>
      {children}
    </Tag>
  )
}
