import { jsx as b } from "react/jsx-runtime";
import { use as h, useEffect as y } from "react";
import g from "prop-types";
import w from "@arcblock/bridge";
import { useDebounceFn as x, useMemoizedFn as T } from "ahooks";
import D from "lodash/pick";
import P from "@arcblock/react-hooks/lib/useBrowser";
import { isUrl as _ } from "@arcblock/ux/lib/Util";
import { joinURL as k } from "ufo";
import { debug as n } from "../utils.js";
import { API_DID_PREFIX as A, BLOCKLET_SERVICE_PATH_PREFIX as E, DEFAULT_TIMEOUT as I } from "../constant.js";
import S from "./hooks/security.js";
import { SessionContext as K } from "../Session/context.js";
import { getWalletDid as U } from "../User/use-did.js";
function G(a) {
  const l = {
    prefix: A,
    checkInterval: 2e3,
    checkTimeout: I * 1e3,
    locale: "en",
    tokenKey: "_t_",
    encKey: "_ek_",
    baseUrl: "",
    messages: {},
    extraParams: {},
    forceConnected: !0,
    enabledConnectTypes: [],
    // 所有的
    disableSwitchApp: !1,
    hideCloseButton: !1,
    qrcodeSize: 160,
    options: {}
  };
  function c(r) {
    const { encryptKey: d, decrypt: f } = S(), m = P(), p = h(K), u = U(p?.session?.user), i = D(r, [
      //
      "action",
      "checkTimeout",
      "locale",
      "messages",
      // 'webWalletUrl', arcSphere 中不需要通过 web 登录，不需要此参数
      //
      "baseUrl",
      // 要写清楚 baseUrl 和 prefix 的处理逻辑
      "prefix",
      "checkInterval",
      "closeTimeout",
      "extraParams",
      "tokenKey",
      "encKey",
      "forceConnected",
      "provider",
      "enabledConnectTypes",
      "disableSwitchApp",
      "hideCloseButton",
      // 不允许用户关闭这个 connect
      // 'qrcodeSize', // 不应该允许配置？
      "options"
      // FIXME: @zhanghan 弄清楚参数的字段
      //
      // 'autoConnect',
      // 'saveConnect',
      // 'useSocket', // 后期再考虑实现
      // 'allowWallet',
      // 'showDownload',
      // 函数，需要在这一层进行处理
      // 'onError',
      // 'onSuccess',
      // 'onRecreateSession', // 貌似没有地方用，直接去掉？
      // 'onClose',
    ]), e = Object.assign({}, l, i);
    e.forceConnected === !0 && (e.forceConnected = u || !0);
    const C = r.serviceHost || window?.env?.apiPrefix || window?.blocklet?.prefix || "/";
    !_(e.prefix) && !e.prefix?.startsWith(E) && (e.prefix = k(C, e.prefix));
    const t = m.arcSphere && r.open && !window.forceOpenConnect, { run: s } = x(
      T(async () => {
        n("bridge call arcDidConnect", r, i, e);
        try {
          const o = await w.callArc("didConnect", { ...e, [e.encKey]: d });
          if (n("bridge callArc didConnect: result", o), o.action === "error") {
            r.onError(o.data);
            return;
          }
          if (o.action === "success") {
            r.onSuccess(o.data, f);
            return;
          }
          r.onClose(o.data);
        } catch (o) {
          logger.error("Failed to callArc didConnect", o);
        }
      }),
      {
        leading: !0,
        wait: 300
      }
    );
    return y(() => {
      t && (n("WrapConnect call"), s());
    }, [s, t]), t ? null : /* @__PURE__ */ b(a, { ...r });
  }
  return c.propTypes = {
    open: g.bool
  }, c;
}
export {
  G as default
};
