"use client";
import { useEffect, useMemo, useRef } from "react";
import { useReadonlySet } from "../useReadonlySet.js";
import { getPartsFromPathname } from "./utils.js";
/**
 * Used to create the `data` prop for the `Navigation` component and handling
 * the expansion of items.
 *
 * @example Main Usage
 * ```tsx
 * "use client";
 * import { Navigation } from "@react-md/core/navigation/Navigation";
 * import { useNavigationExpansion } from "@react-md/core/navigation/useNavigationExpansion";
 * import Link from "next/link";
 * import { usePathname } from "next/navigation.js";
 * import { type ReactElement } from "react";
 *
 * import { navItems } from "./navItems.js";
 *
 * export function Example(): ReactElement {
 *   const pathname = usePathname();
 *   const { data } = useNavigationExpansion({
 *     pathname,
 *     linkComponent: Link,
 *   });
 *
 *   return <Navigation data={data} items={navItems} />;
 * }
 * ```
 *
 * @see {@link https://react-md.dev/getting-started/layout | Layout Demos}
 * @see {@link https://react-md.dev/components/navigation | Navigation Demos}
 * @since 6.0.0
 */ export function useNavigationExpansion(options) {
    const { pathname, linkComponent, defaultExpandedItems, disableFollowingPathname } = options;
    const { value: expandedItems, setValue: setExpandedItems, toggleValue: toggleExpandedItem } = useReadonlySet({
        defaultValue: defaultExpandedItems ?? (()=>new Set(getPartsFromPathname(pathname)))
    });
    const prevPathname = useRef(pathname);
    useEffect(()=>{
        if (disableFollowingPathname || pathname === prevPathname.current) {
            return;
        }
        prevPathname.current = pathname;
        setExpandedItems((prev)=>{
            const next = new Set([
                ...prev,
                ...getPartsFromPathname(pathname)
            ]);
            if (next.size === prev.size) {
                return prev;
            }
            return next;
        });
    }, [
        disableFollowingPathname,
        pathname,
        setExpandedItems
    ]);
    const data = useMemo(()=>({
            pathname,
            linkComponent,
            expandedItems,
            toggleExpandedItem
        }), [
        expandedItems,
        linkComponent,
        pathname,
        toggleExpandedItem
    ]);
    return {
        data,
        expandedItems,
        setExpandedItems,
        toggleExpandedItem
    };
}

//# sourceMappingURL=useNavigationExpansion.js.map