import{KeyboardCode,closestCorners,getFirstCollision}from"@dnd-kit/core";import{getProjection}from"./utilities.js";const directions=[KeyboardCode.Down,KeyboardCode.Right,KeyboardCode.Up,KeyboardCode.Left],horizontal=[KeyboardCode.Left,KeyboardCode.Right];export const sortableTreeKeyboardCoordinates=(e,o,t)=>(i,{currentCoordinates:d,context:{active:r,over:n,collisionRect:s,droppableRects:c,droppableContainers:a}})=>{if(directions.includes(i.code)){if(!r||!s)return;i.preventDefault();const{current:{items:p,offset:l}}=e;if(horizontal.includes(i.code)&&n?.id){const{depth:e,maxDepth:o,minDepth:s}=getProjection(p,`${r.id}`,`${n.id}`,l,t);switch(i.code){case KeyboardCode.Left:if(e>s)return{...d,x:d.x-t};break;case KeyboardCode.Right:if(e<o)return{...d,x:d.x+t}}return}const f=[];a.forEach((e=>{if(e?.disabled||e.id===n?.id)return;const o=c.get(e.id);if(o)switch(i.code){case KeyboardCode.Down:s.top<o.top&&f.push(e);break;case KeyboardCode.Up:s.top>o.top&&f.push(e)}}));const h=closestCorners({active:r,collisionRect:s,pointerCoordinates:null,droppableRects:c,droppableContainers:f});let b=getFirstCollision(h,"id");if(b===n?.id&&h.length>1&&(b=h[1].id),b&&n?.id){const e=c.get(r.id),i=c.get(b),d=a.get(b);if(e&&i&&d){const d=p.findIndex((({id:e})=>e===b)),n=p[d],c=p.findIndex((({id:e})=>e===r.id)),a=p[c];if(n&&a){const{depth:l}=getProjection(p,`${r.id}`,`${b}`,(n.depth-a.depth)*t,t),f=d>c?1:-1,h=o?(s.height-e.height)/2:0;return{x:i.left+l*t,y:i.top+f*h}}}}}};