import { jsxs as B, jsx as a, Fragment as Z } from "react/jsx-runtime"; import { useState as v, useRef as N, useEffect as C, useCallback as E, createContext as $, useMemo as z, useContext as P } from "react"; import { HStack as I, IconButton as D, Button as L, Input as U, Text as S, Spinner as ee } from "@chakra-ui/react"; import { IoChevronForwardOutline as g, IoChevronBackOutline as O } from "react-icons/io5"; import te from "keycloak-js"; import { useQuery as ne } from "@tanstack/react-query"; import re from "axios"; const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => String(e).replace(/\d/g, (t) => "۰۱۲۳۴۵۶۷۸۹"[Number(t)]), K = (e) => String(e).replace(/[۰-۹]/g, (n) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(n))), Ke = ({ currentPage: e, totalPages: t, totalCount: n, onPageChange: s }) => { const [c, l] = v(p(e)), [i, o] = v(!1), [f, u] = v(null), w = N(null), y = [.../* @__PURE__ */ new Set([1, e, t])].filter( (r) => r >= 1 && r <= t ); C(() => { if (f !== null) { e === f && (u(null), o(!1), l(p(e))); return; } i || l(p(e)); }, [e, i, f]), C(() => { if (!i || !c || f !== null) return; const r = parseInt(K(c), 10); if (isNaN(r)) return; const h = setTimeout(() => { var b; const m = Math.min(Math.max(r, 1), t); m !== e ? (u(m), l(p(m)), s(m)) : (o(!1), l(p(e))), (b = w.current) == null || b.blur(); }, 1e3); return () => clearTimeout(h); }, [ c, i, f, t, e, s ]); const A = () => { var m, b; const r = parseInt(K(c), 10); if (isNaN(r)) { l(p(e)), o(!1), u(null), (m = w.current) == null || m.blur(); return; } const h = Math.min(Math.max(r, 1), t); h !== e ? (u(h), l(p(h)), s(h)) : (l(p(e)), o(!1), u(null)), (b = w.current) == null || b.blur(); }; return t === 0 || !n ? null : /* @__PURE__ */ B( I, { justifyContent: "space-around", backgroundColor: "white", py: "10px", px: "16px", rounded: "lg", border: "1px", borderColor: "blue.200", w: "100%", children: [ /* @__PURE__ */ B(I, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ /* @__PURE__ */ a( D, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, onClick: () => s(e - 1), isDisabled: e === 1, icon: /* @__PURE__ */ a(g, {}), "aria-label": "صفحه قبلی" } ), y.map((r) => /* @__PURE__ */ a( L, { size: "md", px: 2, bg: e === r ? "#4B49AC" : "#e6e6f1", color: e === r ? "#e6e6f1" : "#4B49AC", _hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" }, onClick: () => s(r), variant: "soft", children: p(r) }, r )), /* @__PURE__ */ a( D, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, onClick: () => s(e + 1), isDisabled: e === t, icon: /* @__PURE__ */ a(O, {}), "aria-label": "صفحه بعدی" } ), /* @__PURE__ */ a( U, { ref: w, type: "text", inputMode: "numeric", placeholder: "صفحه", w: "56px", value: c, onFocus: () => { o(!0), u(null), l(""); }, onChange: (r) => { const h = r.target.value, m = K(h).replace(/\D/g, ""); l(p(m)); }, onBlur: () => { c || (l(p(e)), o(!1), u(null)); }, onKeyDown: (r) => { r.key === "Enter" && A(); }, textAlign: "center", color: "#1D2939", px: 1, minW: 0, dir: "ltr" } ) ] }), /* @__PURE__ */ B(I, { spacing: 2, children: [ /* @__PURE__ */ a(S, { w: "auto", fontWeight: "bold", whiteSpace: "nowrap", children: "تعداد کل:" }), /* @__PURE__ */ a(S, { children: p(n) }) ] }) ] } ); }, De = ({ currentPage: e, totalPages: t, totalCount: n, onPageChange: s }) => { const [c, l] = v(p(e)), [i, o] = v(!1), [f, u] = v(null), w = N(null), y = Array.from( /* @__PURE__ */ new Set([1, e, e + 1, t]) ).filter((r) => r >= 1 && r <= t).sort((r, h) => r - h); C(() => { if (f !== null) { e === f && (u(null), o(!1), l(p(e))); return; } i || l(p(e)); }, [e, i, f]), C(() => { if (!i || !c || f !== null) return; const r = parseInt(K(c), 10); if (isNaN(r)) return; const h = setTimeout(() => { var b; const m = Math.min(Math.max(r, 1), t); m !== e ? (u(m), l(p(m)), s(m)) : (o(!1), l(p(e))), (b = w.current) == null || b.blur(); }, 1e3); return () => clearTimeout(h); }, [ c, i, f, t, e, s ]); const A = () => { var m, b; const r = parseInt(K(c), 10); if (isNaN(r)) { l(p(e)), o(!1), u(null), (m = w.current) == null || m.blur(); return; } const h = Math.min(Math.max(r, 1), t); h !== e ? (u(h), l(p(h)), s(h)) : (l(p(e)), o(!1), u(null)), (b = w.current) == null || b.blur(); }; return t === 0 || !n ? null : /* @__PURE__ */ B( I, { justifyContent: "space-around", backgroundColor: "white", py: "10px", px: "16px", rounded: "lg", border: "1px", borderColor: "blue.200", w: "100%", children: [ /* @__PURE__ */ B(I, { spacing: 2, whiteSpace: "nowrap", children: [ /* @__PURE__ */ a(S, { fontWeight: "bold", children: "صفحه" }), /* @__PURE__ */ a(S, { children: p(e) }), /* @__PURE__ */ a(S, { fontWeight: "bold", children: "از" }), /* @__PURE__ */ a(S, { children: p(t) }) ] }), /* @__PURE__ */ B(I, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ /* @__PURE__ */ a( D, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, onClick: () => s(e - 1), isDisabled: e === 1, icon: /* @__PURE__ */ a(g, {}), "aria-label": "صفحه قبلی" } ), y.map((r) => /* @__PURE__ */ a( L, { size: "md", bg: e === r ? "#4B49AC" : "#e6e6f1", color: e === r ? "#e6e6f1" : "#4B49AC", _hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" }, onClick: () => s(r), variant: "soft", children: p(r) }, r )), /* @__PURE__ */ a( D, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, onClick: () => s(e + 1), isDisabled: e === t, icon: /* @__PURE__ */ a(O, {}), "aria-label": "صفحه بعدی" } ), /* @__PURE__ */ a( U, { ref: w, type: "text", inputMode: "numeric", placeholder: "صفحه", w: "100px", value: c, onFocus: () => { o(!0), u(null), l(""); }, onChange: (r) => { const h = r.target.value, m = K(h).replace(/\D/g, ""); l(p(m)); }, onBlur: () => { c || (l(p(e)), o(!1), u(null)); }, onKeyDown: (r) => { r.key === "Enter" && A(); }, textAlign: "center", color: "#1D2939", px: 1, minW: 0, dir: "ltr" } ) ] }), /* @__PURE__ */ B(I, { spacing: 2, whiteSpace: "nowrap", children: [ /* @__PURE__ */ a(S, { fontWeight: "bold", children: "تعداد کل:" }), /* @__PURE__ */ a(S, { children: p(n) }) ] }) ] } ); }, Ne = ({ currentPage: e, totalPages: t, totalCount: n, onPageChange: s }) => { const [c, l] = v(""), [i, o] = v(!1), [f, u] = v(null), w = N(null), y = f ?? e, A = E( (x) => { const k = Math.min(Math.max(x, 1), t); o(!1), l(""), u(null), k !== e && s(k); }, [e, t, s] ), r = E(() => { if (!c) { o(!1), l(""), u(null); return; } const x = parseInt(K(c), 10); if (isNaN(x)) { o(!1), l(""), u(null); return; } const k = Math.min(Math.max(x, 1), t); o(!1), l(""), k !== e ? (u(k), s(k)) : u(null); }, [c, e, t, s]); C(() => { var x; i && ((x = w.current) == null || x.focus()); }, [i]), C(() => { f !== null && e === f && u(null); }, [e, f]), C(() => { if (!i || !c) return; const x = setTimeout(() => { r(); }, 1e3); return () => clearTimeout(x); }, [c, i, r]); const h = () => { u(null), l(""), o(!0); }, m = (x, k = !1) => /* @__PURE__ */ a( L, { size: "md", bg: k ? "#4B49AC" : "#e6e6f1", color: k ? "#e6e6f1" : "#4B49AC", _hover: { bg: k ? "#4B49AC" : "#cbcbe8" }, onClick: k ? h : () => A(x), variant: "soft", children: p(x) }, x ); if (t === 0 || !n) return null; const b = t > 1 && y !== 1, F = t > 1 && y !== t; return /* @__PURE__ */ a( I, { justifyContent: "center", backgroundColor: "white", py: "10px", px: "16px", rounded: "lg", border: "1px", borderColor: "blue.200", w: "100%", children: /* @__PURE__ */ B(I, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ /* @__PURE__ */ a( D, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, onClick: () => A(y - 1), isDisabled: y === 1, icon: /* @__PURE__ */ a(g, {}), "aria-label": "صفحه قبلی" } ), b && m(1), i ? /* @__PURE__ */ a( U, { ref: w, type: "text", inputMode: "numeric", placeholder: "صفحه", w: "48px", value: c, onChange: (x) => { const k = x.target.value, X = K(k).replace(/\D/g, ""); l(p(X)); }, onBlur: () => { c ? r() : (l(""), o(!1), u(null)); }, onKeyDown: (x) => { x.key === "Enter" && r(), x.key === "Escape" && (l(""), o(!1), u(null)); }, textAlign: "center", color: "#1D2939", px: 1, minW: 0, dir: "ltr" } ) : m(y, !0), F && m(t), /* @__PURE__ */ a( D, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, onClick: () => A(y + 1), isDisabled: y === t, icon: /* @__PURE__ */ a(O, {}), "aria-label": "صفحه بعدی" } ) ] }) } ); }; let R = null, d = null, V = null, j = !1; const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (d || (d = new te({ url: e, realm: t, clientId: n })), d), se = (e) => (R = e, R), le = () => j, G = () => !!(d != null && d.authenticated), T = () => (d == null ? void 0 : d.token) ?? null, q = async (e = 30) => d ? d.updateToken(e) : !1, H = (e) => d == null ? void 0 : d.logout(e), Te = (e) => d == null ? void 0 : d.login(e), J = ({ config: e, initOptions: t = {} } = {}) => { const n = e ?? R; if (!(n != null && n.url) || !(n != null && n.realm) || !(n != null && n.clientId)) throw new Error("Keycloak config is required."); se(n); const s = oe(n); return V || (V = s.init({ onLoad: "login-required", checkLoginIframe: !1, flow: "implicit", ...t }).then((c) => (j = !0, c)).catch((c) => { throw j = !0, c; })), V; }, ce = ({ children: e, config: t, initOptions: n, loading: s = null }) => { const [c, l] = v(!1), [i, o] = v(!1), [f, u] = v(null), w = N(!1), y = E(() => { d && (o(!!d.authenticated), u(d.token ?? null)); }, []); C(() => (w.current = !0, J({ config: t, initOptions: n }).then((b) => { w.current && (o(!!b), u((d == null ? void 0 : d.token) ?? null), l(!0)); }).catch(() => { w.current && (o(!1), u(null), l(!0)); }), () => { w.current = !1; }), [t, n]); const A = E(() => d == null ? void 0 : d.login(), []), r = E((b) => d == null ? void 0 : d.logout(b), []), h = E( async (b = 30) => { if (!d) return !1; const F = await d.updateToken(b); return y(), F; }, [y] ), m = z( () => ({ initialized: c, authenticated: i, token: f, login: A, logout: r, updateToken: h }), [c, i, f, A, r, h] ); return c ? /* @__PURE__ */ a(ie.Provider, { value: m, children: e }) : s; }, Q = $(null), M = "permissions", ue = (e) => (e == null ? void 0 : e.storageKey) || M, ze = ({ user: e = null, children: t, config: n, loading: s = /* @__PURE__ */ a("div", { children: "Loading permissions..." }), errorFallback: c = /* @__PURE__ */ a("div", { children: "Error loading permissions" }) }) => { const l = ue(n), [i, o] = v( () => ae(l) ), { data: f, isLoading: u, error: w } = me(n), y = f ?? i; C(() => { f && (de(l, f), o(f)); }, [f, l]); const A = z( () => fe(y), [y] ), r = E( (b = "") => pe(A, b), [A] ), h = E((b = "") => !r(b), [r]), m = z( () => ({ user: e, can: r, cannot: h, permissions: y, isLoading: u, error: w }), [e, r, h, y, u, w] ); return w && !y ? c : u && !y ? s : /* @__PURE__ */ a(Q.Provider, { value: m, children: t }); }, ae = (e = M) => { if (!(typeof window > "u")) try { const t = localStorage.getItem(e); return t ? JSON.parse(t) : void 0; } catch { return; } }, de = (e = M, t) => { if (!(typeof window > "u")) try { localStorage.setItem(e, JSON.stringify(t)); } catch { } }, _e = (e = M) => { if (!(typeof window > "u")) try { localStorage.removeItem(e); } catch { } }, fe = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), pe = (e, t = "") => { if (!t) return !1; if (e.has(t) || [...e].some((s) => s.startsWith(t))) return !0; let n = t; for (; n.includes(":"); ) if (n = n.slice(0, n.lastIndexOf(":")), e.has(`${n}:*`)) return !0; return !1; }, Y = { all: ["permissions"], list: (e) => [...Y.all, e] }, he = async (e) => { if (!(e != null && e.url) || !(e != null && e.clientId)) throw new Error("Permission config requires url and clientId."); const t = T(), n = new URL(e.url); n.searchParams.set("client_id", e.clientId); const s = await fetch(n.toString(), { method: "GET", headers: { ...t ? { Authorization: `Bearer ${t}` } : {} } }); if (!s.ok) throw new Error("Failed to load permissions"); return s.json(); }, W = () => { const e = P(Q); if (!e) throw new Error("usePermission must be used within PermissionProvider"); return e; }, me = (e) => ne({ queryKey: Y.list(e == null ? void 0 : e.clientId), queryFn: () => he(e), enabled: !!(e != null && e.url) && !!(e != null && e.clientId) && G(), staleTime: 1 / 0, refetchOnWindowFocus: !1, refetchOnReconnect: !1 }), Me = ({ perm: e, children: t, fallback: n = /* @__PURE__ */ a("div", { children: "You are not allowed to do this" }) }) => { const { can: s } = W(); return s(e) ? t : n; }, Fe = ({ children: e }) => { const { can: t, permissions: n } = W(); return n && t("admin") ? e : null; }, Ve = ({ children: e }) => { const { can: t, isLoading: n } = W(); return C(() => { n || t("panel_access") || H(); }, [n, t]), e; }; let _ = null; const be = (e) => { const { api: t } = e, n = re.create({ baseURL: t.baseUrl }), s = async () => { (!le() || !G()) && await J(); }, c = async (i) => { await s(); let o = T(); if (o) { try { await q(5), o = T(); } catch { } i.headers = { ...i.headers, Authorization: `Bearer ${o}` }; } return i; }, l = async (i) => { var f; const o = i.config; if (((f = i == null ? void 0 : i.response) == null ? void 0 : f.status) === 401 && !(o != null && o._retry)) { o._retry = !0; try { await q(30); const u = T(); return o.headers = { ...o.headers, Authorization: `Bearer ${u}` }, n(o); } catch { H({ redirectUri: window.location.origin }); } } return Promise.reject(i); }; return n.interceptors.request.use(c), n.interceptors.response.use( (i) => i, l ), { api: n }; }, we = (e) => (_ = be(e), _), ye = () => { if (!_) throw new Error("api is not configured. Call configureApi(config) first."); return _.api; }, Re = new Proxy( {}, { get(e, t) { return ye()[t]; } } ); function xe() { const [e, t] = v(!1), n = N(null); return C(() => { if (process.env.NODE_ENV !== "production") return; let s; const c = async () => { const i = await fetch(`/version.json?t=${Date.now()}`, { cache: "no-store" }); if (!i.ok) throw new Error(`Failed to fetch version: ${i.status}`); const o = await i.json(); return String(o.version); }; return (async () => { try { n.current = await c(), s = setInterval(async () => { try { const i = await c(); n.current && i !== n.current && (t(!0), clearInterval(s)); } catch (i) { console.error("Update check failed:", i); } }, 3e4); } catch (i) { console.error("Initial version fetch failed:", i); } })(), () => { s && clearInterval(s); }; }, []), e ? /* @__PURE__ */ a( "div", { style: { position: "fixed", bottom: "16px", right: "16px", background: "#111", color: "#fff", padding: "12px 16px", borderRadius: "8px", zIndex: 9999, cursor: "pointer" }, onClick: () => window.location.reload(), children: "نسخه جدید سامانه در دسترس است" } ) : null; } function je({ children: e, config: t, loading: n = /* @__PURE__ */ a(ee, {}), updateChecker: s = !0, updateCheckerProps: c }) { return z(() => { we(t); }, [t]), /* @__PURE__ */ B(Z, { children: [ s && /* @__PURE__ */ a(xe, { ...c }), /* @__PURE__ */ a(ce, { config: t.keycloak, loading: n, children: e }) ] }); } export { Fe as AdminGuard, Me as Can, ce as KeycloakProvider, Ke as LightPagination, De as Pagination, Ve as PanelAccessGuard, ze as PermissionProvider, Ne as SimplePagination, je as UiKitProvider, xe as UpdateChecker, Re as api, pe as checkPermission, _e as clearCachedPermissions, we as configureApi, be as createApi, fe as createPermissionSet, ye as getApi, ae as getCachedPermissions, T as getKeycloakToken, he as getPermissions, J as initKeycloak, G as isKeycloakAuthenticated, le as isKeycloakInitialized, Te as loginKeycloak, H as logoutKeycloak, Y as permissionKeys, de as setCachedPermissions, K as toEnDigits, p as toFaDigits, Ee as toFaNumber, q as updateKeycloakToken, W as usePermission, me as usePermissionsQuery };