"use client"; import { jsx as L } from "react/jsx-runtime"; import { useMemo as K, useRef as b, useState as x, useCallback as s, useEffect as R } from "react"; import q from "keycloak-js"; import { d as C } from "./chunks/feature-registry-B2iuEiEx.js"; import { K as M } from "./chunks/provider-runtime-B6XIrDzf.js"; import { b as I } from "./chunks/provider-runtime-B6XIrDzf.js"; import { setKeycloakLogoutHandler as v } from "./core-runtime.js"; function P({ children: S, clientId: c, url: f = "https://auth.ibagher.ir", realm: d = "bi", initOptions: h, loading: w = null, errorFallback: z = null, allowUnauthenticated: A = !1, onError: i }) { if (!c) throw new Error(" requires clientId."); const e = K( () => new q({ url: f, realm: d, clientId: c }), [f, d, c] ), k = b(/* @__PURE__ */ new WeakMap()), [t, m] = x({ client: e, initialized: !1, authenticated: !1, token: null, error: null }), a = s(() => { m({ client: e, initialized: !0, authenticated: !!e.authenticated, token: e.token ?? null, error: null }); }, [e]); R(() => { let n = !0; const o = () => { n && a(); }; e.onAuthSuccess = o, e.onAuthRefreshSuccess = o, e.onAuthLogout = o, v((r) => e.logout(r)); let u = k.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 })), k.current.set(e, u)), u.then(o, (r) => { n && (m({ 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, v(null); }; }, [e, h, i, a]); const p = s((n) => e.login(n), [e]), g = s((n) => e.logout(n), [e]), y = s(async (n = 30) => { const o = await e.updateToken(n); return a(), o; }, [e, a]), l = K(() => ({ 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: g, updateToken: y }), [e, t, p, g, y]); return l.initialized ? l.error ? z : !l.authenticated && !A ? null : /* @__PURE__ */ L(M.Provider, { value: l, children: S }) : w; } const $ = C("Keycloak", 40, P); export { $ as Keycloak, I as useKeycloak };