import React from "react"

function hexToRgb(hex?: string | null): [number, number, number] {
  if (!hex) return [37, 99, 235]
  const h = hex.replace('#', '')
  const v = h.length === 3 ? h.split('').map(c => c + c).join('') : h
  const n = parseInt(v, 16)
  return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
}
function chipTint(color?: string | null) {
  const [r, g, b] = hexToRgb(color)
  return {
    borderColor: `rgba(${r},${g},${b},.30)`,
    background: `rgba(${r},${g},${b},.10)`,
  } as React.CSSProperties
}

export function InfoChip({ icon, label, tint }: { icon: React.ReactNode; label: string; tint?: string | null }) {
  const style = tint ? chipTint(tint) : undefined
  return (
    <span
      title={label}
      className="inline-flex max-w-full items-center gap-1 rounded-lg border px-2.5 py-1 text-xs leading-none"
      style={style}
    >
      {icon}
      <span className="truncate">{label}</span>
    </span>
  )
}
