function isClipboardImageFile(file: File) {
  const normalizedType = String(file.type ?? '').trim().toLowerCase();

  if (normalizedType.startsWith('image/')) {
    return true;
  }

  const normalizedName = String(file.name ?? '').trim().toLowerCase();
  return /\.(png|jpe?g|gif|webp|bmp|heic|heif)$/i.test(normalizedName);
}

function isGeneratedClipboardImageName(file: File) {
  const normalizedName = String(file.name ?? '').trim().toLowerCase();

  if (!normalizedName) {
    return true;
  }

  return /^(image|clipboard|pasted image)(?:[-\s]?\d+)?(?: \(\d+\))?\.(png|jpe?g|gif|webp|bmp|tiff?|heic|heif)$/i.test(
    normalizedName,
  );
}

export function buildComposerFilePickKey(file: File) {
  const normalizedType = String(file.type ?? '').trim().toLowerCase();
  const normalizedName = String(file.name ?? '').trim().toLowerCase();
  const shouldIgnoreName = isClipboardImageFile(file) && isGeneratedClipboardImageName(file);

  if (shouldIgnoreName) {
    return `clipboard-image:${file.size}:${normalizedType}`;
  }

  return `${normalizedName}:${file.size}:${normalizedType}:${file.lastModified}`;
}
