import { jsx as O } from "react/jsx-runtime";
import r from "prop-types";
import { useState as S, use as W, useRef as D, useEffect as V, createContext as X } from "react";
import Y from "@arcblock/react-hooks/lib/useBrowser";
import { useUpdate as Z, useCreation as n, useMemoizedFn as h, useLatest as $, useReactive as ee, useSize as te } from "ahooks";
import E from "lodash/isUndefined";
import oe from "lodash/noop";
import { Box as re } from "@mui/material";
import { translate as ne } from "@arcblock/ux/lib/Locale/util";
import { getMaster as T, getCurrentApp as se } from "@arcblock/ux/lib/Util/federated";
import { getDIDMotifInfo as ie } from "@arcblock/did-motif";
import { isEthereumDid as ce, getDIDColor as pe } from "@arcblock/ux/lib/Util";
import { GA_LAST_LOGIN_METHOD as ue } from "@arcblock/ux/lib/withTracker/constant";
import ae from "../hooks/use-apps.js";
import { SessionContext as de } from "../../Session/context.js";
import le from "../assets/locale.js";
const x = X({
  isWalletWebview: !1,
  isMobile: !1,
  matchSmallScreen: !1,
  blocklet: null
}), { Provider: fe, Consumer: We } = x;
function me({
  children: R,
  blocklet: c,
  masterBlocklet: P = void 0,
  action: a,
  extraParams: t = {},
  locale: d = "en",
  testOnlyBorderColor: l = void 0,
  ...L
}) {
  const w = Z(), [f, j] = S([]), [N, q] = S(!1), [m, z] = S(""), G = n(() => new Map(f.map((e) => [e.name, e])), [f]), J = h((e) => G.get(e)), U = $(m), _ = h((e, i = {}) => ne(le, e, d, "en", i)), g = W(de), o = ee({
    sourceAppPid: void 0,
    status: "created",
    autoActiveWebview: !0,
    deeplink: void 0,
    chooseMethod: "",
    retryConnect: oe
  }), k = D({
    cancelCount: 0
  }), s = Y(), { appInfoList: b, autoGenerateSourceAppPid: A, canSwitchApp: v } = ae({
    blocklet: c,
    connectState: o,
    action: a,
    sourceAppPid: t?.sourceAppPid,
    enableSwitchApp: t?.forceSwitch || t?.enableSwitchApp
  }), B = n(() => {
    try {
      return localStorage.getItem(ue);
    } catch {
      return "";
    }
  }, []), C = D(null), M = te(C), y = n(() => (M?.width || 0) < 500, [M?.width]), p = n(() => {
    if (v)
      return {
        ...t,
        sourceAppPid: E(o.sourceAppPid) ? A : o.sourceAppPid
      };
    const e = T(c)?.appPid, i = T(globalThis.blocklet)?.appPid;
    return g?.session?.user && (e === i || !e) ? E(t?.sourceAppPid) ? {
      ...t,
      sourceAppPid: g.session.user?.sourceAppPid
    } : t : {
      ...t,
      sourceAppPid: A
    };
  }, [
    v,
    t,
    o.sourceAppPid,
    a,
    A,
    g?.session?.user
  ]), F = n(() => {
    const e = window?.navigator?.userAgent;
    return e.indexOf("Edge") > -1 || e.indexOf("Edg") > -1 ? "edge" : e.indexOf("Chrome") > -1 ? "chrome" : "unknown";
  }, []);
  V(() => {
    o.sourceAppPid = p.sourceAppPid;
  }, [p.sourceAppPid]);
  const H = h((e) => {
    o.status = e || "created";
  }), u = globalThis.blocklet ? se(c) : globalThis.env, I = n(() => {
    if (l)
      return l;
    const e = u.appPid, i = ce(e), Q = i ? void 0 : ie(e);
    return i ? pe(e) : Q.color;
  }, [u.appId, l]), K = n(() => ({
    isWalletWebview: s.wallet || s.arcSphere,
    isMobile: s.mobile.any,
    matchSmallScreen: y,
    connectState: o,
    staticState: k,
    reactiveState: o,
    appInfoList: b,
    extraParams: p,
    blocklet: c,
    masterBlocklet: P,
    browserBrand: F,
    currentAppInfo: u,
    currentAppColor: I,
    t: _,
    locale: d,
    action: a,
    changeStatus: H,
    getPlugin: J,
    latestActivePlugin: U,
    plugins: f,
    setPlugins: j,
    selectedPlugin: m,
    setSelectedPlugin: z,
    forceUpdate: w,
    // 控制钱包登录的显示
    showWalletOptions: N,
    setShowWalletOptions: q,
    // 用于记录上一次登录方式
    lastLoginMethod: B
  }), [
    s.wallet,
    s.arcSphere,
    s.mobile.any,
    y,
    JSON.stringify(o),
    JSON.stringify(b),
    JSON.stringify(p),
    c,
    P,
    d,
    m,
    w,
    u,
    I
  ]);
  return /* @__PURE__ */ O(fe, { value: K, children: /* @__PURE__ */ O(re, { ...L, ref: C, children: R }) });
}
me.propTypes = {
  children: r.any.isRequired,
  blocklet: r.object.isRequired,
  masterBlocklet: r.object,
  action: r.string.isRequired,
  extraParams: r.object,
  locale: r.string,
  testOnlyBorderColor: r.string
};
function xe() {
  return W(x);
}
export {
  We as StateConsumer,
  x as StateContext,
  me as StateProvider,
  xe as useStateContext
};
