import { noop as c } from "@tanstack/react-query";
import { l as o } from "../../localstorage-DVS0q1yC.js";
import { logger as m } from "../../logger.js";
import { nearSingleRpcClient as f } from "./rpc.js";
const E = 300, C = "ACCOUNT_CHECK_SUPERSEDED", s = /* @__PURE__ */ new Map();
let r = null;
const p = (e) => {
  const t = new Set(o.getItem("verifiedNearAccounts"));
  t.add(e), o.setItem("verifiedNearAccounts", [...t]);
}, N = async (e) => {
  try {
    return await f.query({
      request_type: "view_account",
      account_id: e,
      finality: "final"
    }), p(e), s.set(e, !0), !0;
  } catch (t) {
    const n = /doesn.t exist|does not exist|UNKNOWN_ACCOUNT|AccountDoesNotExist/i, i = t instanceof Error ? `${t.message} ${String(t)}` : String(t);
    return n.test(i) ? (s.set(e, !1), !1) : (m.error(new Error("NEAR RPC check failed", { cause: t })), !0);
  }
}, y = async (e) => {
  if (s.has(e))
    return s.get(e) ?? !1;
  if (new Set(o.getItem("verifiedNearAccounts")).has(e))
    return s.set(e, !0), !0;
  if (r?.accountId === e)
    return r.promise;
  r && (clearTimeout(r.timer), r.reject(new Error(C)), r = null);
  let t = c, n = c;
  const i = new Promise((l, u) => {
    t = l, n = u;
  }), a = setTimeout(async () => {
    r = null, t(await N(e));
  }, E);
  return r = {
    accountId: e,
    promise: i,
    timer: a,
    resolve: t,
    reject: n
  }, i;
};
export {
  C as ACCOUNT_CHECK_SUPERSEDED,
  y as checkNearAccountExists
};
//# sourceMappingURL=checkNearAccountExists.js.map
