import { jsxs as u, Fragment as _, jsx as i } from "react/jsx-runtime";
import { useRef as b, useEffect as l } from "react";
import E from "react-use/lib/useIdle";
import { useLocalStorageState as W } from "ahooks";
import h from "@arcblock/react-hooks/lib/useBrowser";
import { getWebWalletUrl as y, checkSameProtocol as S } from "../Util/wallet.js";
import { detectWalletExtension as w } from "../Util/index.js";
const I = 1e3 * 60 * 30, x = "wallet_sw_keeper_disabled";
let s;
const g = (t) => {
  const e = document.createElement("iframe");
  e.title = "abt wallet", e.id = s, e.style.width = "0", e.style.height = "0", e.style.border = "0", e.style.display = "block", e.style.position = "absolute", e.style.top = "0", e.src = `${t}?action=iframe`, document.body.appendChild(e);
}, D = () => {
  const t = document.getElementById(s);
  t && document.body.removeChild(t);
}, k = () => {
  D(), s = "";
}, A = (t) => {
  s || (s = `web_wallet_sw_keeper_${Date.now()}`, g(t));
};
function K({
  webWalletUrl: t,
  maxIdleTime: e = I
}) {
  const o = b(!!s), n = E(e), r = () => {
    o.current || k();
  };
  return l(() => {
    n ? r() : A(t);
  }, [n]), l(() => () => r(), []), null;
}
const M = (t) => function({ webWalletUrl: o, maxIdleTime: n, ...r }) {
  const c = h();
  o = o || y();
  const [a] = W(x, {
    defaultValue: 1
  }), m = w(), d = S(o), f = c.wallet || c.arcSphere, p = { webWalletUrl: o, ...r };
  return /* @__PURE__ */ u(_, { children: [
    !a && !m && d && !f && /* @__PURE__ */ i(K, { webWalletUrl: o, maxIdleTime: n }),
    /* @__PURE__ */ i(t, { ...p })
  ] });
};
export {
  K as default,
  M as withWebWalletSWKeeper
};
