/**
 * Shared "does this ship-address country require a state/region?" helper for the
 * sales-order pages (create / [id]/edit / OrderForm). Currently USA only.
 *
 * Deliberately NOT dependent on the c_country lookup alone: the country record
 * may be unavailable (lazy lookup still pending, or the fetch failed silently),
 * so the country NAME or ISO code on the form must be enough on their own —
 * otherwise the State field can silently never render.
 */

/** Matches "United States", "United States of America", "USA", "U.S.A.", "Vereinigte Staaten" */
export const US_COUNTRY_NAME_RE = /united states|vereinigte staaten|\busa\b|\bu\.s\.a\.?\b/i

/**
 * True when the selected country is the USA.
 * @param countryRecord c_country record from a lookup (may be undefined)
 * @param name          country display name on the form (e.g. "United States")
 * @param code          ISO2 country code on the form (e.g. "US"), when known
 */
export const isUsCountry = (countryRecord: any, name?: any, code?: any): boolean => {
  if (String(code || '').toUpperCase() === 'US') return true
  if (String(countryRecord?.CountryCode || '').toUpperCase() === 'US') return true
  const n = String(name || '').trim()
  return !!n && US_COUNTRY_NAME_RE.test(n)
}
