const RELEVANT_HEADERS = ['authorization', 'cache-control'] as const

export function getRelevantHeaders(
  headers: Record<string, string> | undefined,
): string {
  if (!headers || typeof headers !== 'object') {
    return ''
  }

  const relevantEntries: Record<string, string> = {}

  for (const [key, value] of Object.entries(headers)) {
    const lowerKey = key.toLowerCase()
    if (
      RELEVANT_HEADERS.includes(lowerKey as (typeof RELEVANT_HEADERS)[number])
    ) {
      relevantEntries[lowerKey] = value
    }
  }

  return Object.keys(relevantEntries).length > 0
    ? JSON.stringify(relevantEntries)
    : ''
}
