import { jsx as T } from "react/jsx-runtime";
import { useRef as f, useEffect as k, useCallback as h } from "react";
import { Classes as E } from "../../constants/selectors.js";
import { cn as F } from "../../utils/cn.js";
import { TreeLevelProvider as L } from "./context.js";
import { updateRovingTabindex as m, getVisibleTreeItems as R, getTreeItemLabel as C, getSiblingFolders as K, getParentTreeItem as S, getFirstChildTreeItem as H } from "./utils.js";
const M = 500, N = ({ className: v, children: A }) => {
  const n = f(null), i = f(""), a = f(
    null
  );
  k(() => {
    if (!n.current || n.current.querySelector(
      '[role="treeitem"][tabindex="0"]'
    ))
      return;
    const r = n.current.querySelector('[role="treeitem"]');
    r && m(n.current, r);
  }, []), k(() => () => {
    a.current && clearTimeout(a.current);
  }, []);
  const c = h((e) => {
    e && n.current && (m(n.current, e), e.focus());
  }, []), x = h((e) => {
    const r = e.target;
    !(r instanceof HTMLElement) || r.getAttribute("role") !== "treeitem" || n.current && m(n.current, r);
  }, []), D = (e) => {
    const r = e.currentTarget, t = e.target;
    if (!(t instanceof HTMLElement) || t.getAttribute("role") !== "treeitem")
      return;
    const o = R(r), l = o.indexOf(t), d = t.getAttribute("aria-expanded") !== null, p = t.getAttribute("aria-expanded") === "true";
    switch (e.key) {
      case "ArrowDown": {
        e.preventDefault(), c(o[l + 1]);
        break;
      }
      case "ArrowUp": {
        e.preventDefault(), c(o[l - 1]);
        break;
      }
      case "ArrowRight": {
        e.preventDefault(), d && (p ? c(H(t)) : t.click());
        break;
      }
      case "ArrowLeft": {
        e.preventDefault(), d && p ? t.click() : c(S(t, r));
        break;
      }
      case "Home": {
        e.preventDefault(), c(o[0]);
        break;
      }
      case "End": {
        e.preventDefault(), c(o.at(-1));
        break;
      }
      case " ":
      case "Enter": {
        e.preventDefault(), t.click();
        break;
      }
      case "*": {
        e.preventDefault(), K(t, r).forEach((s) => {
          s.getAttribute("aria-expanded") === "false" && s.click();
        });
        break;
      }
      default: {
        if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
          e.preventDefault(), a.current && clearTimeout(a.current), i.current += e.key.toLowerCase();
          const b = i.current, s = l + 1, y = o.slice(s), I = o.slice(0, s), g = [...y, ...I].find((w) => {
            const u = C(w);
            return u == null ? void 0 : u.toLowerCase().startsWith(b);
          });
          g && c(g), a.current = setTimeout(() => {
            i.current = "";
          }, M);
        }
        break;
      }
    }
  };
  return /* @__PURE__ */ T(
    "div",
    {
      "aria-label": "File tree",
      className: F(E.Tree, v),
      "data-component-part": "tree-root",
      onFocus: x,
      onKeyDown: D,
      ref: n,
      role: "tree",
      children: /* @__PURE__ */ T(L, { level: 1, parentId: null, children: A })
    }
  );
};
export {
  N as TreeRoot
};
