import { useState } from 'react';
import { ImagePreviewModal } from '@/components/image-preview-modal';

type ImagePreviewButtonProps = {
    srcs: string[];
    title?: string;
    alt?: string;
    className?: string;
};

export function ImagePreviewButton({
    srcs,
    title,
    alt,
    className = 'h-10 w-10',
}: ImagePreviewButtonProps) {
    const [open, setOpen] = useState(false);

    return (
        <>
            <button
                type="button"
                onClick={() => setOpen(true)}
                className={`relative block overflow-hidden rounded-md border bg-muted transition-opacity hover:opacity-80 ${className}`}
            >
                <img
                    src={srcs[0]}
                    alt={alt ?? title ?? 'Preview'}
                    className="h-full w-full object-cover"
                />
                {srcs.length > 1 && (
                    <span className="absolute -top-1 -right-1 flex h-4 min-w-4 items-center justify-center rounded-full bg-primary px-1 text-[10px] font-medium text-primary-foreground">
                        {srcs.length}
                    </span>
                )}
            </button>
            <ImagePreviewModal
                open={open}
                onOpenChange={setOpen}
                src={srcs[0]}
                sources={srcs}
                title={title}
            />
        </>
    );
}
