var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellDraggable/utilities.ts
var utilities_exports = {};
__export(utilities_exports, {
  buildTree: () => buildTree,
  findItem: () => findItem,
  findItemDeep: () => findItemDeep,
  flattenTree: () => flattenTree,
  getChildCount: () => getChildCount,
  getProjection: () => getProjection,
  iOS: () => iOS,
  removeChildrenOf: () => removeChildrenOf,
  removeItem: () => removeItem,
  setProperty: () => setProperty
});
module.exports = __toCommonJS(utilities_exports);
var import_sortable = require("@dnd-kit/sortable");
var iOS = /iPad|iPhone|iPod/.test(navigator.platform);
function getDragDepth(offset, indentationWidth) {
  return Math.round(offset / indentationWidth);
}
function getProjection(items, activeId, overId, dragOffset, indentationWidth) {
  const overItemIndex = items.findIndex(({ id }) => id === overId);
  const activeItemIndex = items.findIndex(({ id }) => id === activeId);
  const activeItem = items[activeItemIndex];
  const newItems = (0, import_sortable.arrayMove)(items, activeItemIndex, overItemIndex);
  const previousItem = newItems[overItemIndex - 1];
  const nextItem = newItems[overItemIndex + 1];
  const dragDepth = getDragDepth(dragOffset, indentationWidth);
  const projectedDepth = activeItem.depth + dragDepth;
  const maxDepth = getMaxDepth({
    previousItem
  });
  const minDepth = getMinDepth({ nextItem });
  let depth = projectedDepth;
  if (projectedDepth >= maxDepth) {
    depth = maxDepth;
  } else if (projectedDepth < minDepth) {
    depth = minDepth;
  }
  return { depth, maxDepth, minDepth, parentId: getParentId() };
  function getParentId() {
    var _a;
    if (depth === 0 || !previousItem) {
      return null;
    }
    if (depth === previousItem.depth) {
      return previousItem.parentId;
    }
    if (depth > previousItem.depth) {
      return previousItem.id;
    }
    const newParent = (_a = newItems.slice(0, overItemIndex).reverse().find((item) => item.depth === depth)) == null ? void 0 : _a.parentId;
    return newParent ?? null;
  }
}
function getMaxDepth({ previousItem }) {
  if (previousItem) {
    return previousItem.depth + 1;
  }
  return 0;
}
function getMinDepth({ nextItem }) {
  if (nextItem) {
    return nextItem.depth;
  }
  return 0;
}
function flatten(items, parentId = null, depth = 0) {
  return items.reduce((acc, item, index) => {
    return [
      ...acc,
      { ...item, parentId, depth, index },
      ...flatten((item == null ? void 0 : item.children) ?? [], item.id, depth + 1)
    ];
  }, []);
}
function flattenTree(items) {
  return flatten(items);
}
function buildTree(flattenedItems) {
  const root = { id: "root", children: [] };
  const nodes = { [root.id]: root };
  const items = flattenedItems.map((item) => ({ ...item, children: [] }));
  for (const item of items) {
    const { id, children } = item;
    const parentId = item.parentId ?? root.id;
    const parent = nodes[parentId] ?? findItem(items, parentId);
    nodes[id] = { id, children };
    parent.children.push(item);
  }
  return root.children;
}
function findItem(items, itemId) {
  return items.find(({ id }) => id === itemId);
}
function findItemDeep(items, itemId) {
  for (const item of items) {
    const { id, children } = item;
    if (id === itemId) {
      return item;
    }
    if (children == null ? void 0 : children.length) {
      const child = findItemDeep(children, itemId);
      if (child) {
        return child;
      }
    }
  }
  return void 0;
}
function removeItem(items, id) {
  var _a;
  const newItems = [];
  for (const item of items) {
    if (item.id === id) {
      continue;
    }
    if ((_a = item.children) == null ? void 0 : _a.length) {
      item.children = removeItem(item.children, id);
    }
    newItems.push(item);
  }
  return newItems;
}
function setProperty(items, id, property, setter) {
  var _a;
  for (const item of items) {
    if (item.id === id) {
      item[property] = setter(item[property]);
      continue;
    }
    if ((_a = item.children) == null ? void 0 : _a.length) {
      item.children = setProperty(item.children, id, property, setter);
    }
  }
  return [...items];
}
function countChildren(items, count = 0) {
  return items.reduce((acc, { children }) => {
    if (children == null ? void 0 : children.length) {
      return countChildren(children, acc + 1);
    }
    return acc + 1;
  }, count);
}
function getChildCount(items, id) {
  const item = findItemDeep(items, id);
  return item ? countChildren(item.children) : 0;
}
function removeChildrenOf(items, ids) {
  const excludeParentIds = [...ids];
  return items.filter((item) => {
    var _a;
    if (item.parentId && excludeParentIds.includes(item.parentId)) {
      if ((_a = item.children) == null ? void 0 : _a.length) {
        excludeParentIds.push(item.id);
      }
      return false;
    }
    return true;
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildTree,
  findItem,
  findItemDeep,
  flattenTree,
  getChildCount,
  getProjection,
  iOS,
  removeChildrenOf,
  removeItem,
  setProperty
});
