var U="2023-11-03",m=null,i=null,S=0,_=3,I=3,B=500,E=null,o=null,R=null,b="",y=!1,w=!1,g=!1,f=0,d=[],p={},u=0,c=0;function a(t,e,n){postMessage({type:"DEBUG",fileId:i,level:t,msg:e,payload:n})}function A(){let t=Math.min(99,Math.round(u/c*100));postMessage({type:"PROGRESS",fileId:i,percent:t})}function T(){postMessage({type:"PROGRESS",fileId:i,percent:100}),postMessage({type:"DONE",fileId:i})}function H(t){postMessage({type:"ERROR",fileId:i,error:t})}function M(t){return new Promise(e=>setTimeout(e,t))}function P(t=2e3){if(!o)return!0;try{let n=new URLSearchParams(o).get("se");return n?Date.parse(n)-Date.now()<t:!0}catch{return!0}}async function $(){if(!i)throw new Error("fileId missing");g=!1,o=null,postMessage({type:"REFRESH_SAS",fileId:i});let t=0;for(;!o&&!g;){if(w)throw new Error("Canceled during SAS refresh");if(await M(60),t+=60,t>12e4)throw new Error("SAS refresh timeout")}if(g)throw new Error("SAS refresh failed")}function D(t){return btoa(t.toString().padStart(10,"0"))}onmessage=t=>{let e=t.data;switch(e.type){case"START":y=!1,w=!1,g=!1,m=e.file,i=e.fileId,S=e.chunkSize,_=e.concurrency,I=e.maxChunkRetries??3,B=e.retryDelayMs??500,E=e.initialUrl.split("?")[0],o=e.initialUrl.split("?")[1],R=e.apiPreFix,b=e.token,v().catch(n=>H(String(n)));break;case"PAUSE":y=!0;break;case"RESUME":y=!1;break;case"CANCEL":w=!0;break;case"REFRESH_SAS_DONE":e.fileId===i&&(o=e.url.split("?")[1]??e.url);break;case"REFRESH_SAS_FAILED":e.fileId===i&&(g=!0);break}};async function v(){if(!m)throw new Error("No file");if(c=m.size,f=Math.ceil(c/S),d=Array.from({length:f},(t,e)=>D(e)),p={},d.forEach(t=>p[t]=0),u=0,a("info","upload-start",{size:c,chunks:f,newBlockIdExample:d[0]}),f===1)return z();if(await L(),u<c){let t=c-u;throw a("error",`Commit failed due to progress mismatch. Missing ${t} bytes.`,{bytesUploaded:u,totalBytes:c}),new Error("Commit failed: worker tracking mismatch. Not all blocks registered successfully.")}await q(),T()}async function z(){(!o||P())&&await $();let t=R+encodeURIComponent(`${E}?${o}`);await new Promise((e,n)=>{let r=new XMLHttpRequest;r.open("PUT",t,!0),r.setRequestHeader("token",b),r.setRequestHeader("x-ms-blob-type","BlockBlob"),r.setRequestHeader("Content-Type",m.type||"application/octet-stream"),r.upload.onprogress=l=>{l.lengthComputable&&postMessage({type:"PROGRESS",fileId:i,percent:Math.round(l.loaded/l.total*100)})},r.onload=()=>r.status>=200&&r.status<300?e():n(r.responseText),r.onerror=()=>n("Network error"),r.send(m)}),T()}async function L(){let t=0,e=0;return new Promise((n,r)=>{let l=()=>{if(w)return r("Canceled");for(;!y&&e<_&&t<f;){let C=t++;e++,N(C).then(()=>{e--,t>=f&&e===0?n():l()}).catch(r)}};l()})}async function N(t){let e=0,n=d[t];for(;;){(!o||P())&&await $();try{await O(t,n);return}catch(r){if(e++,a("warn",`putBlock failed for chunk ${t}. Retrying (try ${e}/${I}).`,String(r)),e>I)throw new Error(`Chunk ${t} failed after max retries: ${String(r)}`);await M(B)}}}function O(t,e){return new Promise((n,r)=>{let l=t*S,C=Math.min(l+S,c),k=m.slice(l,C),s=new XMLHttpRequest;s.open("PUT",R+encodeURIComponent(`${E}?${o}&comp=block&blockid=${encodeURIComponent(e)}`),!0),s.setRequestHeader("x-ms-version",U),s.setRequestHeader("token",b),s.upload.onprogress=h=>{if(!h.lengthComputable)return;let x=p[e],F=h.loaded-x;p[e]=h.loaded,u+=F,A()},s.onload=()=>{if(s.status>=200&&s.status<300){let h=p[e]||0,x=k.size-h;p[e]=k.size,u+=x,A(),a("info",`Chunk ${t} (${e}) successful. Size: ${k.size} bytes.`),n()}else a("error",`putBlock HTTP Error for chunk ${t} (${e})`,{status:s.status,response:s.responseText}),r(s.responseText||s.statusText)},s.onerror=()=>{a("error",`putBlock Network Error for chunk ${t} (${e})`),r("Network error")},s.send(k)})}async function q(){(!o||P())&&await $(),a("info","Waiting 2 seconds before commit for Azure state finalization..."),await M(2e3);let t=`<?xml version="1.0" encoding="utf-8"?>
<BlockList>
`+d.map(n=>`  <Latest>${n}</Latest>`).join(`
`)+`
</BlockList>`;a("info","Committing block list...",{totalBlocks:d.length});let e=await fetch(R+encodeURIComponent(`${E}?${o}&comp=blocklist`),{method:"PUT",headers:{"Content-Type":"application/xml","x-ms-version":U,token:b},body:t});if(!e.ok){let n=await e.text();throw a("error","Commit failed!",{status:e.status,response:n}),new Error(n)}a("info","Commit successful!")}
