{"version":3,"sources":["../src/errors.ts"],"sourcesContent":["import type { AuthErrorResponse, ErrorCode } from \"@tern-secure/types\";\n\n\nexport class TernSecureError extends Error {\n  code: ErrorCode\n\n  constructor(code: ErrorCode, message?: string) {\n    super(message || code)\n    this.name = \"TernSecureError\"\n    this.code = code\n  }\n}\n\n\n/**\n * Handles Firebase authentication errors with multiple fallback mechanisms\n */\nexport function handleFirebaseAuthError(error: unknown): AuthErrorResponse {\n  function extractErrorInfo(input: unknown): { code: string; message: string } | null {\n    // Case 1: String input (direct Firebase error message)\n    if (typeof input === 'string') {\n      const match = input.match(/Firebase:\\s*Error\\s*\\(auth\\/([^)]+)\\)/);\n      if (match) {\n        return { code: match[1], message: input };\n      }\n    }\n\n    // Case 2: Error object\n    if (input && typeof input === 'object') {\n      const err = input as { code?: string; message?: string };\n      \n      // Check for bundled message format first\n      if (err.message) {\n        const match = err.message.match(/Firebase:\\s*Error\\s*\\(auth\\/([^)]+)\\)/);\n        if (match) {\n          return { code: match[1], message: err.message };\n        }\n      }\n\n      // Check for direct code\n      if (err.code) {\n        return {\n          code: err.code.replace('auth/', ''),\n          message: err.message || ''\n        };\n      }\n    }\n\n    return null;\n  }\n\n  // Map error codes to user-friendly messages\n  const ERROR_MESSAGES: Record<string, { message: string; code: ErrorCode }> = {\n    'invalid-email': { message: 'Invalid email format', code: 'INVALID_EMAIL' },\n    'invalid-tenant-id': { message: 'Invalid tenant ID', code: 'INVALID_ARGUMENT' },\n    'invalid-credential': { message: 'Invalid email or password', code: 'INVALID_CREDENTIALS' },\n    'invalid-login-credentials': { message: 'Invalid email or password', code: 'INVALID_CREDENTIALS' },\n    'wrong-password': { message: 'Invalid email or password', code: 'INVALID_CREDENTIALS' },\n    'user-not-found': { message: 'Invalid email or password', code: 'INVALID_CREDENTIALS' },\n    'user-disabled': { message: 'This account has been disabled', code: 'USER_DISABLED' },\n    'too-many-requests': { message: 'Too many attempts. Please try again later', code: 'TOO_MANY_ATTEMPTS' },\n    'network-request-failed': { message: 'Network error. Please check your connection', code: 'NETWORK_ERROR' },\n    'email-already-in-use': { message: 'This email is already in use', code: 'EMAIL_EXISTS' },\n    'weak-password': { message: 'Password is too weak', code: 'WEAK_PASSWORD' },\n    'operation-not-allowed': { message: 'This login method is not enabled', code: 'OPERATION_NOT_ALLOWED' },\n    'popup-blocked': { message: 'Login popup was blocked. Please enable popups', code: 'POPUP_BLOCKED' },\n    'expired-action-code': { message: 'Your session has expired. Please login again', code: 'EXPIRED_TOKEN' },\n    'user-token-expired': { message: 'Your session has expired. Please login again', code: 'EXPIRED_TOKEN' },\n    'tenant-not-found': { message: 'There is no tenant corresponding to the provided identifier.', code: 'INVALID_ARGUMENT' },\n  };\n\n  try {\n    // Extract error information\n    const errorInfo = extractErrorInfo(error);\n    \n    if (errorInfo) {\n      const mappedError = ERROR_MESSAGES[errorInfo.code];\n      if (mappedError) {\n        return {\n          success: false,\n          message: mappedError.message,\n          code: mappedError.code\n        };\n      }\n    }\n\n    // If we couldn't extract or map the error, try one last time with string conversion\n    const errorString = String(error);\n    const lastMatch = errorString.match(/Firebase:\\s*Error\\s*\\(auth\\/([^)]+)\\)/);\n    if (lastMatch && ERROR_MESSAGES[lastMatch[1]]) {\n      return {\n        success: false,\n        ...ERROR_MESSAGES[lastMatch[1]]\n      };\n    }\n\n  } catch (e) {\n    // Silent catch - we'll return the default error\n  }\n\n  // Default fallback\n  return {\n    success: false,\n    message: 'An unexpected error occurred. Please try again later',\n    code: 'INTERNAL_ERROR'\n  };\n}\n\n/**\n * Type guard to check if a response is an AuthErrorResponse\n */\nexport function isAuthErrorResponse(response: unknown): response is AuthErrorResponse {\n  return (\n    typeof response === \"object\" &&\n    response !== null &&\n    \"success\" in response &&\n    (response as { success: boolean }).success === false &&\n    \"code\" in response &&\n    \"message\" in response\n  )\n}\n\nexport function getErrorAlertVariant(error: any | undefined) {\n if (!error) return \"destructive\"\n\n  switch (error.error) {\n    case \"AUTHENTICATED\":\n      return \"default\"\n    case \"EMAIL_EXISTS\":\n    case \"UNAUTHENTICATED\":\n    case \"UNVERIFIED\":\n    case \"REQUIRES_VERIFICATION\":\n    case \"INVALID_EMAIL\":\n    case \"INVALID_TOKEN\":\n    case \"INTERNAL_ERROR\":\n    case \"USER_DISABLED\":\n    case \"TOO_MANY_ATTEMPTS\":\n    case \"NETWORK_ERROR\":\n    case \"SESSION_EXPIRED\":\n    case \"EXPIRED_TOKEN\":\n    case \"INVALID_CREDENTIALS\":\n    default:\n      return \"destructive\"\n  }\n}"],"mappings":";AAGO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EAGzC,YAAY,MAAiB,SAAkB;AAC7C,UAAM,WAAW,IAAI;AACrB,SAAK,OAAO;AACZ,SAAK,OAAO;AAAA,EACd;AACF;AAMO,SAAS,wBAAwB,OAAmC;AACzE,WAAS,iBAAiB,OAA0D;AAElF,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,QAAQ,MAAM,MAAM,uCAAuC;AACjE,UAAI,OAAO;AACT,eAAO,EAAE,MAAM,MAAM,CAAC,GAAG,SAAS,MAAM;AAAA,MAC1C;AAAA,IACF;AAGA,QAAI,SAAS,OAAO,UAAU,UAAU;AACtC,YAAM,MAAM;AAGZ,UAAI,IAAI,SAAS;AACf,cAAM,QAAQ,IAAI,QAAQ,MAAM,uCAAuC;AACvE,YAAI,OAAO;AACT,iBAAO,EAAE,MAAM,MAAM,CAAC,GAAG,SAAS,IAAI,QAAQ;AAAA,QAChD;AAAA,MACF;AAGA,UAAI,IAAI,MAAM;AACZ,eAAO;AAAA,UACL,MAAM,IAAI,KAAK,QAAQ,SAAS,EAAE;AAAA,UAClC,SAAS,IAAI,WAAW;AAAA,QAC1B;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAGA,QAAM,iBAAuE;AAAA,IAC3E,iBAAiB,EAAE,SAAS,wBAAwB,MAAM,gBAAgB;AAAA,IAC1E,qBAAqB,EAAE,SAAS,qBAAqB,MAAM,mBAAmB;AAAA,IAC9E,s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