export type PreviewKind = "image" | "pdf" | "unsupported";

const IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "webp", "gif", "bmp", "avif"];

/**
 * Decide how to render a document URL.
 *
 * Document URLs are stored as plain strings with no MIME type alongside them,
 * and they are typically signed object-storage links — so the extension has to
 * be read from the path with the query string stripped, or every `?X-Amz-...`
 * URL would fall through to "unsupported".
 */
export function getPreviewKind(url: string): PreviewKind {
  const path = url.split("?")[0].split("#")[0].toLowerCase();
  const extension = path.slice(path.lastIndexOf(".") + 1);

  if (IMAGE_EXTENSIONS.includes(extension)) return "image";
  if (extension === "pdf") return "pdf";
  return "unsupported";
}
