/**
 * Encode raw image bytes as a self-contained `data:` URL.
 *
 * Instagram's `profile_pic_url` is a *signed* CDN URL whose token expires within
 * hours. We persist the logged-in account, so hotlinking that URL from the
 * renderer shows a broken image once the token lapses. Embedding the bytes at
 * capture time (while the URL is still live) sidesteps expiry, CORS and referrer
 * policy entirely — the renderer gets a stable, offline image.
 */
export function toAvatarDataUrl(bytes: Uint8Array, contentType: string | null): string | null {
  if (bytes.length === 0) return null;
  const base = (contentType ?? '').split(';')[0]!.trim().toLowerCase();
  const mime = base.startsWith('image/') ? base : 'image/jpeg';
  const b64 = Buffer.from(bytes).toString('base64');
  return `data:${mime};base64,${b64}`;
}
