"use client";
const require_use_event = require("../../utils/use-event.cjs");
const require_use_controllable_state = require("../../utils/use-controllable-state.cjs");
const require_use_fieldset_context = require("../fieldset/use-fieldset-context.cjs");
//#region src/components/checkbox/use-checkbox-group.ts
function useCheckboxGroup(props = {}) {
	const fieldset = require_use_fieldset_context.useFieldsetContext();
	const { defaultValue, value: controlledValue, onValueChange, disabled = fieldset?.disabled, readOnly, name, invalid = fieldset?.invalid, maxSelectedValues } = props;
	const interactive = !(disabled || readOnly);
	const onChangeProp = require_use_event.useEvent(onValueChange, { sync: true });
	const [value, setValue] = require_use_controllable_state.useControllableState({
		value: controlledValue,
		defaultValue: defaultValue || [],
		onChange: onChangeProp
	});
	const isChecked = (val) => {
		return value.some((v) => String(v) === String(val));
	};
	const toggleValue = (val) => {
		isChecked(val) ? removeValue(val) : addValue(val);
	};
	const isAtMax = maxSelectedValues != null && value.length >= maxSelectedValues;
	const addValue = (val) => {
		if (!interactive) return;
		if (isChecked(val)) return;
		if (isAtMax) return;
		setValue(value.concat(val));
	};
	const removeValue = (val) => {
		if (!interactive) return;
		setValue(value.filter((v) => String(v) !== String(val)));
	};
	const getItemProps = (props) => {
		const checked = props.value != null ? isChecked(props.value) : void 0;
		return {
			checked,
			onCheckedChange() {
				if (props.value != null) toggleValue(props.value);
			},
			name,
			disabled: disabled || isAtMax && !checked,
			readOnly,
			invalid
		};
	};
	return {
		isChecked,
		value,
		name,
		disabled: !!disabled,
		readOnly: !!readOnly,
		invalid: !!invalid,
		setValue,
		addValue,
		toggleValue,
		getItemProps
	};
}
//#endregion
exports.useCheckboxGroup = useCheckboxGroup;
