import type { AllPerpMetasResponse } from '@nktkas/hyperliquid'

/**
 * Detect the DEX name from a meta object by inspecting universe item names.
 * HIP-3 DEXes prefix their asset names (e.g., "xyz:NVDA", "flx:AAPL").
 * Main HL uses plain names (e.g., "BTC", "ETH").
 *
 * @param meta - A single DEX's meta from allPerpMetas
 * @returns The DEX name (empty string for main HL)
 */
export const getDexNameFromMeta = ({
  meta,
}: {
  meta: AllPerpMetasResponse[number]
}): string => {
  if (!meta?.universe?.length) {
    return ''
  }

  // Check the first universe item for a prefix
  for (const item of meta.universe) {
    if (item.name?.includes(':')) {
      const prefix = item.name.split(':')[0].toLowerCase()
      return prefix
    }
  }

  // No prefix found - this is main HL
  return ''
}
