/**
 * OAuth provider registry — dispatches id_token verification to the correct provider.
 */

import { verifyGoogleIdToken } from './google.js';
import { verifyMicrosoftIdToken } from './microsoft.js';
import { verifyAppleIdToken } from './apple.js';
import type { OAuthVerifiedIdentity } from './types.js';

export type OAuthProvider = 'google' | 'microsoft' | 'apple';

const verifiers: Record<string, (idToken: string) => Promise<OAuthVerifiedIdentity>> = {
  google: verifyGoogleIdToken,
  microsoft: verifyMicrosoftIdToken,
  apple: (idToken: string) => verifyAppleIdToken(idToken),
};

export function registerVerifier(
  provider: string,
  fn: (idToken: string) => Promise<OAuthVerifiedIdentity>
): void {
  verifiers[provider] = fn;
}

export async function verifyIdToken(
  provider: OAuthProvider,
  idToken: string
): Promise<OAuthVerifiedIdentity> {
  const verifier = verifiers[provider];
  if (!verifier) {
    throw new Error(`OAuth provider "${provider}" is not configured`);
  }
  return verifier(idToken);
}
