/**
 * displayName.ts
 * Bangla-first product name display helper
 * 
 * RULE:
 * - For DISPLAY (UI): always use getDisplayName(product.nameBangla, product.name)
 *   → returns BN name if valid Bangla, otherwise EN name as fallback
 * - For URL slugs/routing: always use product.name (EN) directly
 */

// Bengali Unicode range: U+0980–U+09FF
const BANGLA_REGEX = /[\u0980-\u09FF]/;

/**
 * Returns the best display name for a product.
 * Prefers BN (Bangla) name, falls back to EN name if BN is missing or corrupted.
 */
export const getDisplayName = (nameBangla?: string, nameEn?: string): string => {
  const bn = (nameBangla || '').trim();
  if (bn && BANGLA_REGEX.test(bn)) return bn;      // Valid Bangla → show BN
  return (nameEn || '').trim() || bn || '';         // Fallback → EN name
};

/**
 * Returns true if the string contains valid Bangla characters.
 */
export const isValidBangla = (text?: string): boolean => {
  return BANGLA_REGEX.test((text || '').trim());
};
