import { IGenericCheckbox } from "../../../../generic/types";
import { IRWCAnswerComponent, IRWCOption } from '../../../types';
import { RWCAnswerComponent } from "../../../classes/v5";
import { removeBackticks } from "../../../utils";

export const transformCheckboxGroup = ({ props }: IGenericCheckbox): IRWCAnswerComponent => {
    const type = "rwc-checkbox-group";
    const options: Array<IRWCOption> = props.options.map(op => ({
        value: op.value,
        label: op.label
    }));

    const result = {
        options,
        multiple: props.multiple,
        defaultValue: []
    };

    for (const checkbox in props.value) {
        if (props.value[checkbox]) {
            const value = removeBackticks(options.find(({value}) => value === checkbox).label);
            result.defaultValue.push(
                value
            );
        }
    }

    return new RWCAnswerComponent(result, type);
};