"use client";
const require_use_props = require("../../../core/MantineProvider/use-props/use-props.cjs");
const require_factory = require("../../../core/factory/factory.cjs");
const require_Input = require("../../Input/Input.cjs");
const require_InputsGroupFieldset = require("../../../utils/InputsGroupFieldset/InputsGroupFieldset.cjs");
let react = require("react");
let _mantine_hooks = require("@mantine/hooks");
let react_jsx_runtime = require("react/jsx-runtime");
//#region packages/@mantine/core/src/components/Checkbox/CheckboxGroup/CheckboxGroup.tsx
const CheckboxGroupContext = (0, react.createContext)(null);
const defaultProps = { hiddenInputValuesSeparator: "," };
const CheckboxGroup = require_factory.genericFactory(((props) => {
	const { value, defaultValue, onChange, size, wrapperProps, children, readOnly, name, hiddenInputValuesSeparator, hiddenInputProps, maxSelectedValues, disabled, ...others } = require_use_props.useProps("CheckboxGroup", defaultProps, props);
	const [_value, setValue] = (0, _mantine_hooks.useUncontrolled)({
		value,
		defaultValue,
		finalValue: [],
		onChange
	});
	const handleChange = (event) => {
		const itemValue = typeof event === "string" ? event : event.currentTarget.value;
		if (readOnly) return;
		const isCurrentlySelected = _value.includes(itemValue);
		if (!isCurrentlySelected && maxSelectedValues && _value.length >= maxSelectedValues) return;
		setValue(isCurrentlySelected ? _value.filter((item) => item !== itemValue) : [..._value, itemValue]);
	};
	const isDisabled = (checkboxValue) => {
		if (disabled) return true;
		if (!maxSelectedValues) return false;
		const isCurrentlySelected = _value.includes(checkboxValue);
		const hasReachedLimit = _value.length >= maxSelectedValues;
		return !isCurrentlySelected && hasReachedLimit;
	};
	const hiddenInputValue = _value.join(hiddenInputValuesSeparator);
	return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CheckboxGroupContext, {
		value: {
			value: _value,
			onChange: handleChange,
			size,
			isDisabled
		},
		children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(require_Input.Input.Wrapper, {
			size,
			...wrapperProps,
			...others,
			labelElement: "div",
			__staticSelector: "CheckboxGroup",
			children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_InputsGroupFieldset.InputsGroupFieldset, {
				role: "group",
				children
			}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
				type: "hidden",
				name,
				value: hiddenInputValue,
				...hiddenInputProps
			})]
		})
	});
}));
CheckboxGroup.classes = require_Input.Input.Wrapper.classes;
CheckboxGroup.displayName = "@mantine/core/CheckboxGroup";
//#endregion
exports.CheckboxGroup = CheckboxGroup;
exports.CheckboxGroupContext = CheckboxGroupContext;

//# sourceMappingURL=CheckboxGroup.cjs.map