import { HINKAL_SUPPORTED_CHAINS as e, isSolanaLike as t, isTronLike as n } from "../constants/chains.constants.mjs";
import { transactionErrorCodes as r } from "../error-handling/error-codes.constants.mjs";
import { createJsonRpcProvider as i } from "../functions/utils/create-provider.mjs";
import { getContract as a } from "../functions/web3/getContractMetadata.mjs";
import { ethers as o } from "ethers";
//#region libs/shared/common/src/providers/EthersProviderAdapter.ts
var s = class {
	signer;
	chainId;
	fetchProviders = {};
	chainEventListener;
	constructor() {
		e.forEach((e) => {
			!t(e) && !n(e) && (this.fetchProviders[e] = i(e));
		});
	}
	initConnector() {}
	initSigner(e) {
		this.signer = e;
	}
	async init(e) {
		if (e && (this.chainId = e), !this.chainId) throw Error("No Chain Id In Provider Adapter");
	}
	async disconnectFromConnector() {}
	async connectToConnector() {
		return 0;
	}
	async waitForTransaction(e, t, n) {
		if ((await this.fetchProviders[e]?.waitForTransaction(t, n))?.status) return !0;
		throw Error(r.TRANSACTION_NOT_CONFIRMED);
	}
	async signMessage(e) {
		if (!this.signer) throw Error("IllegalState: no signer");
		let t = await this.signer.signMessage(e);
		if (!t) throw Error(r.SIGNING_FAILED);
		if (t.includes("error")) throw Error(r.SIGNATURE_UNSUPPORTED_PERSONAL_SIGN);
		return t;
	}
	async signTypedData(e, t, n) {
		return this.signer.signTypedData(e, t, n);
	}
	async switchNetwork(e) {
		let t = this.fetchProviders[e.chainId];
		if (!t) throw Error("switchNetwork illegal state: no provider");
		this.signer = this.signer?.connect(t), this.chainEventListener?.onChainChanged(e.chainId);
	}
	async getAddress() {
		if (!this.signer) throw Error("IllegalState: no signer");
		let e = await this.signer.getAddress();
		if (!e) throw Error("IllegalState");
		return o.getAddress(e);
	}
	setChainEventListener(e) {
		this.chainEventListener = e;
	}
	onAccountChanged() {
		return this.init();
	}
	onChainChanged(e) {
		return this.init(e);
	}
	release() {
		this.removeListeners();
	}
	removeListeners() {
		this.chainEventListener = void 0;
	}
	getContract(e, t, n = void 0, r) {
		return a(t, e, n, r);
	}
	getContractWithSigner(e, t, n = void 0) {
		if (!this.signer) throw Error("IllegalState: no signer");
		return this.getContract(e, t, n, this.signer);
	}
	getContractWithFetcher(e, t, n = void 0) {
		if (!this.fetchProviders[e]) throw Error("fetchProvider not initialized");
		return this.getContract(e, t, n, this.fetchProviders[e]);
	}
	async sendTransaction(e) {
		if (!this.signer) throw Error("IllegalState: no signer");
		return await this.signer.sendTransaction(e);
	}
	async connectAndPatchProvider() {
		if (!this.signer) throw Error("IllegalState: no signer");
		let { chainId: e } = await this.signer.provider.getNetwork();
		return Number(e);
	}
	getChainId() {
		return this.chainId;
	}
	isPermitterAvailable() {
		return !1;
	}
	async getGasPrice(e) {
		let t = (await this.fetchProviders[e]?.getFeeData())?.gasPrice;
		if (!t) throw Error("Could not fetch gas price in getGasPrice");
		return t;
	}
}, c = new s(), l = () => new s();
//#endregion
export { s as EthersProviderAdapter, l as default, c as ethersProvierAdapter };
