function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import { arrayMove } from '@dnd-kit/sortable';
export var iOS = /iPad|iPhone|iPod/.test(navigator.platform);
function getDragDepth(offset, indentationWidth) {
  return Math.round(offset / indentationWidth);
}
export function getProjection(items, activeId, overId, dragOffset, indentationWidth) {
  var overItemIndex = items.findIndex(function (_ref) {
    var id = _ref.id;
    return id === overId;
  });
  var activeItemIndex = items.findIndex(function (_ref2) {
    var id = _ref2.id;
    return id === activeId;
  });
  var activeItem = items[activeItemIndex];
  var newItems = arrayMove(items, activeItemIndex, overItemIndex);
  var previousItem = newItems[overItemIndex - 1];
  var nextItem = newItems[overItemIndex + 1];
  var dragDepth = getDragDepth(dragOffset, indentationWidth);
  var projectedDepth = activeItem.depth + dragDepth;
  var maxDepth = getMaxDepth({
    previousItem: previousItem
  });
  var minDepth = getMinDepth({
    nextItem: nextItem
  });
  var depth = projectedDepth;
  if (projectedDepth >= maxDepth) {
    depth = maxDepth;
  } else if (projectedDepth < minDepth) {
    depth = minDepth;
  }
  return {
    depth: depth,
    maxDepth: maxDepth,
    minDepth: minDepth,
    parentId: getParentId()
  };
  function getParentId() {
    var _newItems$slice$rever;
    if (depth === 0 || !previousItem) {
      return null;
    }
    if (depth === previousItem.depth) {
      return previousItem.parentId;
    }
    if (depth > previousItem.depth) {
      return previousItem.id;
    }
    var newParent = (_newItems$slice$rever = newItems.slice(0, overItemIndex).reverse().find(function (item) {
      return item.depth === depth;
    })) === null || _newItems$slice$rever === void 0 ? void 0 : _newItems$slice$rever.parentId;
    return newParent !== null && newParent !== void 0 ? newParent : null;
  }
}
function getMaxDepth(_ref3) {
  var previousItem = _ref3.previousItem;
  if (previousItem) {
    return previousItem.depth + 1;
  }
  return 0;
}
function getMinDepth(_ref4) {
  var nextItem = _ref4.nextItem;
  if (nextItem) {
    return nextItem.depth;
  }
  return 0;
}
function flatten(items) {
  var parentId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  return items.reduce(function (acc, item, index) {
    var _item$children;
    return [].concat(_toConsumableArray(acc), [_objectSpread(_objectSpread({}, item), {}, {
      parentId: parentId,
      depth: depth,
      index: index
    })], _toConsumableArray(flatten((_item$children = item === null || item === void 0 ? void 0 : item.children) !== null && _item$children !== void 0 ? _item$children : [], item.id, depth + 1)));
  }, []);
}
export function flattenTree(items) {
  return flatten(items);
}
export function buildTree(flattenedItems) {
  var root = {
    id: 'root',
    children: []
  };
  var nodes = _defineProperty({}, root.id, root);
  var items = flattenedItems.map(function (item) {
    return _objectSpread(_objectSpread({}, item), {}, {
      children: []
    });
  });
  var _iterator = _createForOfIteratorHelper(items),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _item$parentId, _nodes$parentId;
      var item = _step.value;
      var id = item.id,
        children = item.children;
      var parentId = (_item$parentId = item.parentId) !== null && _item$parentId !== void 0 ? _item$parentId : root.id;
      var parent = (_nodes$parentId = nodes[parentId]) !== null && _nodes$parentId !== void 0 ? _nodes$parentId : findItem(items, parentId);
      nodes[id] = {
        id: id,
        children: children
      };
      parent.children.push(item);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return root.children;
}
export function findItem(items, itemId) {
  return items.find(function (_ref5) {
    var id = _ref5.id;
    return id === itemId;
  });
}
export function findItemDeep(items, itemId) {
  var _iterator2 = _createForOfIteratorHelper(items),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var item = _step2.value;
      var id = item.id,
        children = item.children;
      if (id === itemId) {
        return item;
      }
      if (children !== null && children !== void 0 && children.length) {
        var child = findItemDeep(children, itemId);
        if (child) {
          return child;
        }
      }
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  return undefined;
}
export function removeItem(items, id) {
  var newItems = [];
  var _iterator3 = _createForOfIteratorHelper(items),
    _step3;
  try {
    for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
      var _item$children2;
      var item = _step3.value;
      if (item.id === id) {
        continue;
      }
      if ((_item$children2 = item.children) !== null && _item$children2 !== void 0 && _item$children2.length) {
        item.children = removeItem(item.children, id);
      }
      newItems.push(item);
    }
  } catch (err) {
    _iterator3.e(err);
  } finally {
    _iterator3.f();
  }
  return newItems;
}
export function setProperty(items, id, property, setter) {
  var _iterator4 = _createForOfIteratorHelper(items),
    _step4;
  try {
    for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
      var _item$children3;
      var item = _step4.value;
      if (item.id === id) {
        item[property] = setter(item[property]);
        continue;
      }
      if ((_item$children3 = item.children) !== null && _item$children3 !== void 0 && _item$children3.length) {
        item.children = setProperty(item.children, id, property, setter);
      }
    }
  } catch (err) {
    _iterator4.e(err);
  } finally {
    _iterator4.f();
  }
  return _toConsumableArray(items);
}
function countChildren(items) {
  var count = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  return items.reduce(function (acc, _ref6) {
    var children = _ref6.children;
    if (children !== null && children !== void 0 && children.length) {
      return countChildren(children, acc + 1);
    }
    return acc + 1;
  }, count);
}
export function getChildCount(items, id) {
  var item = findItemDeep(items, id);
  return item ? countChildren(item.children) : 0;
}
export function removeChildrenOf(items, ids) {
  var excludeParentIds = _toConsumableArray(ids);
  return items.filter(function (item) {
    if (item.parentId && excludeParentIds.includes(item.parentId)) {
      var _item$children4;
      if ((_item$children4 = item.children) !== null && _item$children4 !== void 0 && _item$children4.length) {
        excludeParentIds.push(item.id);
      }
      return false;
    }
    return true;
  });
}