"use client";
import { usePathname, useRouter } from "next/navigation";
import { useState, useCallback, useMemo, useEffect, useRef } from "react";
function useTreeNavigation(options) {
  const { basePath, autoExpandPath = true, onPathChange } = options;
  const pathname = usePathname();
  const router = useRouter();
  const prevPathnameRef = useRef(pathname);
  const selectedPath = useMemo(() => {
    if (!pathname.startsWith(basePath)) return null;
    const relativePath = pathname.slice(basePath.length);
    const cleaned = relativePath.replace(/^\//, "");
    return cleaned || null;
  }, [pathname, basePath]);
  const autoExpandedFolders = useMemo(() => {
    if (!selectedPath || !autoExpandPath) return /* @__PURE__ */ new Set();
    const folders = /* @__PURE__ */ new Set();
    const segments = selectedPath.split("/");
    for (let i = 1; i < segments.length; i++) {
      folders.add(segments.slice(0, i).join("/"));
    }
    return folders;
  }, [selectedPath, autoExpandPath]);
  const [manualExpansions, setManualExpansions] = useState(/* @__PURE__ */ new Set());
  const [manualCollapses, setManualCollapses] = useState(/* @__PURE__ */ new Set());
  const expandedFolders = useMemo(() => {
    const result = /* @__PURE__ */ new Set();
    for (const folder of autoExpandedFolders) {
      if (!manualCollapses.has(folder)) {
        result.add(folder);
      }
    }
    for (const folder of manualExpansions) {
      result.add(folder);
    }
    return result;
  }, [autoExpandedFolders, manualExpansions, manualCollapses]);
  useEffect(() => {
    if (prevPathnameRef.current !== pathname) {
      prevPathnameRef.current = pathname;
      setManualCollapses(/* @__PURE__ */ new Set());
      onPathChange == null ? void 0 : onPathChange(selectedPath);
    }
  }, [pathname, selectedPath, onPathChange]);
  const navigateToFile = useCallback((path) => {
    const newUrl = `${basePath}/${path}`;
    router.push(newUrl);
  }, [basePath, router]);
  const clearSelection = useCallback(() => {
    router.push(basePath);
  }, [basePath, router]);
  const toggleFolder = useCallback((path) => {
    const isCurrentlyExpanded = expandedFolders.has(path);
    if (isCurrentlyExpanded) {
      if (autoExpandedFolders.has(path)) {
        setManualCollapses((prev) => /* @__PURE__ */ new Set([...prev, path]));
      } else {
        setManualExpansions((prev) => {
          const next = new Set(prev);
          next.delete(path);
          return next;
        });
      }
    } else {
      setManualCollapses((prev) => {
        const next = new Set(prev);
        next.delete(path);
        return next;
      });
      if (!autoExpandedFolders.has(path)) {
        setManualExpansions((prev) => /* @__PURE__ */ new Set([...prev, path]));
      }
    }
  }, [expandedFolders, autoExpandedFolders]);
  const expandFolder = useCallback((path) => {
    setManualCollapses((prev) => {
      const next = new Set(prev);
      next.delete(path);
      return next;
    });
    if (!autoExpandedFolders.has(path)) {
      setManualExpansions((prev) => /* @__PURE__ */ new Set([...prev, path]));
    }
  }, [autoExpandedFolders]);
  const collapseFolder = useCallback((path) => {
    if (autoExpandedFolders.has(path)) {
      setManualCollapses((prev) => /* @__PURE__ */ new Set([...prev, path]));
    } else {
      setManualExpansions((prev) => {
        const next = new Set(prev);
        next.delete(path);
        return next;
      });
    }
  }, [autoExpandedFolders]);
  const isPathInSelection = useCallback((path) => {
    if (!selectedPath) return false;
    return selectedPath === path || selectedPath.startsWith(path + "/");
  }, [selectedPath]);
  return {
    selectedPath,
    expandedFolders,
    navigateToFile,
    toggleFolder,
    expandFolder,
    collapseFolder,
    isPathInSelection,
    clearSelection
  };
}
export {
  useTreeNavigation
};
