import {
	type EstimateContractGasErrorType,
	EstimateGasExecutionError,
	SwitchChainError,
	TransactionExecutionError,
	TransactionRejectedRpcError,
	UserRejectedRequestError,
} from "viem";

export const getCommonErrors = (error: Error) => {
	if (SwitchChainError.name === error.name) {
		return {
			shortMessage: new SwitchChainError(error).shortMessage,
			name: "Chain switch unsuccessful",
		};
	}

	if (TransactionRejectedRpcError.name === error.name) {
		return {
			shortMessage: new TransactionRejectedRpcError(error).shortMessage,
			name: "RPC Error",
		};
	}

	if (UserRejectedRequestError.name === error.name) {
		return {
			shortMessage: new UserRejectedRequestError(error).shortMessage,
			name: "User Rejected Request",
		};
	}

	if (TransactionExecutionError.name === error.name) {
		return {
			shortMessage: new UserRejectedRequestError(error).shortMessage,
			name: "Execution Error",
		};
	}

	if (EstimateGasExecutionError.name === error.name) {
		return {
			shortMessage: new EstimateGasExecutionError(
				error as EstimateContractGasErrorType,
				{},
			).shortMessage,
			name: "Gas Estimation Error",
		};
	}

	return {
		shortMessage: error.message,
		name: error.name,
	};
};
