"use client"; import { jsx as A } from "react/jsx-runtime"; import { useContext as b, useMemo as v, useRef as R, useState as q, useCallback as s, useEffect as M } from "react"; import P from "keycloak-js"; import { d as T } from "./chunks/feature-registry-B2iuEiEx.js"; import { C as j, K as B } from "./chunks/provider-runtime-CxEpkUq5.js"; import { b as U } from "./chunks/provider-runtime-CxEpkUq5.js"; import { setKeycloakLogoutHandler as L } from "./core-runtime.js"; function F({ children: C, clientId: c, url: f = "https://auth.ibagher.ir", realm: d = "bi", initOptions: h, loading: k, errorFallback: S = null, allowUnauthenticated: w = !1, onError: i }) { if (!c) throw new Error(" requires clientId."); const x = b(j), z = k !== void 0 ? k : x ?? null, e = v( () => new P({ url: f, realm: d, clientId: c }), [f, d, c] ), m = R(/* @__PURE__ */ new WeakMap()), [t, g] = q({ client: e, initialized: !1, authenticated: !1, token: null, error: null }), a = s(() => { g({ client: e, initialized: !0, authenticated: !!e.authenticated, token: e.token ?? null, error: null }); }, [e]); M(() => { let n = !0; const o = () => { n && a(); }; e.onAuthSuccess = o, e.onAuthRefreshSuccess = o, e.onAuthLogout = o, L((r) => e.logout(r)); let u = m.current.get(e); return u || (u = Promise.resolve().then(() => e.init({ onLoad: "login-required", checkLoginIframe: !1, flow: "implicit", // Matches the existing core/BiProvider behavior. ...h })), m.current.set(e, u)), u.then(o, (r) => { n && (g({ client: e, initialized: !0, authenticated: !1, token: null, error: r }), i == null || i(r)); }), () => { n = !1, e.onAuthSuccess = void 0, e.onAuthRefreshSuccess = void 0, e.onAuthLogout = void 0, L(null); }; }, [e, h, i, a]); const p = s((n) => e.login(n), [e]), y = s((n) => e.logout(n), [e]), K = s(async (n = 30) => { const o = await e.updateToken(n); return a(), o; }, [e, a]), l = v(() => ({ instance: e, initialized: t.client === e && t.initialized, authenticated: t.client === e && t.authenticated, token: t.client === e ? t.token : null, error: t.client === e ? t.error : null, login: p, logout: y, updateToken: K }), [e, t, p, y, K]); return l.initialized ? l.error ? S : !l.authenticated && !w ? null : /* @__PURE__ */ A(B.Provider, { value: l, children: C }) : z; } const J = T("Keycloak", 40, F); export { J as Keycloak, U as useKeycloak };