···11+import{f as P,g as C}from"../../../../../chunk-GEPOAB4D.js";import{a as B}from"../../../../../chunk-HDWT7CXU.js";import"../../../../../chunk-TTDW3IDD.js";import"../../../../../chunk-RJJBOBOS.js";import{a as O}from"../../../../../chunk-YEYXC3F5.js";import"../../../../../chunk-6NC6NOLO.js";import"../../../../../chunk-7VVACK5Z.js";import{b as N}from"../../../../../chunk-6WW6Q6YW.js";import"../../../../../chunk-7EUCOO7A.js";import"../../../../../chunk-WLXGGMHC.js";import"../../../../../chunk-QVM63RP5.js";import"../../../../../chunk-UYT7NSKP.js";import"../../../../../chunk-MWEXZQ4O.js";import{a as S}from"../../../../../chunk-2CFBJKYZ.js";import"../../../../../chunk-524HX7V4.js";import"../../../../../chunk-DFARJRPB.js";import"../../../../../chunk-K2CRUTCH.js";import{a as M,b as j}from"../../../../../chunk-GKONEEZQ.js";import{a as L}from"../../../../../chunk-6TMSPXBP.js";import{j as A,o as x}from"../../../../../chunk-4I6PUBR2.js";import{a as f}from"../../../../../chunk-BFIVCPQF.js";var T={cid:void 0,data:[],inventory:{current:{},removed:[]}},E=class extends O{static{f(this,"DaslBytesSyncOutputTransformer")}static NAME="diffuse/transformer/output/bytes/dasl-sync";constructor(){super();let e=this.base(),s=this.#e.get,i=f((a,g,k,{saveLocal:w,saveRemote:d})=>{let r=x(T,{compare:j}),v=x(!1),n=x({isBusy:!1,lastCID:""},{compare:M});return this.effect(()=>{if(!v.value||n.value.isBusy)return;let t=g(),o=e.ready()?k():void 0,h=t?.state==="loaded"?t.data:void 0,D=o?.state==="loaded"?o.data:void 0,b=o?.state,y=h?P(h):void 0,l=D&&b==="loaded"?P(D):void 0;l?y?b==="loaded"&&this.hasDiverged({local:y,remote:l})?this.isLeader().then(I=>{I&&(n.value={isBusy:!0,lastCID:n.value.lastCID},this.merge(y,l).then(async u=>{try{if(r.value=u,u.cid===n.value.lastCID)return;let R=this.save(u);u.cid!==y.cid&&await w(R),e.ready()&&b==="loaded"&&u.cid!==l.cid&&await d(R)}finally{n.value={isBusy:!1,lastCID:u.cid??""}}}))}):r.value=y:(r.value=l,this.isLeader().then(I=>{if(!I)return;let u=this.save(l);w(u)})):y&&(r.value=y,e.ready()&&b==="loaded"&&this.isLeader().then(I=>{if(!I)return;let u=this.save(y);d(u)}))}),A(()=>(v.get()||(v.value=!0),r.get()))},"state"),m=i("facets",A(()=>s()?.facets.collection()??{state:"loading"}),e.facets.collection,{saveLocal:f(async a=>s()?.facets.save(a),"saveLocal"),saveRemote:e.facets.save}),p=i("playlistItems",A(()=>s()?.playlistItems.collection()??{state:"loading"}),e.playlistItems.collection,{saveLocal:f(async a=>s()?.playlistItems.save(a),"saveLocal"),saveRemote:e.playlistItems.save}),c=i("tracks",A(()=>s()?.tracks.collection()??{state:"loading"}),e.tracks.collection,{saveLocal:f(async a=>s()?.tracks.save(a),"saveLocal"),saveRemote:e.tracks.save});this.facets=this.managerProp({save:f(async a=>s()?.facets.save(a),"save")},e.facets,m),this.playlistItems=this.managerProp({save:f(async a=>s()?.playlistItems.save(a),"save")},e.playlistItems,p),this.tracks=this.managerProp({save:f(async a=>s()?.tracks.save(a),"save")},e.tracks,c),this.ready=()=>!0}#e=x(void 0);async connectedCallback(){this.hasAttribute("group")&&this.broadcast(this.identifier,{}),super.connectedCallback();let e=this.root().querySelector("dop-indexed-db");if(!e)throw new Error("Can't find local output");customElements.whenDefined(e.localName).then(()=>{this.#e.value=e})}async updateContainer({previous:e,collection:s}){let i=e.inventory,m=s.map(({id:n})=>n),p=new Set(Object.keys(i.current)),c=new Set(m),a=c.difference(p),g=p.difference(c),w=new Set(i.removed).union(g),d={...i.current};g.forEach(n=>{delete d[n]});let r=[];s.forEach(n=>{if(!a.has(n.id))return;let t=C(n);r.push((async()=>{let o=await S(113,t);d[n.id]=o})())}),await Promise.all(r);let v={current:d,removed:Array.from(w)};return{cid:await S(113,C(v)),data:s,inventory:v}}hasDiverged({local:e,remote:s}){return e.cid!==s.cid}async merge(e,s){let i=new Set(e.inventory.removed),m=new Set(s.inventory.removed),p=i.union(m),c=e.inventory.current,a=s.inventory.current,g=new Map(e.data.map(t=>[t.id,t])),k=new Map(s.data.map(t=>[t.id,t])),w=new Set([...Object.keys(c),...Object.keys(a)]),d={},r=[],v=[];for(let t of w)if(!p.has(t))if(t in c&&t in a){let o=g.get(t),h=k.get(t);if(!o||!h){console.warn("Should have found both items but didn't!");continue}if(c[t]===a[t]){r.push(o),d[t]=c[t];continue}let D=o.updatedAt&&h.updatedAt?N(o.updatedAt,h.updatedAt)>0:!1,b=D?o:h,l={...D?h:o};L.applyDiff(l,b),r.push(l),v.push(S(113,C(l)).then(I=>{d[t]=I}))}else{let o=g.get(t)??k.get(t);o&&(r.push(o),d[t]=c[t]??a[t])}await Promise.all(v);let n={current:d,removed:Array.from(p)};return{cid:await S(113,C(n)),data:r,inventory:n}}save(e){return C(e)}managerProp(e,s,i){return{collection:A(()=>{let m=i();return s.collection().state==="loading"?{state:"loading"}:{state:"loaded",data:m.data}}),reload:s.reload,save:f(async m=>{let p=await this.updateContainer({collection:m,previous:i()}),c=this.save(p);await e.save(c)},"save")}}render({html:e}){return e`
22+ <dop-indexed-db
33+ group="${B(this.getAttribute("group"))}"
44+ namespace="${B(this.getAttribute("namespace"))}"
55+ ></dop-indexed-db>
66+ `}},W=E,q=E,$="dtob-dasl-sync";customElements.define($,q);export{q as CLASS,$ as NAME,W as default};
77+88+//# sourceMappingURL=./element.js.map