import{arrayMove}from"@dnd-kit/sortable";export const iOS=/iPad|iPhone|iPod/.test(navigator.platform);function getDragDepth(e,r){return Math.round(e/r)}export function getProjection(e,r,n,t,o){const p=e.findIndex((({id:e})=>e===n)),i=e.findIndex((({id:e})=>e===r)),d=e[i],c=arrayMove(e,i,p),h=c[p-1],l=c[p+1],s=getDragDepth(t,o),u=d.depth+s,f=getMaxDepth({previousItem:h}),a=getMinDepth({nextItem:l});let m=u;return u>=f?m=f:u<a&&(m=a),{depth:m,maxDepth:f,minDepth:a,parentId:function(){if(0===m||!h)return null;if(m===h.depth)return h.parentId;if(m>h.depth)return h.id;const e=c.slice(0,p).reverse().find((e=>e.depth===m))?.parentId;return e??null}()}}function getMaxDepth({previousItem:e}){return e?e.module.options&&e.module.options.children?e.depth+1:e.depth:0}function getMinDepth({nextItem:e}){return e?e.depth:0}function flatten(e,r=null,n=0){return e.reduce(((e,t,o)=>[...e,{...t,parentId:r,depth:n,index:o},...flatten(t.props.children?t.props.children:[],t.id,n+1)]),[])}export function flattenTree(e){return flatten(e)}export function buildTree(e){const r={id:"root",props:{children:[]},module:{name:"root",options:{children:!0}}},n={[r.id]:r},t=e.map((e=>({...e,props:{...e.props,children:[]}})));for(const e of t){const{id:o,props:{children:p}}=e,i=e.parentId??r.id,d=n[i]??findItem(t,i);n[o]={...n[o],id:o,props:{...n[o]?.props??{},children:p}},d.props?.children?d.props.children.push(e):d.props.children=[e]}return r.props.children}export function findItem(e,r){return e.find((({id:e})=>e===r))}export function findItemDeep(e,r){for(const n of e){const{id:e,props:{children:t}}=n;if(e===r)return n;if(t&&t.length){const e=findItemDeep(t,r);if(e)return e}}}export function removeItem(e,r){const n=[];for(const t of e)t.id!==r&&(t.props.children&&t.props.children.length&&(t.props.children=removeItem(t.props.children,r)),n.push(t));return n}export function setProperty(e,r,n,t){for(const o of e)o.id!==r?o.props.children&&o.props.children.length&&(o.props.children=setProperty(o.props.children,r,n,t)):o[n]=t(o[n]);return[...e]}function countChildren(e,r=0){return e.reduce(((e,{props:{children:r}})=>r&&r.length?countChildren(r,e+1):e+1),r)}export function getChildCount(e,r){const n=findItemDeep(e,r);return n&&n.props.children?countChildren(n.props.children):0}export function removeChildrenOf(e,r){const n=[...r];return e.filter((e=>!e.parentId||!n.includes(e.parentId)||(e.props.children&&e.props.children.length&&n.push(e.id),!1)))}