import { getMessageFromCode, errorCodes, } from "eth-rpc-errors";
const USER_REJECTED_CODES = [
  errorCodes.provider.userRejectedRequest,
  'ACTION_REJECTED',
];
const UNAUTHORIZED_CODE = '0x82b42900';
export const handleError = (e) => {
  let error = e;
  if (e.code === errorCodes.rpc.internal) {
    error = e.data || e;
  }
  const msg = typeof error === 'string'
    ? error
    : (error === null || error === void 0 ? void 0 : error.message) ||
      getMessageFromCode(error === null || error === void 0 ? void 0 : error.code, 'Something went wrong');
  if (msg.includes('insufficient funds') ||
    msg.includes('ERC20: transfer amount exceeds balance')) {
    return "You don't have enough balance in your wallet to cover the cost of this transaction.";
  }
  if (msg.includes(UNAUTHORIZED_CODE)) {
    return 'Unauthorized access. Please connect to the correct wallet or contact us for support.';
  }
  if (USER_REJECTED_CODES.includes(error === null || error === void 0 ? void 0 : error.code)) {
    return 'User denied transaction signature.';
  }
  const regex = /("(message)":|reason=)("([^""]+)"|\[[^[]+])/;
  const errors = msg.match(regex);
  return (errors === null || errors === void 0 ? void 0 : errors.length) ? errors[errors.length - 1] : msg;
};
//# sourceMappingURL=errors.js.map
