"use client";
import { useCallback, useRef } from "react";
import { useReadonlySet } from "../useReadonlySet.js";
const EMPTY_LIST = [];
/**
 * @see {@link https://react-md.dev/components/Checkbox | Checkbox Demos}
 * @see {@link https://react-md.dev/hooks/use-checkbox-group | useCheckboxGroup Demos}
 * @since 6.0.0
 * @internal
 */ export function useCheckboxGroup(options) {
    const { name, menu = false, values, defaultCheckedValues = EMPTY_LIST } = options;
    const { value: checkedValues, setValue: setCheckedValues, toggleValue } = useReadonlySet({
        defaultValue: 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((checkedValues)=>{
                        if (checkedValues.size === 0 || indeterminate) {
                            return new Set(values);
                        }
                        return new Set();
                    });
                }
            };
        };
    }
    return {
        reset: useCallback(()=>{
            setCheckedValues(initial.current);
        }, [
            setCheckedValues
        ]),
        checkedValues,
        setCheckedValues,
        getIndeterminateProps,
        getCheckboxProps (value) {
            return {
                name,
                value: menu ? undefined : value,
                checked: checkedValues.has(value),
                [menu ? "onCheckedChange" : "onChange"] () {
                    toggleValue(value);
                }
            };
        }
    };
}

//# sourceMappingURL=useCheckboxGroup.js.map