import { TableActionTooltip } from '@admin/components/ui/table-action-tooltip';
import { buildQrUrl, type QrStyleOptions } from '@admin/lib/qr-code';

type QrFormatDownloadsProps = QrStyleOptions & {
    baseUrl: string;
    formats?: string[];
    extra?: Record<string, string>;
    pdfHref?: string;
};

function downloadTooltip(format: string, pdfHref?: string): string {
    if (format === 'pdf' && pdfHref) {
        return 'Download document PDF';
    }

    return `Download QR code ${format.toUpperCase()}`;
}

export function QrFormatDownloads({
    baseUrl,
    size,
    template,
    color,
    background,
    logo,
    logoWidth,
    punchout,
    formats = ['png', 'svg', 'webp'],
    extra,
    pdfHref,
}: QrFormatDownloadsProps) {
    return (
        <div className="inline-flex flex-wrap items-center gap-1" role="group" aria-label="Download formats">
            {formats.map((format) => (
                <TableActionTooltip
                    key={format}
                    href={
                        format === 'pdf' && pdfHref
                            ? pdfHref
                            : buildQrUrl(baseUrl, {
                                  size,
                                  template,
                                  color,
                                  background,
                                  logo,
                                  logoWidth,
                                  punchout,
                                  format,
                                  download: true,
                                  extra,
                              })
                    }
                    label={format}
                    tooltip={downloadTooltip(format, pdfHref)}
                    download
                />
            ))}
        </div>
    );
}
