import type { LocalChatMediaRow } from "@/data/storage/chat";

export function localChatMediaRowToBlob(
  row: LocalChatMediaRow,
): Blob | null {
  if (row.bytes && row.bytes.byteLength > 0) {
    const mimeType = row.mimeType || fallbackMediaMimeType(row.kind);
    return new Blob([row.bytes], { type: mimeType });
  }

  if (row.blob && row.blob.size > 0) {
    const mimeType =
      row.mimeType || row.blob.type || fallbackMediaMimeType(row.kind);
    return new Blob([row.blob], { type: mimeType });
  }

  return null;
}

function fallbackMediaMimeType(kind: LocalChatMediaRow["kind"]): string {
  return kind === "image" ? "image/*" : "audio/mpeg";
}
