"use strict";
const jsxRuntime = require("./jsx-runtime-B3vWrP5x.cjs");
const React = require("react");
const Toggle = require("./Toggle-CAW20E9V.cjs");
const Link = require("./Link-B6B7MB7O.cjs");
const css = require("./css-Pf9qxhQ4.cjs");
const aggregator = require("./aggregator-Ca8xiVD2.cjs");
require("./Center-5NJ61qfA.cjs");
const List = require("./List-J5T88ynF.cjs");
const shouldExpand = (node, expanded) => expanded.includes(node.key);
const sortAndSplice = (nodes, sort) => {
  if (sort) {
    nodes.sort((a, b) => a.name.localeCompare(b.name));
  }
  let found = false;
  for (let i = 0; i < nodes.length; i++) {
    const node = nodes[i];
    if (node.forcePosition != null && i !== node.forcePosition) {
      found = true;
      nodes.splice(i, 1);
      nodes.splice(node.forcePosition, 0, node);
    }
  }
  if (found)
    return sortAndSplice(nodes, false);
  return nodes;
};
const flatten = ({
  nodes,
  expanded,
  depth = 0,
  sort = true
}) => {
  if (depth === 0 && sort)
    nodes = nodes.sort((a, b) => a.name.localeCompare(b.name));
  const flattened = [];
  nodes.forEach((node, index) => {
    const expand = shouldExpand(node, expanded);
    flattened.push({ ...node, depth, expanded: expand, index });
    if (expand && node.children != null) {
      node.children = sortAndSplice(node.children, sort);
      flattened.push(
        ...flatten({ nodes: node.children, expanded, depth: depth + 1, sort })
      );
    }
  });
  return flattened;
};
const moveNode = ({ tree, destination, keys }) => {
  keys = aggregator.A(keys);
  keys.forEach((key) => {
    const node = findNode({ tree, key });
    if (node == null)
      return;
    removeNode({ tree, keys: key });
    setNode({ tree, destination, additions: node });
  });
  return tree;
};
const removeNode = ({ tree, keys }) => {
  keys = aggregator.A(keys);
  const treeKeys = tree.map((node) => node.key);
  keys.forEach((key) => {
    var _a;
    const index = treeKeys.indexOf(key);
    if (index !== -1)
      tree.splice(index, 1);
    else {
      const parent = findNodeParent({ tree, key });
      if (parent != null)
        parent.children = (_a = parent.children) == null ? void 0 : _a.filter((child) => child.key !== key);
    }
  });
  return tree;
};
const setNode = ({ tree, destination, additions }) => {
  additions = aggregator.A(additions);
  const node = findNode({ tree, key: destination });
  if (node == null)
    throw new Error(`Could not find node with key ${destination}`);
  if (node.children == null)
    node.children = [];
  const addedKeys = additions.map((node2) => node2.key);
  node.children = [
    ...additions,
    ...node.children.filter((child) => !addedKeys.includes(child.key))
  ];
  return tree;
};
const updateNode = ({
  tree,
  key,
  updater,
  throwOnMissing = true
}) => {
  var _a, _b;
  const node = findNode({ tree, key });
  if (node == null) {
    if (throwOnMissing)
      throw new Error(`Could not find node with key ${key}`);
    return tree;
  }
  const parent = findNodeParent({ tree, key });
  if (parent != null) {
    const index = (_a = parent.children) == null ? void 0 : _a.findIndex((child) => child.key === key);
    if (index != null && index !== -1)
      (_b = parent.children) == null ? void 0 : _b.splice(index, 1, updater(node));
  } else {
    tree.splice(
      tree.findIndex((node2) => node2.key === key),
      1,
      updater(node)
    );
  }
  return tree;
};
const findNode = ({
  tree,
  key,
  depth = 0
}) => {
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.key === key) {
      const n = node;
      n.depth = depth;
      n.position = i;
      return n;
    }
    if (node.children != null) {
      const found = findNode({ tree: node.children, key, depth: depth + 1 });
      if (found != null)
        return found;
    }
  }
  return null;
};
const findNodes = ({ tree, keys }) => {
  const nodes = [];
  for (const key of keys) {
    const node = findNode({ tree, key });
    if (node != null)
      nodes.push(node);
  }
  return nodes;
};
const findNodeParent = ({ tree, key }) => {
  for (const node of tree) {
    if (node.children != null) {
      if (node.children.some((child) => child.key === key))
        return node;
      const found = findNodeParent({ tree: node.children, key });
      if (found != null)
        return found;
    }
  }
  return null;
};
const HAUL_TYPE = "tree-item";
const SHIFT_TRIGGERS = [["Shift"]];
const use = (props) => {
  const {
    onExpand,
    nodes,
    initialExpanded = [],
    sort = true,
    selected: propsSelected,
    onSelectedChange
  } = props ?? {};
  const [expanded, setExpanded, ref] = Toggle.useCombinedStateAndRef(initialExpanded);
  const [selected, setSelected] = aggregator.usePassthrough({
    initial: [],
    value: propsSelected,
    onChange: onSelectedChange
  });
  const flat = React.useMemo(
    () => flatten({ nodes, expanded, sort }),
    [nodes, expanded, sort]
  );
  const flatRef = Toggle.useSyncedRef(flat);
  const shiftRef = Toggle.useHeldRef({ triggers: SHIFT_TRIGGERS });
  const handleSelect = React.useCallback(
    (keys, { clicked }) => {
      setSelected(keys);
      const n = flatRef.current.find((node) => node.key === clicked);
      if ((n == null ? void 0 : n.hasChildren) === false)
        return;
      if (clicked == null || shiftRef.current.held)
        return;
      const currentlyExpanded = ref.current;
      const action = currentlyExpanded.some((key) => key === clicked) ? "contract" : "expand";
      let nextExpanded = currentlyExpanded;
      if (action === "contract")
        nextExpanded = currentlyExpanded.filter((key) => key !== clicked);
      else
        nextExpanded = [...currentlyExpanded, clicked];
      setExpanded(nextExpanded);
      onExpand == null ? void 0 : onExpand({ current: nextExpanded, action, clicked });
    },
    [onExpand, flatRef, setExpanded, setSelected]
  );
  return {
    onSelect: handleSelect,
    selected,
    expanded,
    nodes: flat
  };
};
const expandedCaret = /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(Toggle.m0.Caret.Down, { className: css.CSS.B("caret") });
const collapsedCaret = /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(Toggle.m0.Caret.Right, { className: css.CSS.B("caret") });
const DefaultItem = React.memo(
  ({
    entry,
    selected,
    onSelect,
    onDrop,
    onRename,
    onSuccessfulDrop,
    onDoubleClick,
    loading = false,
    useMargin = false,
    translate
  }) => {
    const {
      key,
      hasChildren = false,
      allowRename = false,
      children,
      icon,
      name,
      depth,
      expanded,
      href,
      haulItems = []
    } = entry;
    const { getSelected } = Toggle.useSelectionUtils();
    const { getSourceData } = Toggle.useDataUtilContext();
    const actuallyHasChildren = hasChildren || children != null && children.length > 0;
    const startIcons = [];
    if (actuallyHasChildren)
      startIcons.push(expanded ? expandedCaret : collapsedCaret);
    if (icon != null)
      startIcons.push(icon);
    const endIcons = [];
    if (loading)
      endIcons.push(/* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(Toggle.m0.Loading, { className: css.CSS.B("loading-indicator") }));
    const [draggingOver, setDraggingOver] = React.useState(false);
    const { startDrag, ...dropProps } = Toggle.useDragAndDrop({
      type: "Tree.Item",
      key,
      canDrop: React.useCallback(({ items: entities, source }) => {
        const keys = entities.map((item) => item.key);
        setDraggingOver(false);
        return source.type === "Tree.Item" && !keys.includes(key);
      }, []),
      onDrop: React.useCallback((props) => (onDrop == null ? void 0 : onDrop(key, props)) ?? [], [key, onDrop]),
      onDragOver: React.useCallback(() => setDraggingOver(true), [])
    });
    const handleDragStart = () => {
      const selectedItemKeys = getSelected();
      const selectedItems = getSourceData().filter(
        (item) => selectedItemKeys.includes(item.key)
      );
      if (selectedItemKeys.includes(key)) {
        const selectedHaulItems = selectedItems.map(({ key: key2, haulItems: haulItems2 }) => [{ type: HAUL_TYPE, key: key2 }, ...haulItems2 ?? []]).flat();
        return startDrag(selectedHaulItems, (props) => onSuccessfulDrop == null ? void 0 : onSuccessfulDrop(key, props));
      }
      startDrag(
        [{ type: HAUL_TYPE, key }, ...haulItems],
        (props) => onSuccessfulDrop == null ? void 0 : onSuccessfulDrop(key, props)
      );
    };
    const offsetKey = useMargin ? "marginLeft" : "paddingLeft";
    const baseProps = {
      id: key,
      variant: "text",
      draggable: true,
      className: css.CSS(
        css.CSS.BE("list", "item"),
        Toggle.CONTEXT_TARGET,
        draggingOver && css.CSS.M("dragging-over"),
        selected && Toggle.CONTEXT_SELECTED,
        css.CSS.selected(selected),
        actuallyHasChildren && css.CSS.M("has-children")
      ),
      onDragLeave: () => setDraggingOver(false),
      onDragStart: handleDragStart,
      onClick: () => onSelect == null ? void 0 : onSelect(key),
      style: {
        position: translate != null ? "absolute" : "relative",
        transform: `translateY(${translate}px)`,
        [offsetKey]: `${depth * 1.5 + 1}rem`
      },
      startIcon: startIcons,
      iconSpacing: "small",
      noWrap: true,
      endIcon: endIcons,
      onDoubleClick: (e) => onDoubleClick == null ? void 0 : onDoubleClick(key, e),
      href,
      ...dropProps
    };
    const Base = href != null ? Link.Link : Toggle.Button;
    return /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(Base, { className: css.CSS.BE("list", "item"), ...baseProps, children: /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(
      Toggle.MaybeEditable,
      {
        id: `text-${key}`,
        level: "p",
        allowDoubleClick: false,
        value: name,
        disabled: !allowRename,
        onChange: (name2) => onRename == null ? void 0 : onRename(key, name2)
      }
    ) });
  }
);
DefaultItem.displayName = "Tree.Item";
const defaultChild = Toggle.componentRenderProp(DefaultItem);
const Tree = ({
  nodes,
  selected = [],
  onSelect,
  onDrop,
  onRename,
  onSuccessfulDrop,
  onDoubleClick,
  className,
  children = defaultChild,
  itemHeight = 27,
  useMargin = false,
  virtual = true,
  ...props
}) => {
  const Core = virtual ? Toggle.Core.Virtual : Toggle.Core;
  return /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(List.List, { data: nodes, children: /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(Toggle.Selector, { value: selected, onChange: onSelect, allowMultiple: true, replaceOnSingle: true, children: /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(
    Core,
    {
      itemHeight,
      className: css.CSS(className, css.CSS.B("tree")),
      ...props,
      children: (props2) => children({
        ...props2,
        useMargin,
        onDrop,
        onRename,
        onSuccessfulDrop,
        onDoubleClick
      })
    }
  ) }) });
};
const startRenaming = (key) => Toggle.edit(`text-${key}`);
const external = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  DefaultItem,
  HAUL_TYPE,
  Tree,
  findNode,
  findNodeParent,
  findNodes,
  flatten,
  moveNode,
  removeNode,
  setNode,
  shouldExpand,
  sortAndSplice,
  startRenaming,
  updateNode,
  use
}, Symbol.toStringTag, { value: "Module" }));
exports.external = external;
//# sourceMappingURL=external-DL9-TJzC.cjs.map
