(globalThis.TURBOPACK||(globalThis.TURBOPACK=[])).push(["object"==typeof document?document.currentScript:void 0,1845,8350,e=>{"use strict";var t=e.i(43476),r=e.i(71645),a=e.i(63398),o=e.i(22677),i=e.i(87922),n=e.i(83656),s=e.i(52388);function l(e,t){let r=new Date(`${e}T12:00:00Z`);return r.setUTCDate(r.getUTCDate()+t),r.toISOString().slice(0,10)}function d(e,t){let r,a=(0,n.toLocalDateString)();if("again"===t)return{...e,repetitions:0,intervalDays:1,nextReviewAt:l(a,1)};let o=e.repetitions+1;r=1===o?1:2===o?6:Math.round(e.intervalDays*e.easeFactor);let i="easy"===t?Math.min(e.easeFactor+.15,3):e.easeFactor;return"easy"===t&&(r=Math.round(1.3*r)),{...e,repetitions:o,intervalDays:r,easeFactor:i,nextReviewAt:l(a,r)}}e.s(["getDueRefs",0,function(e,t){return Object.entries(e).filter(([,e])=>e.nextReviewAt<=t).sort((e,t)=>e[1].nextReviewAt.localeCompare(t[1].nextReviewAt)).map(([e])=>e)},"scheduleNext",0,d],8350);var c=e.i(88922),u=e.i(51264),h=e.i(58579);let p={chapters:{},history:{},doneChapters:{},doneBooks:{},memorized:{},review:{}},m=(0,r.createContext)({data:p,markVerseComplete:()=>{},completedVersesForChapter:()=>({}),markChapterDone:()=>{},recordReview:()=>{},isLoaded:!1});e.s(["WritingDataProvider",0,function({children:e}){let{user:l,sessionReady:g}=(0,a.useAuth)(),w=l?.email||"",{show:v}=(0,c.useToast)(),f=(0,u.useUiLang)(),y=(0,h.getUiText)(f),[C,k]=(0,r.useState)({email:"",data:p,loaded:!1});C.email!==w&&k({email:w,data:p,loaded:!1});let R=C.email===w,S=R?C.data:p,T=R&&C.loaded,b=(0,r.useCallback)(e=>{k(t=>t.email===w?{...t,data:e(t.data)}:t)},[w]),D=(0,r.useRef)(0),x=(0,r.useRef)(Promise.resolve());(0,r.useEffect)(()=>{D.current+=1},[w]);let A=(0,r.useCallback)(()=>{if(!w||!g)return Promise.resolve();let e=++D.current;return(0,i.fetchWithRetry)(`https://jwhhboergk.execute-api.ap-northeast-2.amazonaws.com/Prod/loadWriting?userEmail=${encodeURIComponent(w)}`,{method:"GET",headers:{Authorization:`Bearer ${(0,o.getSessionToken)()}`}}).then(e=>{if(!e.ok)throw Error(`HTTP ${e.status}`);return e.json()}).then(t=>{D.current===e&&k({email:w,loaded:!0,data:{chapters:t.chapters||{},history:t.history||{},doneChapters:t.doneChapters||{},doneBooks:t.doneBooks||{},typingStats:t.typingStats||void 0,memorized:t.memorized||{},review:t.review||{}}})}).catch(t=>{console.error("Failed to load writing data:",t),D.current===e&&v(y.writingLoadFailed,{type:"error"})})},[w,g,v,y]),z=(0,r.useCallback)(async e=>{if(!w)return;let t=async()=>{try{let t=await fetch("https://jwhhboergk.execute-api.ap-northeast-2.amazonaws.com/Prod/saveWriting",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${(0,o.getSessionToken)()}`},body:JSON.stringify({userEmail:w,data:e})});t.ok||console.error(`Failed to save writing data: HTTP ${t.status}`)}catch(e){console.error("Failed to save writing data:",e)}};x.current=x.current.then(t,t),await x.current},[w]);(0,r.useEffect)(()=>{A()},[A]);let P=(0,r.useCallback)((e,t,r,a)=>{if(!T)return void console.warn("markVerseComplete ignored: writing data not loaded yet");let o=(0,n.toLocalDateString)();b(i=>{let n=String(t),l=String(r),d=(0,s.makeNoteRef)(e,t,r),c=!!i.memorized?.[d],u={...i,chapters:{...i.chapters,[e]:{...i.chapters[e]||{},[n]:{...i.chapters[e]?.[n]||{},[l]:!0}}},history:{...i.history,[o]:(i.history[o]||0)+1},...a?.memorize&&!c?{memorized:{...i.memorized||{},[d]:o},review:{...i.review||{},[d]:{nextReviewAt:o,intervalDays:1,easeFactor:2.5,repetitions:0}}}:{}};return z(u),u})},[z,T,b]),$=(0,r.useCallback)((e,t)=>S.chapters[e]?.[String(t)]||{},[S]),B=(0,r.useCallback)((e,t,r,a)=>{T?b(o=>{let i=o.doneChapters[e]||[];if(i.includes(t))return o;let n=[...i,t].sort((e,t)=>e-t),s=n.length===r,l={...o,doneChapters:{...o.doneChapters,[e]:n},doneBooks:{...o.doneBooks,...s?{[e]:!0}:{}},...a&&a.keystrokes>0?{typingStats:{...o.typingStats,[e]:{...o.typingStats?.[e]||{},[String(t)]:a}}}:{}};return z(l),l}):console.warn("markChapterDone ignored: writing data not loaded yet")},[z,T,b]),F=(0,r.useCallback)((e,t)=>{T?b(r=>{let a=r.review?.[e];if(!a)return r;let o=(0,n.toLocalDateString)(),i={...d(a,t),lastReviewedAt:o},s={...r,review:{...r.review||{},[e]:i}};return z(s),s}):console.warn("recordReview ignored: writing data not loaded yet")},[z,T,b]);return(0,t.jsx)(m.Provider,{value:{data:S,markVerseComplete:P,completedVersesForChapter:$,markChapterDone:B,recordReview:F,isLoaded:T},children:e})},"useWritingData",0,function(){return(0,r.useContext)(m)}],1845)},52388,e=>{"use strict";e.s(["isChapterNote",0,function(e){return e.endsWith("#0")},"makeChapterNoteRef",0,function(e,t){return`${e}#${t}#0`},"makeNoteRef",0,function(e,t,r){return`${e}#${t}#${r}`},"parseNoteRef",0,function(e){let t=e.split("#");return{book:t[0],chapter:parseInt(t[1],10),verse:parseInt(t[2],10)}}])}]);