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