import { authenticator } from 'otplib';

export function generateTotpSecret(): string {
  return authenticator.generateSecret();
}

export function verifyTotpCode(secret: string, code: string): boolean {
  try {
    return authenticator.verify({ token: code.replace(/\s+/gu, ''), secret });
  } catch {
    return false;
  }
}

export function buildOtpAuthUrl(
  issuer: string,
  accountName: string,
  secret: string,
): string {
  return authenticator.keyuri(accountName, issuer, secret);
}
