import { __assign } from '../../node_modules/tslib/tslib.es6.js';
import React, { forwardRef } from 'react';
import FormControlSection from '../formControlSection/index.js';
import Flex from '../flex/index.js';
import { RadioGroup, Divider } from '@chakra-ui/react';
import Radio from './index.js';

var QuestionWithFollowUp = forwardRef(function (_a, ref) {
    var name = _a.name, values = _a.values, onChange = _a.onChange, options = _a.options, groupLabel = _a.groupLabel, tooltip = _a.tooltip, id = _a.id, errorMessage = _a.errorMessage, hint = _a.hint, followUps = _a.followUps, recurring = _a.recurring;
    var currentValue = values[id];
    var handleChange = function (groupId, value) {
        var _a;
        var newValues = __assign(__assign({}, values), (_a = {}, _a[groupId] = value, _a));
        if (followUps) {
            for (var _i = 0, _b = Object.entries(followUps); _i < _b.length; _i++) {
                var _c = _b[_i], key = _c[0], followUp = _c[1];
                if (!followUp)
                    continue;
                if (key !== value && followUp.id in newValues) {
                    delete newValues[followUp.id];
                }
            }
        }
        onChange(newValues);
    };
    return (React.createElement(FormControlSection, { id: id, errorMessage: errorMessage, label: groupLabel, hint: hint, tooltip: tooltip, recurring: recurring },
        React.createElement(RadioGroup, { value: currentValue || '', onChange: function (value) { return handleChange(id, value); } },
            React.createElement(Flex, { flexDirection: "column" },
                options.map(function (_a) {
                    var label = _a.label, value = _a.value;
                    return (React.createElement(Radio, { key: value, name: name, value: value, isChecked: value === currentValue, ref: ref, label: label, isInvalid: false }));
                }),
                (followUps === null || followUps === void 0 ? void 0 : followUps[currentValue]) ? (React.createElement(React.Fragment, null,
                    React.createElement(Divider, { marginY: "xl" }),
                    React.createElement(QuestionWithFollowUp, __assign({}, followUps[currentValue], { values: values, onChange: onChange, recurring: true })))) : null))));
});
Radio.displayName = 'QuestionWithFollowUp';

export { QuestionWithFollowUp as default };
//# sourceMappingURL=QuestionWithFollowUp.js.map
