"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const utils_external_1 = require("../../utils-external");
const consoleWarning_1 = require("../../utils/helpers/consoleWarning");
const useFormField_1 = require("../useFormField");
const CheckboxGroup_context_1 = require("./CheckboxGroup.context");
/**
 * Handles props for Checkboxes in context with Fieldset and CheckboxGroup
 */
const useCheckbox = (props) => {
    const checkboxGroup = (0, react_1.useContext)(CheckboxGroup_context_1.CheckboxGroupContext);
    const _a = (0, useFormField_1.useFormField)((0, utils_external_1.omit)(props, ["description", "children"]), "checkbox"), { inputProps, readOnly } = _a, rest = __rest(_a, ["inputProps", "readOnly"]);
    if (checkboxGroup) {
        if (props.checked) {
            (0, consoleWarning_1.consoleWarning)("`checked` is unsupported on <Checkbox> elements within a <CheckboxGroup>. Please set a `value` or `defaultValue` on <CheckboxGroup> instead.");
        }
        if (props.value === undefined) {
            (0, consoleWarning_1.consoleWarning)("A <Checkbox> element within a <CheckboxGroup> requires a `value` property.");
        }
    }
    return Object.assign(Object.assign({}, rest), { readOnly, nested: !!checkboxGroup, inputProps: Object.assign(Object.assign({}, inputProps), { checked: (checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.value)
                ? checkboxGroup.value.includes(props.value)
                : props.checked, defaultChecked: (checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.defaultValue)
                ? checkboxGroup.defaultValue.includes(props.value)
                : props.defaultChecked, onChange: (event) => {
                var _a;
                if (readOnly) {
                    return;
                }
                (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, event);
                checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.toggleValue(props.value);
            }, onClick: (event) => {
                var _a;
                if (readOnly) {
                    event.preventDefault();
                    return;
                }
                (_a = props === null || props === void 0 ? void 0 : props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, event);
            } }) });
};
exports.default = useCheckbox;
//# sourceMappingURL=useCheckbox.js.map