import{i as y,z as s,K as h,aq as l,j as x}from"./index-BbBXKozi.js";import{u as I}from"./index-BZRwgrYr.js";const{createContext:A,useState:u,useEffect:T,useMemo:f}=await y("react"),b=s.object({storedAt:s.string().datetime(),playedTime:s.number(),lastRedemptionTime:s.number(),userId:s.string()}),E={length:0,start:()=>0,end:()=>0},g=A({playedTime:0,setPlayed:()=>null,lastRedemptionTime:0,setLastRedemptionTime:()=>null}),L=({children:V})=>{const[e,v]=u(E),[c,p]=u(0),[o,n]=u(0),{isAuthenticated:a,userId:d}=h(),m=f(()=>(e.length>0?Array.from({length:e.length}).reduce((t,w,i)=>(t+=e.end(i)-e.start(i),t),0):0)+c,[e,c]),r=I(()=>{a&&window.localStorage.setItem("persistedViewTime",JSON.stringify({storedAt:l.now().toUTC().toString(),playedTime:m,lastRedemptionTime:o,userId:d}))});T(()=>{const t=window.localStorage.getItem("persistedViewTime");if(!t||!a){n(0),p(0);return}try{const{storedAt:w,playedTime:i,lastRedemptionTime:R,userId:S}=b.parse(JSON.parse(t));l.fromISO(w).startOf("day").toMillis()===l.now().startOf("day").toMillis()&&d===S&&(p(i),n(R))}catch{}finally{window.localStorage.removeItem("persistedViewTime")}},[a,d]),T(()=>(window.addEventListener("beforeunload",r,!1),()=>{window.removeEventListener("beforeunload",r),r()}),[r]);const P=f(()=>({playedTime:m,setPlayed:v,lastRedemptionTime:o,setLastRedemptionTime:n}),[m,o]);return x.jsx(g.Provider,{value:P,children:V})},{useContext:O}=await y("react"),D=()=>O(g);export{L as V,D as u};
//# sourceMappingURL=useViewTime-0zU0PEMr.js.map