"use strict";(self.webpackChunkscreenity=self.webpackChunkscreenity||[]).push([[266],{2266(e,t,s){s.d(t,{resumeAllJournals:()=>m});var r=s(3790),a=s.n(r),n=s(3068),o=s(3812);a().config({driver:a().INDEXEDDB,name:"screenity",version:1});const i="uploadJournal-",c=e=>"camera"===e?"camera":"screen",d=async e=>{const t=`${i}${e.mediaId}`;try{const s=await chrome.storage.local.get([t]),r=s?.[t]||e,a={...r,resumeAttempts:(r.resumeAttempts||0)+1,lastResumeAttemptAt:Date.now()};await chrome.storage.local.set({[t]:a})}catch{}},u=(e,t)=>{try{chrome.runtime.sendMessage({type:"offscreen-diag",source:e,payload:t}).catch(()=>{})}catch{}},l=async e=>{const t=e.trackType||e.type||"screen",s=e.mediaId,r=e.projectId,a=e.sceneId;if(!s||!r)return{ok:!1,error:"journal-missing-ids"};u("upload_resume_started",{mediaId:s,projectId:r,sceneId:a,trackType:t,offset:e.offset,totalBytes:e.totalBytes,resumeAttempts:e.resumeAttempts||0});const{backend:l,opfsSessionId:m}=await(async e=>{const t=e?.sessionId||null,s=c(e?.trackType||"screen");if(t){const e=`cloudRecorderSession:${t}`;try{const r=await chrome.storage.local.get([e]),a=r?.[e];if(a?.storageBackends)return{backend:a.storageBackends[s]||"idb",opfsSessionId:a.opfsSessionId||t}}catch{}}try{const e=await chrome.storage.local.get(["recorderSession"]),r=e?.recorderSession;if(r?.storageBackends&&(r.id===t||!t))return{backend:r.storageBackends[s]||"idb",opfsSessionId:r.opfsSessionId||t}}catch{}return{backend:"idb",opfsSessionId:t}})(e),{store:p}=(0,o.HO)({sessionId:m,track:c(t),backend:l}),h=await(async(e,t="chunk_")=>{const s=[];return await e.iterate((e,r)=>{r&&r.startsWith(t)&&e&&e.chunk&&s.push({index:"number"==typeof e.index?e.index:parseInt(r.replace(t,""),10)||0,chunk:e.chunk,timestamp:e.timestamp||0})}),s.sort((e,t)=>e.index-t.index),s})(p,(e=>"camera"===e?"camera_chunk_":"chunk_")(t));if(!h.length){u("upload_resume_chunks_missing",{mediaId:s,trackType:t});try{await chrome.storage.local.remove([`${i}${s}`])}catch{}return{ok:!1,error:"chunks-missing"}}const f=new n.A({trackType:t});try{await f.initialize(r,{title:e.title||`Recording ${s}`,type:t,width:e.width||null,height:e.height||null,sceneId:a||null,sessionId:e.sessionId||null,reuse:{videoId:e.videoId,mediaId:s}})}catch(t){return console.warn("[resumeJournal] initialize failed:",t),await d(e),u("upload_resume_failed",{mediaId:s,phase:"initialize",error:t?.message||String(t)}),{ok:!1,error:`initialize: ${t?.message||t}`}}const k=f.offset||0,y=e.totalBytes||0;let I=[];if(y>0&&k>=y)u("upload_resume_finalize_only",{mediaId:s,trackType:t,serverOffset:k,journalTotalBytes:y});else{if(!h.every((e,t)=>e.index===t)){u("upload_resume_sparse_chunks",{mediaId:s,trackType:t,firstIndex:h[0]?.index,lastIndex:h[h.length-1]?.index,count:h.length,serverOffset:k,journalTotalBytes:y});try{await chrome.storage.local.remove([`${i}${s}`])}catch{}return{ok:!1,error:"sparse-chunks"}}I=((e,t)=>{if(!t||t<=0)return e;let s=0;for(let r=0;r<e.length;r++){const a=s+(e[r].chunk?.size||0);if(a<=t){s=a;continue}const n=t-s,o=e.slice(r);return n>0&&"function"==typeof o[0]?.chunk?.slice&&(o[0]={...o[0],chunk:o[0].chunk.slice(n)}),o}return[]})(h,k),u("upload_resume_chunks_skipped",{mediaId:s,serverOffset:k,totalChunks:h.length,remainingChunks:I.length})}try{for(const{chunk:e}of I){if("aborted"===f.status||"error"===f.status)break;await f.write(e)}return await f.finalize(),u("upload_resume_completed",{mediaId:s,projectId:r,sceneId:a,trackType:t,finalizedBytes:f.totalBytes}),{ok:!0}}catch(t){return console.warn("[resumeJournal] upload/finalize failed:",t),await d(e),u("upload_resume_failed",{mediaId:s,phase:"upload",error:t?.message||String(t),resumeAttempts:(e.resumeAttempts||0)+1}),{ok:!1,error:`upload: ${t?.message||t}`}}},m=async e=>{const t=[];for(const s of e)try{const e=await l(s);t.push({mediaId:s.mediaId,...e})}catch(e){t.push({mediaId:s.mediaId,ok:!1,error:e?.message||String(e)})}return t}}}]);