/** Cooldown timestamp for rate-limit backoff (ms from now). */
export function rateLimitCooldownUntil(ms) {
  return Date.now() + ms;
}

/** Upload a base64 scan capture to Blob storage; returns public URL or null on 429. */
export async function uploadScanCapture(imageData) {
  const response = await fetch('/api/scan/upload-image', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${localStorage.getItem('auth_token')}`,
    },
    body: JSON.stringify({ imageData }),
  });

  if (response.status === 429) {
    console.warn('Scan image upload rate-limited');
    return null;
  }

  if (!response.ok) {
    const errBody = await response.json().catch(() => ({}));
    throw new Error(errBody.error || 'Failed to upload scan image');
  }

  const result = await response.json();
  return result.url || null;
}
