/**
 * PrivacyBoost SDK Error Codes
 *
 * These codes match the SCREAMING_SNAKE_CASE codes produced by the WASM layer.
 * Use them for switch/case matching in your error handlers.
 */
export const ErrorCodes = {
    // Wallet
    WALLET_NOT_CONNECTED: 'WALLET_NOT_CONNECTED',
    WALLET_CONNECTION_FAILED: 'WALLET_CONNECTION_FAILED',
    WRONG_NETWORK: 'WRONG_NETWORK',
    TRANSACTION_REJECTED: 'TRANSACTION_REJECTED',
    TRANSACTION_FAILED: 'TRANSACTION_FAILED',
    TRANSACTION_REVERTED: 'TRANSACTION_REVERTED',
    INSUFFICIENT_GAS: 'INSUFFICIENT_GAS',
    // Auth
    NOT_AUTHENTICATED: 'NOT_AUTHENTICATED',
    AUTHENTICATION_FAILED: 'AUTHENTICATION_FAILED',
    SESSION_EXPIRED: 'SESSION_EXPIRED',
    INVALID_TOKEN: 'INVALID_TOKEN',
    MISSING_KEYS: 'MISSING_KEYS',
    UNAUTHORIZED: 'UNAUTHORIZED',
    FORBIDDEN: 'FORBIDDEN',
    PERMISSION_DENIED: 'PERMISSION_DENIED',
    /** User-supplied PIN/password did not decrypt the vault. UI should prompt for retry. */
    INVALID_CREDENTIAL: 'INVALID_CREDENTIAL',
    /** Internal key management error not covered by a more specific code. */
    KEY_MANAGEMENT_ERROR: 'KEY_MANAGEMENT_ERROR',
    // Auth (server)
    AUTH_NONCE_EXPIRED: 'AUTH_NONCE_EXPIRED',
    AUTH_INVALID_SIGNATURE: 'AUTH_INVALID_SIGNATURE',
    AUTH_MPK_MISMATCH: 'AUTH_MPK_MISMATCH',
    AUTH_ETH_SIGNATURE_INVALID: 'AUTH_ETH_SIGNATURE_INVALID',
    AUTH_ETH_SIGNATURE_MISMATCH: 'AUTH_ETH_SIGNATURE_MISMATCH',
    AUTH_USER_NOT_REGISTERED: 'AUTH_USER_NOT_REGISTERED',
    AUTH_MPK_ALREADY_REGISTERED: 'AUTH_MPK_ALREADY_REGISTERED',
    // Validation
    INVALID_ADDRESS: 'INVALID_ADDRESS',
    INVALID_AMOUNT: 'INVALID_AMOUNT',
    INSUFFICIENT_BALANCE: 'INSUFFICIENT_BALANCE',
    INVALID_PARAMETER: 'INVALID_PARAMETER',
    MISSING_PARAMETER: 'MISSING_PARAMETER',
    INVALID_CONFIG: 'INVALID_CONFIG',
    VALIDATION_FAILED: 'VALIDATION_FAILED',
    INVALID_INPUT: 'INVALID_INPUT',
    // Network
    CHAIN_ERROR: 'CHAIN_ERROR',
    NETWORK_ERROR: 'NETWORK_ERROR',
    TIMEOUT: 'TIMEOUT',
    RATE_LIMITED: 'RATE_LIMITED',
    SERVICE_UNAVAILABLE: 'SERVICE_UNAVAILABLE',
    HTTP_ERROR: 'HTTP_ERROR',
    // Crypto
    CRYPTO_ERROR: 'CRYPTO_ERROR',
    ENCRYPTION_ERROR: 'ENCRYPTION_ERROR',
    DECRYPTION_ERROR: 'DECRYPTION_ERROR',
    KEY_DERIVATION_ERROR: 'KEY_DERIVATION_ERROR',
    // Compliance
    COMPLIANCE_BLOCKED: 'COMPLIANCE_BLOCKED',
    // Shield
    SHIELD_NOT_FOUND: 'SHIELD_NOT_FOUND',
    SHIELD_ALREADY_CLAIMED: 'SHIELD_ALREADY_CLAIMED',
    SHIELD_PROOF_EXPIRED: 'SHIELD_PROOF_EXPIRED',
    SHIELD_PROOF_FUTURE: 'SHIELD_PROOF_FUTURE',
    SHIELD_PROOF_MISMATCH: 'SHIELD_PROOF_MISMATCH',
    SHIELD_PUBKEY_MISMATCH: 'SHIELD_PUBKEY_MISMATCH',
    SHIELD_COMMITMENT_NOT_FOUND: 'SHIELD_COMMITMENT_NOT_FOUND',
    // Transfer
    TRANSFER_NOT_FOUND: 'TRANSFER_NOT_FOUND',
    TRANSFER_COMPLIANCE_FAILED: 'TRANSFER_COMPLIANCE_FAILED',
    TRANSFER_PROOF_FAILED: 'TRANSFER_PROOF_FAILED',
    TRANSFER_SUBMISSION_FAILED: 'TRANSFER_SUBMISSION_FAILED',
    TRANSFER_METHOD_NOT_SUPPORTED: 'TRANSFER_METHOD_NOT_SUPPORTED',
    // Note
    NOTE_NOT_FOUND: 'NOTE_NOT_FOUND',
    NOTE_ACCESS_DENIED: 'NOTE_ACCESS_DENIED',
    NOTE_ALREADY_SPENT: 'NOTE_ALREADY_SPENT',
    // Merkle
    MERKLE_LEAF_NOT_FOUND: 'MERKLE_LEAF_NOT_FOUND',
    MERKLE_PROOF_FAILED: 'MERKLE_PROOF_FAILED',
    MERKLE_ROOT_STALE: 'MERKLE_ROOT_STALE',
    // Transaction validation (server)
    INVALID_FIELD_ELEMENT: 'INVALID_FIELD_ELEMENT',
    VALUE_OUT_OF_RANGE: 'VALUE_OUT_OF_RANGE',
    TOKEN_ID_INVALID: 'TOKEN_ID_INVALID',
    ZERO_FIELD: 'ZERO_FIELD',
    NO_INPUTS: 'NO_INPUTS',
    NO_OUTPUTS: 'NO_OUTPUTS',
    TOO_MANY_INPUTS: 'TOO_MANY_INPUTS',
    TOO_MANY_OUTPUTS: 'TOO_MANY_OUTPUTS',
    INVALID_UNSHIELD_TO: 'INVALID_UNSHIELD_TO',
    ZERO_UNSHIELD_AMOUNT: 'ZERO_UNSHIELD_AMOUNT',
    MISSING_SIGNED_ROOT: 'MISSING_SIGNED_ROOT',
    ZERO_COMMITMENT: 'ZERO_COMMITMENT',
    DUPLICATE_NULLIFIER: 'DUPLICATE_NULLIFIER',
    TOKEN_ID_MISMATCH: 'TOKEN_ID_MISMATCH',
    INPUT_SUM_OVERFLOW: 'INPUT_SUM_OVERFLOW',
    OUTPUT_SUM_OVERFLOW: 'OUTPUT_SUM_OVERFLOW',
    BALANCE_MISMATCH: 'BALANCE_MISMATCH',
    LEAF_NOT_FOUND: 'LEAF_NOT_FOUND',
    INVALID_COMMITMENT: 'INVALID_COMMITMENT',
    NULLIFIER_ALREADY_SPENT: 'NULLIFIER_ALREADY_SPENT',
    AUTH_KEY_NOT_REGISTERED: 'AUTH_KEY_NOT_REGISTERED',
    INVALID_SIGNATURE: 'INVALID_SIGNATURE',
    INVALID_CIPHERTEXT: 'INVALID_CIPHERTEXT',
    MISSING_CIPHERTEXT: 'MISSING_CIPHERTEXT',
    NULLIFIER_ALREADY_USED: 'NULLIFIER_ALREADY_USED',
    INVALID_MAX_INPUTS: 'INVALID_MAX_INPUTS',
    NO_FEE_NOTES: 'NO_FEE_NOTES',
    BELOW_MIN_TRANSFER_AMOUNT: 'BELOW_MIN_TRANSFER_AMOUNT',
    // Internal
    SERIALIZATION_ERROR: 'SERIALIZATION_ERROR',
    OPERATION_FAILED: 'OPERATION_FAILED',
    INTERNAL_ERROR: 'INTERNAL_ERROR',
    // SDK lifecycle
    SDK_INITIALIZATION_FAILED: 'SDK_INITIALIZATION_FAILED',
    SDK_NOT_INITIALIZED: 'SDK_NOT_INITIALIZED',
};
const RETRYABLE_CODES = new Set([
    ErrorCodes.NETWORK_ERROR,
    ErrorCodes.TIMEOUT,
    ErrorCodes.RATE_LIMITED,
    ErrorCodes.SERVICE_UNAVAILABLE,
    ErrorCodes.MERKLE_ROOT_STALE,
]);
const AUTH_CODES = new Set([
    ErrorCodes.NOT_AUTHENTICATED,
    ErrorCodes.AUTHENTICATION_FAILED,
    ErrorCodes.SESSION_EXPIRED,
    ErrorCodes.INVALID_TOKEN,
    ErrorCodes.UNAUTHORIZED,
    ErrorCodes.FORBIDDEN,
    ErrorCodes.PERMISSION_DENIED,
    ErrorCodes.AUTH_NONCE_EXPIRED,
    ErrorCodes.AUTH_INVALID_SIGNATURE,
    ErrorCodes.AUTH_MPK_MISMATCH,
    ErrorCodes.AUTH_ETH_SIGNATURE_INVALID,
    ErrorCodes.AUTH_ETH_SIGNATURE_MISMATCH,
    ErrorCodes.AUTH_USER_NOT_REGISTERED,
    ErrorCodes.AUTH_MPK_ALREADY_REGISTERED,
    ErrorCodes.INVALID_CREDENTIAL,
]);
const COMPLIANCE_CODES = new Set([
    ErrorCodes.COMPLIANCE_BLOCKED,
    ErrorCodes.TRANSFER_COMPLIANCE_FAILED,
]);
const WALLET_CODES = new Set([
    ErrorCodes.WALLET_NOT_CONNECTED,
    ErrorCodes.WALLET_CONNECTION_FAILED,
    ErrorCodes.WRONG_NETWORK,
    ErrorCodes.TRANSACTION_REJECTED,
    ErrorCodes.TRANSACTION_FAILED,
    ErrorCodes.TRANSACTION_REVERTED,
    ErrorCodes.INSUFFICIENT_GAS,
]);
/**
 * Structured error thrown by the PrivacyBoost SDK.
 *
 * Wraps WASM errors with a machine-readable `code` property and
 * helper methods for classification.
 *
 * @example
 * ```ts
 * try {
 *   await sdk.vault.shield('0x...', 1000n);
 * } catch (err) {
 *   if (err instanceof PrivacyBoostError) {
 *     if (err.retryable) {
 *       // retry after backoff
 *     }
 *     switch (err.code) {
 *       case ErrorCodes.INSUFFICIENT_BALANCE:
 *         showToast('Not enough funds');
 *         break;
 *       case ErrorCodes.TRANSACTION_REJECTED:
 *         showToast('Transaction was rejected');
 *         break;
 *     }
 *   }
 * }
 * ```
 */
export class PrivacyBoostError extends Error {
    /** Machine-readable error code (e.g. `"WALLET_NOT_CONNECTED"`).
     *  May be an `ErrorCode` value or an unknown code from the WASM layer. */
    code;
    /** Whether this error is retryable */
    retryable;
    constructor(code, message, options) {
        super(message, { cause: options?.cause });
        this.name = 'PrivacyBoostError';
        this.code = code;
        this.retryable = options?.retryable ?? RETRYABLE_CODES.has(code);
    }
    /** Check if this is an authentication/authorization error */
    isAuthError() {
        return AUTH_CODES.has(this.code);
    }
    /** Check if this is a wallet-related error */
    isWalletError() {
        return WALLET_CODES.has(this.code);
    }
    /** Check if this is a compliance/screening error */
    isComplianceError() {
        return COMPLIANCE_CODES.has(this.code);
    }
    /** Check if the error matches a specific code */
    hasCode(code) {
        return this.code === code;
    }
    /**
     * Wrap an error thrown by the WASM layer into a PrivacyBoostError.
     * If the error is already a PrivacyBoostError, return it as-is.
     */
    static from(error) {
        if (error instanceof PrivacyBoostError) {
            return error;
        }
        if (error instanceof Error) {
            // WASM sets error.name to the code (e.g. "WALLET_NOT_CONNECTED")
            const code = error.name !== 'Error' ? error.name : ErrorCodes.INTERNAL_ERROR;
            return new PrivacyBoostError(code, error.message, { cause: error });
        }
        return new PrivacyBoostError(ErrorCodes.INTERNAL_ERROR, String(error), { cause: error });
    }
}
//# sourceMappingURL=errors.js.map