export function emitUploadEvent(eventName, payload = {}) {
  try {
    if (typeof window !== 'undefined') {
      window.dispatchEvent(
        new CustomEvent('skinbase:upload-analytics', {
          detail: {
            event: eventName,
            payload,
            timestamp: Date.now(),
          },
        })
      )
    }

    const endpoint = typeof window !== 'undefined' ? window?.SKINBASE_UPLOAD_ANALYTICS_URL : null
    if (endpoint && typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
      const body = JSON.stringify({ event: eventName, payload, ts: Date.now() })
      const blob = new Blob([body], { type: 'application/json' })
      navigator.sendBeacon(endpoint, blob)
    }
  } catch {
    // analytics must remain non-blocking
  }
}
