"use client";
import { useCallback, useState } from "react";
/**
 * The `useReadonlySet` hook is used to create a more performant lookup
 * behavior for lists/arrays. This is most likely an internal only hook to
 * manage state for a `ReadonlySet`. You most likely want to use one of the
 * other hooks that leverage this instead:
 *
 * - `useCheckboxGroup`
 * - `useExpansionPanels`
 * - `useTreeSelection`
 * - `useTreeExpansion`
 * - etc
 *
 * @example Simple Example
 * ```tsx
 * import { cnb } from "cnbuilder";
 * import { useReadonlySet } from "@react-md/core/useReadonlySet";
 *
 * function Example() {
 *   const { value, toggleValue } = useReadonlySet();
 *
 *   return (
 *     <>
 *       {someList.map((item) => (
 *         <div key={item.id} className={cnb(value.has(item.id) && styles.selected)}>
 *           {item.name}
 *           <Button onClick={() => toggleValue(item.id)}>Button</Button>
 *         </div>
 *       ))}
 *     </>
 *   );
 * }
 * ```
 * @since 6.0.0
 */ export function useReadonlySet(options = {}) {
    const { defaultValue, toggleType = "multiple" } = options;
    const [value, setValue] = useState(()=>{
        const initial = defaultValue instanceof Function ? defaultValue() : defaultValue ?? [];
        return new Set(initial);
    });
    const toggleValue = useCallback((item)=>{
        setValue((prevValue)=>{
            const exists = prevValue.has(item);
            if (toggleType === "single") {
                return new Set(exists ? [] : [
                    item
                ]);
            }
            if (toggleType === "single-select") {
                return exists ? prevValue : new Set([
                    item
                ]);
            }
            const nextValue = new Set(prevValue);
            if (exists) {
                nextValue.delete(item);
            } else {
                nextValue.add(item);
            }
            return nextValue;
        });
    }, [
        toggleType
    ]);
    return {
        value,
        setValue,
        toggleValue
    };
}

//# sourceMappingURL=useReadonlySet.js.map