import { useState as p } from "react";
import { B as g } from "../index-DEq3oXQF.js";
import { s as y, D as N } from "../config-D-Ik_SSx.js";
import { notReachable as S } from "../utils/notReachable.js";
import { getIntentsAccountId as E } from "../utils/intents/getIntentsAccountId.js";
import { l } from "../localstorage-DVS0q1yC.js";
import { verifyWalletSignature as I, walletVerificationMessageFactory as d } from "../utils/intents/walletCompatibilityVerification.js";
import { logger as D } from "../logger.js";
function i(t, n) {
  if (t == null)
    throw D.error(n), new N({
      code: "TRANSFER_INVALID_INITIAL",
      meta: { message: n }
    });
}
function K({ providers: t, walletAddress: n }) {
  const [f, c] = p(!1), { intentsAccountType: a } = y(), u = E({
    walletAddress: n,
    addressType: a
  });
  async function m() {
    i(n, "No wallet connected"), i(a, "Intents account type is not defined"), i(u, "Intents account ID is not defined");
    const e = d(n, a === "sol" ? "solana" : a);
    switch (a) {
      case "evm":
        return i(t?.evm, "No EVM provider configured"), {
          type: "ERC191",
          signatureData: await (typeof t.evm == "function" ? await t.evm() : t.evm).request({
            method: "personal_sign",
            params: [e.ERC191.message, u]
          }),
          signedData: e.ERC191
        };
      case "sol":
        return i(t?.sol, "No SOL provider configured"), {
          type: "SOLANA",
          signatureData: await t.sol.signMessage(
            e.SOLANA.message
          ),
          signedData: e.SOLANA
        };
      case "near": {
        const s = t?.near ? t.near() : null;
        i(s?.signMessage, "No NEAR provider configured");
        const r = await s.signMessage({
          message: e.NEP413.message,
          nonce: g.from(e.NEP413.nonce),
          recipient: e.NEP413.recipient
        });
        return i(r, "No public key returned from NEAR wallet"), {
          type: "NEP413",
          signatureData: {
            accountId: r.accountId,
            publicKey: r.publicKey.toString(),
            signature: r.signature.toString()
          },
          signedData: e.NEP413
        };
      }
      case "stellar": {
        i(t?.stellar, "No Stellar provider configured");
        const { signedMessage: s } = await t.stellar.signMessage(
          e.STELLAR.message
        );
        return {
          type: "STELLAR_SEP53",
          signatureData: new Uint8Array(g.from(s, "base64")),
          signedData: e.STELLAR
        };
      }
      default:
        S(a);
    }
  }
  return {
    handleSign: async () => {
      if (!a)
        return !1;
      try {
        c(!0);
        const o = await m(), e = await I(
          o,
          n,
          a === "sol" ? "solana" : a
        );
        if (e && a === "near" && n) {
          const s = l.getItem("nearWalletsPk") ?? {};
          !s[n] && o.type === "NEP413" && l.setItem("nearWalletsPk", {
            ...s,
            [n]: o.signatureData.publicKey
          });
        }
        return e;
      } catch {
        return !1;
      } finally {
        c(!1);
      }
    },
    isSigning: f
  };
}
export {
  K as useCompatibilityCheck
};
//# sourceMappingURL=useCompatibilityCheck.js.map
