import CheckboxElement from "../Checkbox/Checkbox";
import React, { useEffect } from "react";
export const enumToCategoryCheckboxOption = (obj) => {
    const options = [];
    for (const key in obj) {
        options.push({
            label: obj[key].toString(),
            value: obj[key].toString(),
        });
    }
    return options;
};
const CheckBoxList = (props) => {
    const [currentValues, setCurrentValues] = React.useState(props.initialValue || []);
    useEffect(() => {
        setCurrentValues(props.initialValue || []);
    }, [props.initialValue]);
    return (React.createElement("div", null, props.options.map((option, i) => {
        return (React.createElement(CheckboxElement, { key: i, title: option.label, value: Boolean((currentValues === null || currentValues === void 0 ? void 0 : currentValues.find((value) => {
                return value === option.value;
            })) || false), onChange: (changedValue) => {
                if (changedValue) {
                    if (props.onChecked) {
                        props.onChecked(option.value);
                    }
                    // add the option.value to the currentValues array
                    const newValues = [
                        ...currentValues,
                    ];
                    if (newValues.includes(option.value)) {
                        return;
                    }
                    newValues.push(option.value);
                    setCurrentValues(newValues);
                    props.onChange(newValues);
                }
                else {
                    if (props.onUnchecked) {
                        props.onUnchecked(option.value);
                    }
                    // remove the option.value from the currentValues array
                    const newValues = [
                        ...currentValues,
                    ];
                    while (newValues.includes(option.value)) {
                        const index = newValues.findIndex((value) => {
                            return value === option.value;
                        });
                        newValues.splice(index, 1);
                    }
                    setCurrentValues(newValues);
                    props.onChange(newValues);
                }
            } }));
    })));
};
export default CheckBoxList;
//# sourceMappingURL=CheckboxList.js.map