"use client"; import W, { useState as G, useRef as L, useEffect as D, useCallback as z } from "react"; import { d as X } from "./chunks/feature-registry-B2iuEiEx.js"; import { u as Y } from "./chunks/provider-runtime-CxEpkUq5.js"; const Z = [87, 77], $ = 24, J = 20, A = 10, Q = $ * A, tt = J * A, F = 160, et = 3, I = { cool: { r: 0, g: 195, b: 255 }, warm: { r: 250, g: 130, b: 0 } }; function N(r) { if (typeof r != "string") throw new TypeError("User id must be a UUID string"); const t = r.toLowerCase(); if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(t)) throw new TypeError("User id must be a canonical 36-character UUID"); return t; } function rt(r) { let t = 65535; for (const s of r) { t ^= s << 8; for (let e = 0; e < 8; e++) t = t & 32768 ? (t << 1 ^ 4129) & 65535 : t << 1 & 65535; } return t; } function B(r) { const s = N(r).replaceAll("-", ""), e = new Uint8Array(20); e.set(Z, 0); for (let c = 0; c < 16; c++) e[c + 2] = Number.parseInt(s.slice(c * 2, c * 2 + 2), 16); const o = rt(e.subarray(0, 18)); e[18] = o >> 8, e[19] = o & 255; const n = []; for (const c of e) for (let a = 7; a >= 0; a--) n.push(c >> a & 1); return n; } function nt(r) { let t = Math.imul(r + 1, 2654435761) + 2135587861 >>> 0; return t ^= t >>> 16, t = Math.imul(t, 2246822507) >>> 0, t ^= t >>> 13, t >>> 0; } function ot(r) { const t = nt(r), s = { x: 1 + (t & 1), y: 2 + (t >> 1 & 1), w: 3, h: 3 }, e = { x: 5 + (t >> 2 & 1), y: 2 + (t >> 3 & 1), w: 3, h: 3 }; return { left: s, right: e }; } const M = 20, q = 17, R = 12, st = M * R, ct = q * R, K = M * q, at = 97, it = [1, 0, 1, 1, 0, 1, 0, 0], j = (r) => { let t = r; return t ^= t >>> 4, t ^= t >>> 2, t ^= t >>> 1, t & 1; }; function ut(r) { const t = [...B(r), ...Array(6).fill(0)], s = []; let e = 0; for (const o of t) { const n = e << 1 | o; s.push(j(n & 121), j(n & 91)), e = n & 63; } return [...s, ...it]; } const lt = (r) => r * at % K; function ft(r) { const t = r % M, s = Math.floor(r / M), e = Math.imul(r + 1, 2654435761) >>> 28 & 3, o = s * R + 2 + e % 2, n = e >> 1 & 1; return { left: { x: t * R + 1 + n, y: o, w: 4, h: 6 }, right: { x: t * R + 7 + n, y: o, w: 4, h: 6 } }; } function dt(r, { opacity: t = 0.03 } = {}) { if (!(t > 0 && t <= 0.08)) throw new RangeError("opacity must be between 0 and 0.08"); const s = ut(r), e = document.createElement("canvas"); e.width = st, e.height = ct; const o = e.getContext("2d", { alpha: !0 }); if (!o) throw new Error("2D canvas is unavailable"); const n = `rgba(255,255,255,${t})`, c = `rgba(0,0,0,${t})`; for (let a = 0; a < K; a++) { const { left: f, right: d } = ft(lt(a)); o.fillStyle = s[a] ? n : c, o.fillRect(f.x, f.y, f.w, f.h), o.fillStyle = s[a] ? c : n, o.fillRect(d.x, d.y, d.w, d.h); } return e; } function ht(r, { opacity: t, version: s = 3 } = {}) { if (s === 3) return dt(r, { opacity: t ?? 0.03 }); if (s !== 2) throw new RangeError("Unsupported watermark version"); if (t ?? (t = 0.014), !(t > 0 && t <= 0.08)) throw new RangeError("opacity must be between 0 and 0.08"); const e = B(r), o = document.createElement("canvas"); o.width = Q, o.height = tt; const n = o.getContext("2d", { alpha: !0 }); if (!n) throw new Error("2D canvas is unavailable"); const c = `rgba(${I.cool.r},${I.cool.g},${I.cool.b},${t})`, a = `rgba(${I.warm.r},${I.warm.g},${I.warm.b},${t})`; for (let f = 0; f < F * et; f++) { const d = e[f % F], g = f % $ * A, i = Math.floor(f / $) * A, { left: w, right: h } = ot(f); n.fillStyle = d ? a : c, n.fillRect(g + w.x, i + w.y, w.w, w.h), n.fillStyle = d ? c : a, n.fillRect(g + h.x, i + h.y, h.w, h.h); } return o; } function wt(r, t, s = 1) { const e = Math.max(1, window.innerWidth), o = Math.max(1, window.innerHeight), n = Math.max(1, s); r.width = Math.round(e * n), r.height = Math.round(o * n), r.style.width = `${e}px`, r.style.height = `${o}px`; const c = r.getContext("2d", { alpha: !0 }); if (!c) return !1; c.setTransform(n, 0, 0, n, 0, 0), c.imageSmoothingEnabled = !1; const a = c.createPattern(t, "repeat"); return a ? (c.clearRect(0, 0, e, o), c.fillStyle = a, c.fillRect(0, 0, e, o), !0) : !1; } const mt = "https://api.d.aiengines.ir/user_api/v1/users/my_user"; function gt({ getAccessToken: r, enabled: t = !0, userUrl: s = mt, opacity: e, version: o = 3, sessionKey: n, onError: c, onReady: a, onStatusChange: f }) { const [d, g] = G({ sessionKey: n, userId: null }), i = L({ onError: c, onReady: a, onStatusChange: f, getAccessToken: r }); i.current = { onError: c, onReady: a, onStatusChange: f, getAccessToken: r }, D(() => { var p, x, k, v; let h = !1; const u = new AbortController(); return g({ sessionKey: n, userId: null }), (x = (p = i.current).onStatusChange) == null || x.call(p, "loading"), t ? ((async () => { var S, b, y, m; try { if (typeof i.current.getAccessToken != "function") throw new Error("Watermark requires getAccessToken or an authenticated Keycloak token"); const l = await i.current.getAccessToken(); if (h) return; if (typeof l != "string" || !l.trim()) throw new Error("Watermark access token is unavailable"); const E = await fetch(s, { method: "GET", headers: { Authorization: `Bearer ${l}`, Accept: "application/json" }, cache: "no-store", credentials: "omit", signal: u.signal }); if (!E.ok) throw new Error(`Watermark identity HTTP ${E.status}`); const T = await E.json(), _ = N(T == null ? void 0 : T.id); h || g({ sessionKey: n, userId: _ }); } catch (l) { if (h || (l == null ? void 0 : l.name) === "AbortError") return; g({ sessionKey: n, userId: null }), (b = (S = i.current).onStatusChange) == null || b.call(S, "error"), (m = (y = i.current).onError) == null || m.call(y, l); } })(), () => { h = !0, u.abort(); }) : ((v = (k = i.current).onStatusChange) == null || v.call(k, "disabled"), () => u.abort()); }, [t, s, n]); const w = d.sessionKey === n ? d.userId : null; return D(() => { var x, k, v, S, b, y, m, l, E, T, _, U, O, H, V, P; if (!t || !w || typeof document > "u") return; let h; try { h = ht(w, { opacity: e, version: o }); } catch (C) { (k = (x = i.current).onStatusChange) == null || k.call(x, "error"), (S = (v = i.current).onError) == null || S.call(v, C); return; } if (document.querySelector("canvas[data-forensic-watermark]")) { const C = new Error("A forensic watermark canvas is already mounted"); (y = (b = i.current).onStatusChange) == null || y.call(b, "error"), (l = (m = i.current).onError) == null || l.call(m, C); return; } const u = document.createElement("canvas"); u.setAttribute("aria-hidden", "true"), u.setAttribute("data-forensic-watermark", ""), Object.assign(u.style, { position: "fixed", inset: "0", zIndex: "2147483647", pointerEvents: "none", userSelect: "none", width: "100vw", height: "100vh", display: "block" }); const p = () => wt(u, h, window.devicePixelRatio || 1); document.body.appendChild(u); try { if (p() !== !0) throw new Error("Could not paint watermark canvas"); (T = (E = i.current).onStatusChange) == null || T.call(E, "ready"), (U = (_ = i.current).onReady) == null || U.call(_); } catch (C) { u.remove(), (H = (O = i.current).onStatusChange) == null || H.call(O, "error"), (P = (V = i.current).onError) == null || P.call(V, C); return; } return window.addEventListener("resize", p, { passive: !0 }), () => { window.removeEventListener("resize", p), u.remove(); }; }, [t, w, e, o, n]), null; } async function pt(r, t) { var s; if (r != null && r.authenticated) { const e = r.instance; try { e != null && e.refreshToken && await ((s = r.updateToken) == null ? void 0 : s.call(r, 5)); } catch { } let o = !1; try { o = typeof (e == null ? void 0 : e.isTokenExpired) == "function" ? e.isTokenExpired(0) : !1; } catch { o = !0; } if (!o) { const n = (e == null ? void 0 : e.token) || r.token; if (typeof n == "string" && n.trim()) return n; } } return typeof t == "function" && await t() || null; } function Et({ children: r, enabled: t = !0, getAccessToken: s, getToken: e, sessionKey: o, blockUntilReady: n = !1, onReady: c, onError: a, ...f }) { var b, y; const d = Y(), g = L(d); g.current = d; const i = L(s ?? e); i.current = s ?? e; const w = L({ onReady: c, onError: a }); w.current = { onReady: c, onError: a }; const h = z( () => pt(g.current, i.current), [] ), u = ((y = (b = d == null ? void 0 : d.instance) == null ? void 0 : b.tokenParsed) == null ? void 0 : y.sub) ?? o, [p, x] = G({ sessionKey: u, status: "loading" }), k = p.sessionKey === u ? p.status : "loading", v = z((m) => { x({ sessionKey: u, status: m }); }, [u]), S = t && k === "ready"; return W.createElement( W.Fragment, null, W.createElement(gt, { ...f, enabled: t, sessionKey: u, getAccessToken: h, onStatusChange: v, onReady: () => { var m, l; return (l = (m = w.current).onReady) == null ? void 0 : l.call(m); }, onError: (m) => { var l, E; return (E = (l = w.current).onError) == null ? void 0 : E.call(l, m); } }), !n || !t || S ? r : null ); } const kt = X("Watermark", 65, Et); export { gt as ForensicWatermark, kt as Watermark };