(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "../hooks/useStateProps", "./styles"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var useStateProps_1 = require("../hooks/useStateProps");
    var styles_1 = require("./styles");
    var CheckBoxGroup = (0, react_1.forwardRef)(function (_a, ref) {
        var options = _a.options, _b = _a.direction, direction = _b === void 0 ? "column" : _b, className = _a.className, value = _a.value, classNameCheckBox = _a.classNameCheckBox, onChange = _a.onChange, isDisabled = _a.isDisabled, name = _a.name, props = (0, tslib_1.__rest)(_a, ["options", "direction", "className", "value", "classNameCheckBox", "onChange", "isDisabled", "name"]);
        var _c = (0, tslib_1.__read)((0, useStateProps_1.useStateProps)(value), 2), selectedItems = _c[0], setSelectedItems = _c[1];
        var getChecked = function (_value) {
            return (selectedItems === null || selectedItems === void 0 ? void 0 : selectedItems.findIndex(function (v) { return v === _value; })) !== -1;
        };
        var handleChange = function (event, selectedValue) {
            var newValue = [];
            if (getChecked(selectedValue)) {
                newValue = selectedItems === null || selectedItems === void 0 ? void 0 : selectedItems.filter(function (item) { return item !== selectedValue; });
            }
            else {
                newValue = (0, tslib_1.__spreadArray)([], (0, tslib_1.__read)(selectedItems), false);
                newValue.push(selectedValue);
            }
            setSelectedItems(newValue);
            onChange === null || onChange === void 0 ? void 0 : onChange(event, newValue);
        };
        return (react_1.default.createElement(styles_1.CheckBoxGroupStyled, (0, tslib_1.__assign)({}, props, { ref: ref, className: className, direction: direction, "data-element": "checkBox-group" }), options.map(function (option) { return (react_1.default.createElement(styles_1.CheckBoxStyled, { key: option.value, label: option.label, isDisabled: isDisabled || option.isDisabled, isChecked: getChecked(option.value), onChange: function (event) { return handleChange(event, option.value); }, className: classNameCheckBox, name: name })); })));
    });
    CheckBoxGroup.displayName = "CheckBoxGroup";
    exports.default = CheckBoxGroup;
});
