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,DragOverlay,KeyboardSensor,MeasuringStrategy,PointerSensor,closestCenter,defaultDropAnimation,useSensor,useSensors}from"@dnd-kit/core";import{SortableContext,arrayMove,verticalListSortingStrategy}from"@dnd-kit/sortable";import{CSS}from"@dnd-kit/utilities";import{SortableTreeItem}from"./components/index.js";import{sortableTreeKeyboardCoordinates}from"./keyboardCoordinates.js";import{buildTree,flattenTree,getChildCount,getProjection,removeChildrenOf,removeItem}from"./utilities.js";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:n="",indicator:o=!1,indentationWidth:r=24,removable:i,onSelect:a,onDelete:d,reorderModules:s}){const[l,c]=useState(t),[u,m]=useState(new Set),[p,f]=useState(null),[g,S]=useState(null),[v,h]=useState(0),[y,I]=useState(null);useEffect((()=>{const e=t.map((e=>mapTreeItemsToItems(e)));c(e)}),[t]);const b=useMemo((()=>{const e=flattenTree(l),t=e.reduce(((e,{props:{children:t},id:n})=>u.has(n)&&t&&t.length?[...e,n]:e),[]);return removeChildrenOf(e,p?[p,...t]:t)}),[p,l,u]),x=p&&g?getProjection(b,p,g,v,r):null,C=useRef({items:b,offset:v}),[D]=useState((()=>sortableTreeKeyboardCoordinates(C,o,r))),T=useSensors(useSensor(PointerSensor),useSensor(KeyboardSensor,{coordinateGetter:D})),j=useMemo((()=>b.map((({id:e})=>e))),[b]),M=p?b.find((({id:e})=>e===p)):null;useEffect((()=>{C.current={items:b,offset:v}}),[b,v]);return _jsx(DndContext,{accessibility:{announcements:{onDragStart:({active:e})=>`Picked up ${e.id}.`,onDragMove:({active:e,over:t})=>$("onDragMove",e.id,t?.id),onDragOver:({active:e,over:t})=>$("onDragOver",e.id,t?.id),onDragEnd:({active:e,over:t})=>$("onDragEnd",e.id,t?.id),onDragCancel:({active:e})=>`Moving was cancelled. ${e.id} was dropped in its original position.`}},sensors:T,collisionDetection:closestCenter,measuring:measuring,onDragStart:function({active:{id:e}}){f(e),S(e);const t=b.find((({id:t})=>t===e));t&&I({parentId:t.parentId,overId:e});document.body.style.setProperty("cursor","grabbing")},onDragMove:function({delta:e}){h(e.x)},onDragOver:function({over:e}){S(e?.id??null)},onDragEnd:function({active:e,over:t}){if(O(),x&&t){const{depth:n,parentId:o}=x,r=JSON.parse(JSON.stringify(flattenTree(l))),i=r.findIndex((({id:e})=>e===t.id)),a=r.findIndex((({id:t})=>t===e.id)),d=r[a];r[a]={...d,depth:n,parentId:o};const u=arrayMove(r,a,i),m=buildTree(u);if(s)return void s(u);c(m)}},onDragCancel:function(){O()},children:_jsxs(SortableContext,{items:j,strategy:verticalListSortingStrategy,children:[b.map((({id:t,props:{children:s},depth:l,module:{name:f}},g)=>_jsx(SortableTreeItem,{id:t,index:g,value:f,depth:t===p&&x?x.depth:l,indentationWidth:r,indicator:o,highlighted:t===n,collapsed:Boolean(u.has(t)&&s&&s.length),onClickName:a,onCollapse:e&&s&&s.length?()=>function(e){const t=new Set(u);u.has(e)?(console.log("here",e),t.delete(e)):(console.log("there",e),t.add(e));m(t)}(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:o?[adjustTranslate]:void 0,children:p&&M?_jsx(SortableTreeItem,{id:p,depth:M.depth,clone:!0,childCount:getChildCount(l,p)+1,value:b.find((({id:e})=>e===p))?.module.name??"",indentationWidth:r}):null}),document.body)]})});function O(){S(null),f(null),h(0),I(null),document.body.style.setProperty("cursor","")}function $(e,t,n){if(n&&x){if("onDragEnd"!==e){if(y&&x.parentId===y.parentId&&n===y.overId)return;I({parentId:x.parentId,overId:n})}const o=JSON.parse(JSON.stringify(flattenTree(l))),r=o.findIndex((({id:e})=>e===n)),i=o.findIndex((({id:e})=>e===t)),a=arrayMove(o,i,r),d=a[r-1];let s;const c="onDragEnd"===e?"dropped":"moved",u="onDragEnd"===e?"dropped":"nested";if(d)if(x.depth>d.depth)s=`${t} was ${u} under ${d.id}.`;else{let e=d;for(;e&&x.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[r+1];s=`${t} was ${c} before ${e.id}.`}return s}}}const adjustTranslate=({transform:e})=>({...e,y:e.y-25});