import { toValidDisplay } from './to-display-symbol'

/**
 * Resolve the name to show for a spot listing — the spot counterpart of
 * `toDisplaySymbol`, with the same precedence and the same sanitization.
 *
 * Precedence: curated override (`TOKEN_SYMBOL_OVERRIDES`, already applied by
 * the caller via `normalizeTokenSymbol`, detected as `displaySymbol !== name`)
 * → the token's live `setTokenAnnotation` display name → the curated/clean
 * symbol itself. The result is always a non-empty string.
 *
 * A live annotation that is not a plausible market name is refused and the
 * symbol is used instead — see `toValidDisplay` in `to-display-symbol.ts`.
 * Curated overrides skip that check because we write them.
 */
export const toSpotDisplaySymbol = ({
  annotationDisplayName,
  displaySymbol,
  name,
}: {
  /** `tokenConciseAnnotations`' `displayName` for this token, if any. */
  readonly annotationDisplayName?: string | undefined
  /** The curated symbol from `normalizeTokenSymbol` (`BTC` for `UBTC`). */
  readonly displaySymbol: string
  /** The raw token name from spotMeta (`UBTC`). */
  readonly name: string
}): string => {
  if (displaySymbol !== name) {
    return displaySymbol
  }
  if (annotationDisplayName === undefined) {
    return displaySymbol
  }
  return toValidDisplay(annotationDisplayName) ?? displaySymbol
}
