import { jsx as S, jsxs as L } from "react/jsx-runtime";
import { createContext as R, useRef as b, useState as h, use as w } from "react";
import l from "prop-types";
import x from "lodash/omit";
import { RequestStorageAccessApiDialog as F } from "@arcblock/ux/lib/DIDConnect";
import { joinURL as U } from "ufo";
import { getFederatedEnabled as _, getMaster as v } from "@arcblock/ux/lib/Util/federated";
import { useLocalStorageState as I, useMemoizedFn as p, useCreation as T, useInterval as k } from "ahooks";
import { BLOCKLET_SERVICE_PATH_PREFIX as q } from "@arcblock/ux/lib/Util/constant";
import P from "js-cookie";
import { getBrowserLang as D } from "../utils.js";
import { LANG_COOKIE_NAME as M, CHECK_INTERVAL_TIME as j } from "../constant.js";
const m = R({}), { Provider: y, Consumer: Z } = m;
function K({ children: o, locale: d = "en" }) {
  const c = window?.blocklet, n = _(c), r = v(c), i = b(null), [e, u] = I("storage-access", {
    defaultValue: {
      appUrl: r?.appUrl,
      iat: Date.now(),
      state: void 0
    }
  }), [s, f] = h(d), g = p(() => {
    const t = P.get(M) || D();
    t !== s && f(t);
  }), A = p(async () => {
    const { value: t, origin: E } = await i.current.requestStorageAccess();
    let a = e.state || "prompt";
    return E === "broswer" && e.state && (a = t ? "granted" : "denied"), a !== e.state && u({
      ...e,
      iat: Date.now(),
      state: a
    }), t;
  }), C = T(() => r?.appUrl && n ? /* @__PURE__ */ S(
    F,
    {
      ref: i,
      locale: s,
      src: U(r.appUrl, q, "/share/shared-bridge.html"),
      storageAccessState: e.state
    }
  ) : null, [s, r?.appUrl, n, e.appUrl, e.iat, e.state]);
  return k(() => {
    g();
  }, j), /* @__PURE__ */ L(y, { value: { requestStorageAccess: A }, children: [
    o,
    C
  ] });
}
K.propTypes = {
  children: l.node.isRequired,
  locale: l.string
};
function $() {
  const o = w(m);
  return x(o, ["locale"]);
}
export {
  Z as FederatedConsumer,
  m as FederatedContext,
  K as FederatedProvider,
  $ as useFederatedContext
};
