// Pure helper (no DB/SDK imports) for App Store Server Notification routing.

/**
 * Decode an ASSN signedPayload's JWS body WITHOUT verifying its signature — used
 * ONLY to read bundleId/environment so the webhook can select the matching
 * PaymentMethod before doing the trusted verification with THAT method's client.
 * Never trust these values for anything else.
 */
export function peekNotificationRouting(signedPayload: string): { bundleId?: string; environment?: string } | null {
  const seg = signedPayload.split('.')[1];
  if (!seg) return null;
  try {
    const payload = JSON.parse(Buffer.from(seg, 'base64url').toString('utf8'));
    return { bundleId: payload?.data?.bundleId, environment: payload?.data?.environment };
  } catch {
    return null;
  }
}
