import{a as e,b as t,g as n,o}from"./vendor-zU9dkAB-.js";const i=(e,t,n)=>Math.max(t,Math.min(e,n));function s(s){const r=Math.max(1,Math.floor(s.dragThresholdPx??6)),l=Math.max(0,Math.floor(s.edgeScrollThresholdPx??56)),a=Math.max(1,Math.floor(s.edgeScrollMaxStepPx??20)),[d,c]=e(null),[u,g]=e(null),[f,m]=e(null);let p=null,h=null,I=0,v=null;const x=new Map,M=()=>{document.removeEventListener("pointermove",S),document.removeEventListener("pointerup",P),document.removeEventListener("pointercancel",w)},b=()=>{v?.(),v=null},y=()=>{if(!h?.dragging)return;const e=((e,t)=>{const n=s.sessions();if(n.findIndex(t=>t.id===e)<0)return{targetIndex:0,targetSessionId:null};const o=n.filter(t=>t.id!==e);if(!o.length)return{targetIndex:0,targetSessionId:null};let r=o.length,l=o[o.length-1]?.id??null;for(let i=0;i<o.length;i+=1){const e=x.get(o[i].id);if(!e)continue;const n=e.getBoundingClientRect();if(t<n.left+n.width/2){r=i,l=o[i].id;break}}return{targetIndex:i(r,0,n.length-1),targetSessionId:l}})(h.sessionId,h.latestClientX);g(e.targetIndex),m(e.targetSessionId)},C=()=>{if(I=0,!h?.dragging||!p||l<=0)return;const e=p.getBoundingClientRect();let t=0;if(h.latestClientX<e.left+l){const n=(e.left+l-h.latestClientX)/l;t=-Math.max(4,Math.round(n*a))}else if(h.latestClientX>e.right-l){const n=(h.latestClientX-(e.right-l))/l;t=Math.max(4,Math.round(n*a))}if(0!==t){const e=Math.max(0,p.scrollWidth-p.clientWidth),n=i(p.scrollLeft+t,0,e);n!==p.scrollLeft&&(p.scrollLeft=n,y())}I=requestAnimationFrame(C)},T=()=>{!I&&h?.dragging&&(I=requestAnimationFrame(C))},X=()=>{h=null,I&&(cancelAnimationFrame(I),I=0),M(),c(null),g(null),m(null),"undefined"!=typeof document&&document.body.style.removeProperty("cursor")},L=(e,t)=>{if(!h)return;if(e&&e.pointerId!==h.pointerId)return;const n=h,o=u(),i=t&&n.dragging&&null!==o&&o!==n.startIndex,r=n.dragging;X(),i&&null!==o&&s.onMoveSession(n.sessionId,o),r&&(()=>{b();let e=!1,t=0;const n=()=>{e||(e=!0,document.removeEventListener("click",o,!0),window.clearTimeout(t),v===n&&(v=null))},o=e=>{n(),e.preventDefault(),e.stopPropagation(),"function"==typeof e.stopImmediatePropagation&&e.stopImmediatePropagation()};document.addEventListener("click",o,!0),t=window.setTimeout(n,180),v=n})(),e?.cancelable&&r&&e.preventDefault()};function P(e){L(e,!0)}function w(e){L(e,!1)}function S(e){if(h&&e.pointerId===h.pointerId){if(h.latestClientX=e.clientX,h.latestClientY=e.clientY,!h.dragging){const n=e.clientX-h.startClientX,o=e.clientY-h.startClientY;if(n*n+o*o<r*r)return;h.dragging=!0,c(h.sessionId),g(h.startIndex),m(h.sessionId),"mouse"===h.pointerType&&(t="grabbing","undefined"!=typeof document&&(document.body.style.cursor=t)),T()}var t;y(),e.cancelable&&e.preventDefault()}}const E=t(()=>{const e=d(),t=u();return e&&null!==t?((e,t,n)=>{const o=e.findIndex(e=>e.id===t);if(o<0||e.length<=1)return e;const s=i(n,0,e.length-1);if(s===o)return e;const r=e.slice(),[l]=r.splice(o,1);return r.splice(s,0,l),r})(s.sessions(),e,t):s.sessions()}),Y=t(()=>{const e={};for(const[t,n]of s.sessions().entries())e[n.id]=t;return e}),D=t(()=>{const e={};for(const[t,n]of E().entries())e[n.id]=t;return e});return n(()=>{const e=new Set(s.sessions().map(e=>e.id));for(const n of Array.from(x.keys()))e.has(n)||x.delete(n);const t=d();t&&!e.has(t)&&X()}),o(()=>{b(),X()}),{sessions:E,setTabsRef:e=>{p=e},setTabRef:(e,t)=>{t?x.set(e,t):x.delete(e)},handleTabPointerDown:(e,t)=>{if(h||!(e=>"mouse"===e.pointerType?0===e.button:!1!==e.isPrimary)(t))return;const n=s.sessions().findIndex(t=>t.id===e);n<0||(h={pointerId:t.pointerId,pointerType:t.pointerType||"mouse",sessionId:e,startIndex:n,startClientX:t.clientX,startClientY:t.clientY,latestClientX:t.clientX,latestClientY:t.clientY,dragging:!1},M(),document.addEventListener("pointermove",S),document.addEventListener("pointerup",P),document.addEventListener("pointercancel",w))},isDraggingTab:e=>d()===e,isDropTargetTab:e=>{const t=d();return!(!t||t===e)&&f()===e},isDropPreviewTab:e=>{const t=d();return!(!t||t===e)&&Y()[e]!==D()[e]}}}export{s as u};
