'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var utils = require('@dynamic-labs/utils');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react');
require('react/jsx-runtime');
require('../../../context/ViewContext/ViewContext.cjs');
var logger = require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../../utils/constants/colors.cjs');
require('../../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../../shared/consts/index.cjs');

const magicRpcError = -32603;
const errorCodesMapping = {
    '-32002': 'Insufficient funds for this transaction.',
    0: 'The operation either timed out or was not allowed.',
    16: 'Invalid Passkey signature. Select the passkey for this account and device.',
    3: 'You reached the limit of passkeys for this account.',
    5: 'Invalid Passkey signature.',
    INSUFFICIENT_FUNDS: 'Insufficient funds for this transaction.',
    [magicRpcError.toString()]: 'A network error has occurred. Please try again later',
};
const errorReasonsMapping = {
    'provided ENS name resolves to null': 'Invalid address. Please check that the entered address is correct.',
};
const getCode = (error) => {
    var _a;
    const err = error;
    return (err === null || err === void 0 ? void 0 : err.code) || ((_a = err === null || err === void 0 ? void 0 : err.cause) === null || _a === void 0 ? void 0 : _a.code);
};
const getMessage = (error) => {
    const err = error;
    return err === null || err === void 0 ? void 0 : err.message;
};
const isConfirmationTimeoutError = (error) => error instanceof utils.TransactionConfirmationTimeoutError;
const getTransactionSignatureFromError = (error) => isConfirmationTimeoutError(error) ? error.transactionSignature : undefined;
const hasReason = (error) => 'reason' in error && error.reason !== undefined;
const isTransactionExecutionError = (error) => error !== undefined &&
    error !== null &&
    typeof error === 'object' &&
    'walk' in error &&
    typeof error['walk'] === 'function';
const getPolicyViolationErrorMsg = (error) => {
    const message = getMessage(error);
    return ((message === null || message === void 0 ? void 0 : message.replace('Error: ', '')) ||
        'Policy violation: unknown error, please try again.');
};
const isPolicyViolationError = (error) => {
    const message = getMessage(error);
    return ((message === null || message === void 0 ? void 0 : message.toLowerCase().includes('policy violation')) ||
        (message === null || message === void 0 ? void 0 : message.toLowerCase().includes('explicitly denied by policy')));
};
const transactionErrorMessage = (err) => {
    var _a, _b;
    let error = err;
    if (isTransactionExecutionError(err)) {
        error = err.walk();
    }
    logger.logger.debug('transaction error:', error);
    if (isPolicyViolationError(error)) {
        return getPolicyViolationErrorMsg(error);
    }
    if (utils.TransactionGasCannotBeSponsoredError.isInstance(error)) {
        return;
    }
    if (utils.InsufficientFundsError.isInstance(error)) {
        return errorCodesMapping.INSUFFICIENT_FUNDS;
    }
    if (isMagicInsufficientFundsError(error)) {
        return errorCodesMapping.INSUFFICIENT_FUNDS;
    }
    const code = getCode(error);
    if (code !== undefined && errorCodesMapping[code]) {
        return errorCodesMapping[code];
    }
    if (hasReason(error) && errorReasonsMapping[error.reason]) {
        return errorReasonsMapping[error.reason];
    }
    if (isConfirmationTimeoutError(error)) {
        return undefined; // Use translation key in UI for timeout message
    }
    // Midnight: every transaction needs DUST to pay fees. The wallet client throws
    // "No dust balance — register dust first" when the wallet has no registered
    // dust, so surface an actionable message instead of the generic fallback.
    const message = (_b = (_a = getMessage(error)) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '';
    if (message.includes('no dust') || message.includes('register dust')) {
        return 'You need DUST to pay network fees. Register DUST first, then try again.';
    }
    return 'Something went wrong with the transaction. Please try again.';
};
const isMagicInsufficientFundsError = (error) => {
    const code = getCode(error);
    const message = getMessage(error);
    return (code === magicRpcError &&
        Boolean(message === null || message === void 0 ? void 0 : message.includes('insufficient funds for gas * price + value')));
};

exports.getPolicyViolationErrorMsg = getPolicyViolationErrorMsg;
exports.getTransactionSignatureFromError = getTransactionSignatureFromError;
exports.isConfirmationTimeoutError = isConfirmationTimeoutError;
exports.isPolicyViolationError = isPolicyViolationError;
exports.isTransactionExecutionError = isTransactionExecutionError;
exports.transactionErrorMessage = transactionErrorMessage;
