'use client';

import { Plus, Wallet } from 'lucide-react';
import Link from 'next/link';

import { formatMoney } from '@/lib/money';
import { cn } from '@/lib/utils';

export function WalletPill({ balanceMinor, currency, lowBalance }: { balanceMinor: number; currency: string; lowBalance: boolean }) {
  const state = balanceMinor <= 0 ? 'zero' : lowBalance ? 'low' : 'normal';
  const label = `Wallet balance ${formatMoney(balanceMinor, currency)}${state === 'zero' ? ', empty' : state === 'low' ? ', low' : ''}. Open wallet`;
  return (
    <Link
      href="/wallet"
      aria-label={label}
      className={cn(
        'group inline-flex h-10 items-center gap-2 rounded-pill border pl-1 pr-3 text-small font-semibold transition-[background-color,border-color,padding] duration-180 ease-brand hover:pr-1.5',
        state === 'normal' && 'border-border bg-surface text-foreground hover:border-border-strong',
        state === 'low' && 'border-warning/30 bg-warning-soft text-warning',
        state === 'zero' && 'border-danger/30 bg-danger-soft text-danger',
      )}
    >
      <span
        className={cn(
          'relative grid h-8 w-8 place-items-center rounded-full',
          state === 'normal' && 'bg-primary-soft text-primary',
          state === 'low' && 'bg-surface/70 text-warning',
          state === 'zero' && 'bg-surface/70 text-danger',
        )}
      >
        <Wallet className="h-4 w-4" aria-hidden="true" />
        {state === 'low' ? (
          <span className="absolute -right-0.5 -top-0.5 flex h-2.5 w-2.5" aria-hidden="true">
            <span className="absolute inset-0 animate-pulse-dot rounded-full bg-warning" />
            <span className="relative h-2.5 w-2.5 rounded-full bg-warning ring-2 ring-surface" />
          </span>
        ) : null}
      </span>
      <span className="tabular-nums" aria-hidden="true">
        {formatMoney(balanceMinor, currency)}
      </span>
      <span
        className="grid h-0 w-0 place-items-center overflow-hidden rounded-full bg-primary text-primary-foreground opacity-0 transition-all duration-180 ease-brand group-hover:h-7 group-hover:w-7 group-hover:opacity-100 group-focus-visible:h-7 group-focus-visible:w-7 group-focus-visible:opacity-100"
        aria-hidden="true"
      >
        <Plus className="h-3.5 w-3.5" />
      </span>
    </Link>
  );
}
