import type { TicketAttachment } from '@/features/tickets/types/ticket.types';

const IMAGE_EXT = /\.(jpe?g|png|gif|webp|bmp)$/i;

export function isImageMime(mimeType?: string | null): boolean {
  return Boolean(mimeType?.startsWith('image/'));
}

export function isImageFileName(fileName?: string | null): boolean {
  return Boolean(fileName && IMAGE_EXT.test(fileName));
}

export function isImageAttachment(attachment: Pick<TicketAttachment, 'mime_type' | 'file_name'>): boolean {
  return isImageMime(attachment.mime_type) || isImageFileName(attachment.file_name);
}

export function isPreviewableFile(file: File): boolean {
  if (file.type.startsWith('image/')) {
    return true;
  }
  return isImageFileName(file.name);
}

export function isPdfAttachment(attachment: Pick<TicketAttachment, 'mime_type' | 'file_name'>): boolean {
  return attachment.mime_type === 'application/pdf' || /\.pdf$/i.test(attachment.file_name ?? '');
}

export function attachmentKindLabel(
  attachment: Pick<TicketAttachment, 'mime_type' | 'file_name'>,
): string {
  if (isImageAttachment(attachment)) {
    return 'image';
  }
  if (isPdfAttachment(attachment)) {
    return 'pdf';
  }
  return 'file';
}
