import{a as B,j as C,m as P}from"./chunk-5NY4HKJF.js";import{e as g}from"./chunk-YBFCDINU.js";import{e as U,f as _}from"./chunk-KOB6T6NK.js";import{l as v,m as w}from"./chunk-5T3OVTF5.js";import{xa as E}from"./chunk-2HZMOK4U.js";import{Pb as m}from"./chunk-EYDNSYYE.js";import{g as T}from"./chunk-VKWPEFWQ.js";T();var $=20,J=1024*1024,A=2;function oe(t,e,r,s,o={},l){let n=B(r),i=new Map,c=a=>a.total===0?"[100%]":`[${Math.round(a.current/a.total*100)}%]`,u=R(t,e,n),h=Promise.all([s.ready(),u]).then(()=>q(n,s,t,e,i)),p=h.then(a=>a.promise).then(()=>j(n,s,t,e,o,i)),k=o?.deferPartialWork?u:p.then(a=>a.promise);return o?.backgroundWorkCatch&&Promise.all([u,h.then(a=>a.promise),p.then(a=>a.promise)]).catch(o.backgroundWorkCatch),{uploadResults:i,workPromise:k,renderThemeSyncProgress:async()=>{if(o?.deferPartialWork)return;let{progress:a,promise:d}=await h,f=o.multiEnvironment?4e3:1e3;await g(b({promise:d,titleGetter:()=>`Uploading files to remote theme ${c(a)}`,updateInterval:f}),l?.stderr,o?.multiEnvironment);let{progress:y,promise:F}=await p;await g(b({promise:F,titleGetter:()=>`Cleaning your remote theme ${c(y)}`,updateInterval:f}),l?.stderr,o?.multiEnvironment),await g([{title:"Theme upload complete",task:async()=>{}}],l?.stderr,o?.multiEnvironment),H(i,o?.environment)}}}function b({promise:t,titleGetter:e,updateInterval:r}){let s=[],o=()=>{s.push({title:e(),task:async()=>{await Promise.race([t,new Promise(n=>setTimeout(()=>n("timeout"),r))])==="timeout"&&o()}})};return o(),s}function j(t,e,r,s,o,l){if(o.nodelete)return{progress:{current:0,total:0},promise:Promise.resolve()};let n=L(t,e),i=x(n),c={current:0,total:i.length};if(i.length===0)return{progress:c,promise:Promise.resolve()};let u=[];for(let p=0;p<i.length;p+=$){let k=i.slice(p,p+$),a=v(r.id,k.map(d=>d.key),s).then(d=>{d.forEach(f=>{if(l.set(f.key,f),!f.success){let y=f.errors?.asset?.map(F=>`-${F}`).join(`
`);_(f.key,"delete")(new Error(`Failed to delete ${f.key}: ${y}`))}}),c.current+=k.length});u.push(a)}let h=Promise.all(u).then(()=>{c.current=c.total});return{progress:c,promise:h}}function L(t,e){let s=e.applyIgnoreFilters(t).filter(o=>!e.files.has(o.key));return m(`Files to be deleted:
${s.map(o=>`-${o.key}`).join(`
`)}`),s}function x(t){let e=P(t);return[...e.contextualizedJsonFiles,...e.templateJsonFiles,...e.sectionJsonFiles,...e.otherJsonFiles,...e.sectionLiquidFiles,...e.blockLiquidFiles,...e.layoutFiles,...e.otherLiquidFiles,...e.configDataFile,...e.configSchemaFile,...e.staticAssetFiles]}var D=[{key:"config/settings_schema.json",value:"[]"},{key:"layout/password.liquid",value:"{{ content_for_header }}{{ content_for_layout }}"},{key:"layout/theme.liquid",value:"{{ content_for_header }}{{ content_for_layout }}"}];async function R(t,e,r){let s=new Set(r.map(l=>l.key)),o=D.filter(({key:l})=>!s.has(l));o.length>0&&await w(t.id,o,e)}function q(t,e,r,s,o){let l=I(e,t);e.unsyncedFileKeys.clear(),l.forEach(a=>e.unsyncedFileKeys.add(a.key));let{independentFiles:n,dependentFiles:i}=z(l),c={current:0,total:l.length},u=a=>a.length===0?Promise.resolve():Promise.all(S(a).map(d=>N(d,e,s,r.id,o).then(()=>{c.current+=d.length,d.forEach(f=>e.unsyncedFileKeys.delete(f.key))}))).then(()=>{}),h=i.reduce((a,d)=>a.then(()=>u(d)),Promise.resolve()),p=Promise.resolve().then(()=>u(n.flat())),k=Promise.all([h,p]).then(()=>{c.current=c.total});return{progress:c,promise:k}}function I(t,e){let r=K(t),s=t.applyIgnoreFilters(r),o=new Map(e.map(n=>[n.key,n])),l=s.filter(n=>{let i=o.get(n.key);return!i||i.checksum!==n.checksum});return m(`Files to be uploaded:
${l.map(n=>`-${n.key}`).join(`
`)}`),l}function z(t){let e=P(t);return{independentFiles:[e.otherLiquidFiles,e.otherJsonFiles,e.staticAssetFiles],dependentFiles:[e.configSchemaFile,e.layoutFiles,e.blockLiquidFiles,e.sectionLiquidFiles,e.sectionJsonFiles,e.templateJsonFiles,e.contextualizedJsonFiles,e.configDataFile]}}function S(t){let e=[],r=[],s=0;for(let o of t){let l=r.length>=$,n=s>=J;(l||n)&&(e.push(r),r=[],s=0),r.push(o),s+=o.size??0}return r.length>0&&e.push(r),e}function K(t){let e=[];return t.files.forEach((r,s)=>{let o=r.value?Buffer.byteLength(r.value,"utf8"):r.attachment?.length??0;e.push({key:s,checksum:r.checksum,size:o})}),e}async function N(t,e,r,s,o){let l=t.map(i=>{let c=e.files.get(i.key)?.value,u=e.files.get(i.key)?.attachment;return{key:i.key,...c&&{value:c},...u&&{attachment:u}}});m(`Uploading the following files:
${t.map(i=>`-${i.key}`).join(`
`)}`),(await M(l,s,r)).forEach(i=>{o.set(i.key,i),W(i,e,s)})}function W(t,e,r){if(t.success)e.uploadErrors.delete(t.key);else{let s=t.errors?.asset??["Response was not successful."];e.uploadErrors.set(t.key,s),C(r,t.key)}}async function M(t,e,r,s=0){if(t.length===0)return[];s>0&&m(`Retry Attempt ${s}/${A} for the following files:
      ${t.map(n=>`-${n.key}`).join(`
`)}`);let o=await w(e,t,r);m(`File Upload Results:
${o.map(n=>`-${n.key}: ${n.success?"success":"failure"}`).join(`
`)}`);let l=o.filter(n=>!n.success);if(l.length>0){m(`The following files failed to upload:
${l.map(i=>`-${i.key}`).join(`
`)}`);let n=await G(l,t,e,r,s);return o.concat(n)}return o}async function G(t,e,r,s,o){let l=new Set(t.map(i=>i.key)),n=e.filter(i=>l.has(i.key));return o===A?(E("theme-service:upload-failed-retry:max-retry"),m(`Max retry count reached for the following files:
${n.map(i=>`-${i.key}`).join(`
`)}`),t):(E(`theme-service:upload-failed-retry:${n.length}`),M(n,r,s,o+1))}function H(t,e){for(let[r,s]of t.entries())if(!s.success){let o=s.errors?.asset?.join(`
`)??"File upload failed",l=e?`[${e}] ${r}`:r;U(l,new Error(o))}}export{oe as a};
