import { Platform } from 'react-native';
export const PARA_PASSKEY_ERROR_CODES = [
    'USER_CANCELLED',
    'TIMED_OUT',
    'BAD_CONFIGURATION',
    'NOT_CONFIGURED',
    'RP_ID_MISMATCH',
    'NOT_SUPPORTED',
    'NO_CREDENTIALS',
    'REQUEST_FAILED',
    'INTERRUPTED',
    'UNKNOWN',
];
const DOCS_BASE_URL = 'https://docs.getpara.com/v2/react-native/error-codes';
export class ParaPasskeyError extends Error {
    constructor(params) {
        super(params.message);
        Object.setPrototypeOf(this, ParaPasskeyError.prototype);
        this.name = 'ParaPasskeyError';
        this.code = params.code;
        this.platform = params.platform;
        this.suggestion = params.suggestion;
        this.docsUrl = params.docsUrl;
        if (params.cause !== undefined) {
            this.cause = params.cause;
        }
    }
}
function isPasskeyError(error) {
    return (typeof error === 'object' &&
        error !== null &&
        'error' in error &&
        'message' in error &&
        typeof error.error === 'string' &&
        typeof error.message === 'string');
}
function detectPlatform() {
    if (Platform.OS === 'ios')
        return 'ios';
    if (Platform.OS === 'android')
        return 'android';
    return 'unknown';
}
function docsUrlForCode(code) {
    const fragment = code.toLowerCase();
    return `${DOCS_BASE_URL}#${fragment}`;
}
export function mapPasskeyError(error) {
    const platform = detectPlatform();
    if (!isPasskeyError(error)) {
        const code = 'UNKNOWN';
        return new ParaPasskeyError({
            code,
            message: error instanceof Error ? error.message : String(error),
            platform,
            suggestion: "An unexpected error occurred during the passkey operation. Check the 'cause' property for details.",
            docsUrl: docsUrlForCode(code),
            cause: error,
        });
    }
    const errorField = error.error;
    const messageField = error.message;
    switch (errorField) {
        case 'UserCancelled': {
            const code = 'USER_CANCELLED';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The user dismissed the passkey prompt. Handle this gracefully and allow the user to retry.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'TimedOut': {
            const code = 'TIMED_OUT';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The passkey operation timed out. Ensure the user completes the biometric prompt within the timeout period.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'BadConfiguration': {
            const code = 'BAD_CONFIGURATION';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'App entitlements or associated domains are misconfigured. Verify your webcredentials entries and confirm your Team ID + Bundle ID is registered in the Developer Portal.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'NotConfigured': {
            const code = 'NOT_CONFIGURED';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The credential provider is not configured. Ensure Google Play Services is available and a Google account is signed in on the device.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'NotSupported': {
            const code = 'NOT_SUPPORTED';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'Passkeys are not supported on this device. iOS 16+ or Android API 28+ is required.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'NoCredentials': {
            const code = 'NO_CREDENTIALS';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'No passkeys found for this user on this device. The user may need to register a passkey first or is on a different device.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'RequestFailed': {
            const code = 'REQUEST_FAILED';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The passkey request failed. On iOS, check associated domains and Team ID + Bundle ID registration. On Android, verify your SHA-256 fingerprint in the Developer Portal.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'Interrupted': {
            const code = 'INTERRUPTED';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The passkey operation was interrupted and may be retried.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'InvalidChallenge': {
            const code = 'BAD_CONFIGURATION';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The challenge provided to the passkey request was invalid. This is likely an SDK internal issue.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'InvalidUser':
        case 'InvalidUserId': {
            const code = 'BAD_CONFIGURATION';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The user ID provided to the passkey request was invalid. This is likely an SDK internal issue.',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'HandlerUndefined': {
            const code = 'NOT_SUPPORTED';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: 'The passkey handler is not available. This typically means the iOS version does not support passkeys (requires iOS 16+).',
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'Native error': {
            const gpsMatch = /\[(\d+)\]/.exec(messageField);
            if (gpsMatch && gpsMatch[1] === '50152') {
                const code = 'RP_ID_MISMATCH';
                return new ParaPasskeyError({
                    code,
                    message: messageField,
                    platform,
                    suggestion: "Your app's signing certificate SHA-256 fingerprint doesn't match what's registered in the Developer Portal. Debug and release builds use different signing keys. Run 'keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android' to get your debug fingerprint, then register it in the Developer Portal.",
                    docsUrl: docsUrlForCode(code),
                    cause: error,
                });
            }
            const code = 'UNKNOWN';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: "An unrecognized native passkey error occurred. Check the 'cause' property for the original error details.",
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        case 'UnknownError':
        case 'Unknown error': {
            const code = 'UNKNOWN';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: "An unknown passkey error occurred. Check the 'cause' property for details.",
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
        default: {
            const code = 'UNKNOWN';
            return new ParaPasskeyError({
                code,
                message: messageField,
                platform,
                suggestion: "An unexpected error occurred during the passkey operation. Check the 'cause' property for details.",
                docsUrl: docsUrlForCode(code),
                cause: error,
            });
        }
    }
}
export function logPasskeyDiagnostic(error, operation, relyingPartyId) {
    if (typeof __DEV__ !== 'undefined' && __DEV__) {
        console.error(`[Para] Passkey ${operation} failed\n` +
            `  Code:       ${error.code}\n` +
            `  Platform:   ${error.platform}\n` +
            `  Message:    ${error.message}\n` +
            `  RP ID:      ${relyingPartyId}\n` +
            `  Suggestion: ${error.suggestion}\n` +
            `  Docs:       ${error.docsUrl}`);
    }
}
