(()=>{let e;htmx.registerExtension("upsert",{init:t=>{e=t},htmx_process_upsert:(t,r)=>{let{ctx:i,tasks:n}=r,l={style:"upsert"},s=t.getAttribute("key"),o=t.getAttribute("sort"),p=t.hasAttribute("prepend");s&&(l.key=s),null!==o&&(l.sort=o||!0),p&&(l.prepend=!0),n.push({type:"partial",fragment:t.content.cloneNode(!0),target:e.attributeValue(t,"hx-target"),swapSpec:l,sourceElement:i.sourceElement})},handle_swap:(e,t,r,i)=>{if("upsert"===e){let e=i.key||"id",n="desc"===i.sort,l=t.firstChild,s=t=>t.getAttribute(e)||t.id,o=(e,t)=>{let r=e.localeCompare(t,void 0,{numeric:!0});return n?-r:r};for(let e of Array.from(r.children)){let r=e.id;if(r){let t=document.getElementById(r);if(t){t.replaceWith(e);continue}}let n=s(e);if(!n){i.prepend?t.insertBefore(e,l):t.appendChild(e);continue}let p=!1;for(let r of t.children){let i=s(r);if(i&&o(n,i)<0){t.insertBefore(e,r),p=!0;break}}p||t.appendChild(e)}return!0}return!1}})})();