"use client";
import { useCallback, useRef, useState } from "react";
const EMPTY_LIST = [];
export function useCheckboxGroup(options) {
    const { name, menu = false, values, defaultCheckedValues = EMPTY_LIST } = options;
    const [checkedValues, setCheckedValues] = useState(()=>{
        if (typeof defaultCheckedValues === "function") {
            return new Set(defaultCheckedValues());
        }
        return new Set(defaultCheckedValues);
    });
    const initial = useRef(checkedValues);
    let getIndeterminateProps;
    if (values) {
        getIndeterminateProps = ()=>{
            const checked = checkedValues.size > 0;
            const indeterminate = checked && checkedValues.size < values.length;
            return {
                "aria-checked": indeterminate ? "mixed" : undefined,
                name,
                checked,
                indeterminate,
                [menu ? "onCheckedChange" : "onChange"] () {
                    setCheckedValues(()=>{
                        if (checkedValues.size === 0 || indeterminate) {
                            return new Set(values);
                        }
                        return new Set();
                    });
                }
            };
        };
    }
    return {
        reset: useCallback(()=>{
            setCheckedValues(initial.current);
        }, []),
        checkedValues,
        setCheckedValues,
        getIndeterminateProps,
        getCheckboxProps (value) {
            return {
                name,
                value: menu ? undefined : value,
                checked: checkedValues.has(value),
                [menu ? "onCheckedChange" : "onChange"] () {
                    setCheckedValues((prevValues)=>{
                        const nextValues = new Set(prevValues);
                        if (prevValues.has(value)) {
                            nextValues.delete(value);
                        } else {
                            nextValues.add(value);
                        }
                        return nextValues;
                    });
                }
            };
        }
    };
}

//# sourceMappingURL=useCheckboxGroup.js.map