import { logger as c } from "../../logger.js";
class h extends Error {
  code;
  targetChainId;
  constructor(r, e, o) {
    super(r), this.name = "SwitchChainError", this.code = e, this.targetChainId = o;
  }
}
const n = async (t, r) => {
  try {
    const e = await r.request({
      method: "eth_chainId"
    }), o = parseInt(e, 16);
    if (o === t)
      return;
    await r.request({
      method: "wallet_switchEthereumChain",
      params: [{ chainId: `0x${t.toString(16)}` }]
    }), c.debug(
      `Successfully switched chain from ${o} to ${t}`
    );
  } catch (e) {
    throw e && typeof e == "object" && "code" in e && e.code === 4902 ? new h(
      `Chain ${t} is not available.`,
      "CHAIN_NOT_AVAILABLE",
      t
    ) : (c.error(e), new h(
      `Please switch to the correct network (Chain ID: ${t}) in your wallet`,
      "SWITCH_FAILED",
      t
    ));
  }
};
export {
  h as SwitchChainError,
  n as switchEthereumChain
};
//# sourceMappingURL=switchEthereumChain.js.map
