import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{useEffect,useMemo,useRef,useState}from"react";import{createPortal}from"react-dom";import{DndContext,closestCenter,KeyboardSensor,PointerSensor,useSensor,useSensors,DragOverlay,MeasuringStrategy,defaultDropAnimation}from"@dnd-kit/core";import{SortableContext,arrayMove,verticalListSortingStrategy}from"@dnd-kit/sortable";import{buildTree,flattenTree,getProjection,getChildCount,removeItem,removeChildrenOf,setProperty}from"./utilities";import{sortableTreeKeyboardCoordinates}from"./keyboardCoordinates";import{SortableTreeItem}from"./components";import{CSS}from"@dnd-kit/utilities";const measuring={droppable:{strategy:MeasuringStrategy.Always}},dropAnimationConfig={keyframes:({transform:e})=>[{opacity:1,transform:CSS.Transform.toString(e.initial)},{opacity:0,transform:CSS.Transform.toString({...e.final,x:e.final.x+5,y:e.final.y+5})}],easing:"ease-out",sideEffects({active:e}){e.node.animate([{opacity:0},{opacity:1}],{duration:defaultDropAnimation.duration,easing:defaultDropAnimation.easing})}};function mapTreeItemsToItems(e){return{id:e.id,module:{name:e.module.name,options:e.module.options},props:{...e.props,children:e.props.children?e.props.children.map(mapTreeItemsToItems):[]}}}export function SortableTree({collapsible:e,treeItems:t=[],currItemId:o="",indicator:r=!1,indentationWidth:n=30,removable:i,onSelect:a,onDelete:d,reorderModules:s}){const[l,c]=useState(t),[u,m]=useState(null),[p,f]=useState(null),[g,v]=useState(0),[S,y]=useState(null);useEffect((()=>{const e=t.map(mapTreeItemsToItems);c(e)}),[t]);const h=useMemo((()=>{const e=flattenTree(l),t=e.reduce(((e,{props:{children:t},collapsed:o,id:r})=>o&&t&&t.length?[...e,r]:e),[]);return removeChildrenOf(e,u?[u,...t]:t)}),[u,l]),I=u&&p?getProjection(h,u,p,g,n):null,b=useRef({items:h,offset:g}),[C]=useState((()=>sortableTreeKeyboardCoordinates(b,r,n))),D=useSensors(useSensor(PointerSensor),useSensor(KeyboardSensor,{coordinateGetter:C})),x=useMemo((()=>h.map((({id:e})=>e))),[h]),T=u?h.find((({id:e})=>e===u)):null;useEffect((()=>{b.current={items:h,offset:g}}),[h,g]);return _jsx(DndContext,{accessibility:{announcements:{onDragStart:({active:e})=>`Picked up ${e.id}.`,onDragMove:({active:e,over:t})=>M("onDragMove",e.id,t?.id),onDragOver:({active:e,over:t})=>M("onDragOver",e.id,t?.id),onDragEnd:({active:e,over:t})=>M("onDragEnd",e.id,t?.id),onDragCancel:({active:e})=>`Moving was cancelled. ${e.id} was dropped in its original position.`}},sensors:D,collisionDetection:closestCenter,measuring:measuring,onDragStart:function({active:{id:e}}){m(e),f(e);const t=h.find((({id:t})=>t===e));t&&y({parentId:t.parentId,overId:e});document.body.style.setProperty("cursor","grabbing")},onDragMove:function({delta:e}){v(e.x)},onDragOver:function({over:e}){f(e?.id??null)},onDragEnd:function({active:e,over:t}){if(j(),I&&t){const{depth:o,parentId:r}=I,n=JSON.parse(JSON.stringify(flattenTree(l))),i=n.findIndex((({id:e})=>e===t.id)),a=n.findIndex((({id:t})=>t===e.id)),d=n[a];n[a]={...d,depth:o,parentId:r};const u=arrayMove(n,a,i),m=buildTree(u);if(s)return void s(u);c(m)}},onDragCancel:function(){j()},children:_jsxs(SortableContext,{items:x,strategy:verticalListSortingStrategy,children:[h.map((({id:t,props:{children:s},collapsed:l,depth:m,module:{name:p}})=>_jsx(SortableTreeItem,{id:t,value:p,depth:t===u&&I?I.depth:m,indentationWidth:n,indicator:r,highlighted:t===o,collapsed:Boolean(l&&s&&s.length),onClickName:a,onCollapse:e&&s&&s.length?()=>function(e){c((t=>setProperty(t,e,"collapsed",(e=>!e))))}(t):void 0,onRemove:i?()=>function(e){if(d)return void d(e);c((t=>removeItem(t,e)))}(t):void 0},t))),createPortal(_jsx(DragOverlay,{dropAnimation:dropAnimationConfig,zIndex:9999,modifiers:r?[adjustTranslate]:void 0,children:u&&T?_jsx(SortableTreeItem,{id:u,depth:T.depth,clone:!0,childCount:getChildCount(l,u)+1,value:h.find((({id:e})=>e===u))?.module.name??"",indentationWidth:n}):null}),document.body)]})});function j(){f(null),m(null),v(0),y(null),document.body.style.setProperty("cursor","")}function M(e,t,o){if(o&&I){if("onDragEnd"!==e){if(S&&I.parentId===S.parentId&&o===S.overId)return;y({parentId:I.parentId,overId:o})}const r=JSON.parse(JSON.stringify(flattenTree(l))),n=r.findIndex((({id:e})=>e===o)),i=r.findIndex((({id:e})=>e===t)),a=arrayMove(r,i,n),d=a[n-1];let s;const c="onDragEnd"===e?"dropped":"moved",u="onDragEnd"===e?"dropped":"nested";if(d)if(I.depth>d.depth)s=`${t} was ${u} under ${d.id}.`;else{let e=d;for(;e&&I.depth<e.depth;){const t=e.parentId;e=a.find((({id:e})=>e===t))}e&&(s=`${t} was ${c} after ${e.id}.`)}else{const e=a[n+1];s=`${t} was ${c} before ${e.id}.`}return s}}}const adjustTranslate=({transform:e})=>({...e,y:e.y-25});