import { prepareBroadcastRequest as i, messageFactory as s, authIdentity as o } from "@defuse-protocol/internal-utils";
import { nearRpcClient as c } from "../near/rpc.js";
function u(e, t) {
  return i.prepareSwapSignedData(e, {
    userAddress: t.credential,
    userChainType: t.credentialType
  });
}
function d(e) {
  return o.authHandleToIntentsUserId(
    e.credential,
    e.credentialType
  );
}
function l(e) {
  return typeof e == "string" ? e : d(e);
}
function m(e, t) {
  const n = s.makeEmptyMessage({
    signerId: l(e.signerId),
    deadlineTimestamp: e.deadlineTimestamp ?? Date.now() + 3e5,
    nonce: e.nonce
  });
  if (t === "tron") {
    let r;
    try {
      r = JSON.parse(n.TRON.message);
    } catch {
      throw new Error("[WIDGET] Invalid TRON message format");
    }
    const a = {
      ...r,
      message_size_validation: "Validates message size compatibility with wallet signing requirements."
    };
    return {
      ...n,
      TRON: {
        ...n.TRON,
        message: JSON.stringify(a, null, 2)
      }
    };
  }
  return n;
}
async function p(e, t, n) {
  if (
    /**
     * NEP-413 signatures can't be verified onchain for explicit account IDs (e.g., foo.near)
     * until the user sends a one-time transaction to register their public key with the account.
     * So we fall back to local verification.
     */
    e.type === "NEP413"
  )
    return e.signatureData.accountId === t;
  const r = u(e, {
    credential: t,
    credentialType: n
  });
  try {
    return await c.query({
      request_type: "call_function",
      account_id: "intents.near",
      method_name: "simulate_intents",
      args_base64: btoa(JSON.stringify({ signed: [r] })),
      finality: "optimistic"
    }), !0;
  } catch {
    return !1;
  }
}
function I(e, t) {
  return m(
    {
      signerId: { credential: e, credentialType: t }
    },
    t
  );
}
export {
  p as verifyWalletSignature,
  I as walletVerificationMessageFactory
};
//# sourceMappingURL=walletCompatibilityVerification.js.map
