import { getApps as f } from "@arcblock/ux/lib/Util/federated";
import { isUrl as h } from "@arcblock/ux/lib/Util";
import { withQuery as w, joinURL as n } from "ufo";
import c from "semver";
import p from "@arcblock/bridge";
import P from "path-to-regexp";
import { BLOCKLET_SERVICE_PATH_PREFIX as a } from "../../constant.js";
import { logger as L } from "../../utils.js";
const b = [
  `${a}/connect`,
  `${a}/login`,
  `${a}/invite`,
  `${a}/issue-passport`,
  `${a}/oauth/*`
], y = b.map((t) => P(t, []));
async function S() {
  const t = window?.blocklet || {}, o = window.location.pathname, { componentId: s, CHAIN_TYPE: l = "arcblock", appPid: m } = t;
  if (y.some((e) => e.exec(o)))
    return null;
  const u = f();
  try {
    const g = new URLSearchParams(window.location.search).get("inviter") || localStorage.getItem("inviter"), d = w(n(window.location.origin, a, "/api/user/loginByWallet"), {
      inviter: g
    });
    return await p.asyncCall("arcLogin", {
      target: d,
      chainType: l,
      appPid: m,
      componentId: s,
      apps: u.map((r) => {
        let i = r?.appLogo;
        return h(i) || (i = n(r.appUrl, i)), {
          appName: r.appName,
          appPid: r.appPid,
          appUrl: r.appUrl,
          appLogo: i,
          sourceAppPid: r.sourceAppPid
        };
      })
    });
  } catch (e) {
    return L.error("Failed login mobile", e), null;
  }
}
async function B() {
  return await p.asyncCall("arcGetVisitorId");
}
function N({ platform: t, version: o } = {}) {
  return !!(t === "ios" && o && c.gte(o, "4.15.6") || t === "android" && o && c.gte(o, "4.15.1"));
}
export {
  N as checkEnableAutoLogin,
  B as getMobileVisitorId,
  S as login
};
