import{a as $,c as z}from"../../chunk-OULDMZYN.js";import{a as M}from"../../chunk-DBT6Y6WH.js";import{a as R}from"../../chunk-LF3PYEFR.js";import{a as T}from"../../chunk-UK26QCDM.js";import{a as ne}from"../../chunk-UCSK5ACD.js";import{b as j,c as U}from"../../chunk-PFPOVTY6.js";import{a as H}from"../../chunk-UKGLT346.js";import"../../chunk-EY555SK2.js";import{a as _}from"../../chunk-XH2OJDAW.js";import{b as V}from"../../chunk-KC6VWGIB.js";import"../../chunk-3XIZD54N.js";import{c as N,e as m,f as S,h as O}from"../../chunk-IRFDE6LX.js";import{b as oe}from"../../chunk-FW4363Y4.js";var x=oe(ne(),1);import{lazy as me,Suspense as pe,useEffect as q,useRef as fe,useState as p}from"react";var L=class{constructor(e){this.onmessage=null;this.onmessageerror=null;this.onerror=null;this.#e=e}#e;postMessage(e,s){Array.isArray(s)?this.#e.postMessage(e,s):this.#e.postMessage(e)}addEventListener(e,s,t){this.#e.addEventListener(e,s,t)}removeEventListener(e,s,t){this.#e.removeEventListener(e,s,t)}dispatchEvent(e){return this.#e.dispatchEvent(e)}terminate(){this.#e.close()}},F=()=>{let{port1:r,port2:e}=new MessageChannel;return r.start(),e.start(),{worker:new L(r),port:e}};var B=async(r,e,s)=>{if(process.env.NODE_ENV==="development"){let{getDevPhysicsWorker:t}=await import("../../getDevPhysicsWorker-Z7QCL7OR.js");return t(r,e,s)}return new Worker(`/dice/${S(e)?"mobile":"previewer"}/physicsWorker.V7R6GDDZ.js`,{name:R("physics",r,e,s),type:"module"})};var K=async(r,e,s)=>{if(process.env.NODE_ENV==="development"){let{getDevRenderedWorker:t}=await import("../../getDevRenderedWorker-YZ4KKJNF.js");return t(r,e,s)}return new Worker(`/dice/${S(e)?"mobile":"previewer"}/renderedWorker.QCFTNQ5Y.js`,{name:R("rendered",r,e,s),type:"module"})};import{useEffect as ie,useRef as ae}from"react";var X=r=>{let e=ae(!1);ie(()=>{if(m||!r||e.current)return;let s=()=>{(async()=>e.current||!await $()||(e.current=!0,i()))()},t=["pointerdown","touchstart","mousedown","keydown"],i=()=>{t.forEach(o=>{window.removeEventListener(o,s,!0)})};return t.forEach(o=>{window.addEventListener(o,s,{capture:!0,passive:!0})}),i},[r])};import{useEffect as de,useState as ce}from"react";var Y=(r,e,s,t)=>{let[i,o]=ce(),d=U(n=>n.customUrlMap),k=U(n=>n.selectedSet),h=U(n=>n.additionalSetIds),f=O(String(r),String(e)),u=M(r,e),W=V(n=>n.getUserDiceData(Number(s?.id))?.setId);return de(()=>{if(m||!(r&&e&&u))return;let n=t?k?.id:W,c=[];if(n&&c.push(String(n)),t)for(let a of h)a!==n&&!c.includes(a)&&c.push(a);c.length!==0&&(async()=>{let a={};await Promise.all(c.map(async(l,P)=>{let y=P===0,v=y&&d?.[j]||u.find(({id:D})=>D===l)?.manifestUrl;if(!v){console.error(`[DiceCanvases] Manifest URL for set ID "${l}" not found.`);return}let g=await T({mediaUrl:f,setId:l,manifestUrl:v,...y&&d?{customUrlMap:d}:{}});g&&(a[l]=g,z(f,[g],y?d:void 0))})),Object.keys(a).length!==0&&o(a)})()},[h,d,u,e,f,r,t,k?.id,W]),i};import{jsx as G,jsxs as ye}from"react/jsx-runtime";var ue=me(async()=>import("../../DiceCanvases-PW7MVQ5U.js").then(r=>({default:r.DiceCanvases}))),le=({children:r,logger:e,errorCallback:s,nodeEnv:t,env:i,user:o,gameId:d,toastCallback:k,preview:h})=>{let f=fe(!1),[u,W]=p(),[n,c]=p(!1),[a,l]=p(!1),[P,y]=p(),[v,g]=p(),[D,J]=p(!1),[Q,Z]=p(!1),ee=n||D,re=a&&Q;H(t,i,Number(o?.id)),X(o);let b=Y(t,i,o,h),C=M(t,i);return q(()=>{if(m||!o||(0,x.isEmpty)(b)||(0,x.isEmpty)(C)||f.current)return;let A=!0;return(async()=>{let E,w;if(N){let{worker:te,port:se}=F();E=te,w=se}else E=await K(t,i,o);let I=await B(t,i,o);if(!A){I.terminate(),E.terminate(),w?.close();return}W(I),y(E),w&&g(w),f.current=!0})(),()=>{A=!1}},[i,b,t,o,C]),q(()=>()=>{u?.terminate(),P?.terminate()},[]),o?ye(_.Provider,{value:{renderedWorker:P,renderedWorkerPort:v,physicsWorker:u,nodeEnv:t,env:i,user:o,hasPddErrored:ee,isPddReady:re,userManifest:b,setHasPhysicsWorkerErrored:c,setIsPhysicsWorkerReady:l,gameId:d,logger:e,errorCallback:s,toastCallback:k,setHasRenderedWorkerErrored:J,setIsRenderedWorkerReady:Z,diceSets:C},children:[r,!m&&G(pe,{children:G(ue,{})})]}):r};export{le as PddContextProvider};
//# sourceMappingURL=index.js.map
