let url,direction,isNested,dragElementIndex,dropElementIndex,form,formData,container=document.querySelector(".js-draggable");if(container)url=container.dataset.url,direction=container.dataset.direction,isNested=container.dataset.nested;else if(Joomla.getOptions("draggable-list")){const e=Joomla.getOptions("draggable-list");container=document.querySelector(e.id),container.classList.contains("js-draggable")||container.classList.add("js-draggable"),({url:url}=e),({direction:direction}=e),isNested=e.nested}if(container){form=container.closest("form"),formData=new FormData(form),formData.delete("task"),formData.delete("order[]"),document.addEventListener("touchstart",(()=>{}),!1);const e=(e,t,a,r)=>{let n;const o=[];if(a<r)for(e[r].value=e[r-1].value,n=a;n<r;n+=1)e[n].value="asc"===direction?parseInt(e[n].value,10)-1:parseInt(e[n].value,10)+1;else for(e[r].value=e[r+1].value,n=r+1;n<=a;n+=1)e[n].value="asc"===direction?parseInt(e[n].value,10)+1:parseInt(e[n].value,10)-1;for(n=0;n<e.length-1;n+=1)o.push(`order[]=${encodeURIComponent(e[n].value)}`),o.push(`cid[]=${encodeURIComponent(t[n].value)}`);return o},t=e=>{if(!e.dataset.itemId)return;const t=e.dataset.itemId,a=container.querySelectorAll(`tr[data-parents~="${t}"]`);a.length&&e.after(...a)},a=a=>{let r,n,o;const l=a.dataset.draggableGroup;l?(o=`tr[data-draggable-group="${l}"]`,r=`[data-draggable-group="${l}"] [name="order[]"]`,n=`[data-draggable-group="${l}"] [name="cid[]"]`):(o="tr",r='[name="order[]"]',n='[name="cid[]"]');const d=[].slice.call(container.querySelectorAll(o)),s=[].slice.call(container.querySelectorAll(r)),c=[].slice.call(container.querySelectorAll(n));if(dropElementIndex=d.indexOf(a),url){const t=document.querySelector('[name="task"]');t&&t.setAttribute("name","some__Temporary__Name__");const a={url:url,method:"POST",data:`${new URLSearchParams(formData).toString()}&${e(s,c,dragElementIndex,dropElementIndex).join("&")}`,perform:!0};Joomla.request(a),t&&t.setAttribute("name","task")}t(a)};dragula([container],{direction:"vertical",copy:!1,revertOnSpill:!0,accepts:(e,t,a,r)=>isNested&&null!==r?r.dataset.draggableGroup&&r.dataset.draggableGroup===e.dataset.draggableGroup:null===r||r&&"tr"===r.tagName.toLowerCase(),mirrorContainer:container}).on("drag",(e=>{let t;const a=e.dataset.draggableGroup;t=a?`tr[data-draggable-group="${a}"]`:"tr";const r=[].slice.call(container.querySelectorAll(t));dragElementIndex=r.indexOf(e)})).on("drop",(e=>{a(e)}))}