"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const uikit_1 = require("@gravity-ui/uikit");
const FormGenerator_1 = tslib_1.__importDefault(require("../../../FormGenerator.js"));
const cn_1 = require("../../../utils/cn.js");
const FieldBase_1 = tslib_1.__importDefault(require("../../FieldBase/FieldBase.js"));
const b = (0, cn_1.formGeneratorCn)('oneof-dynamic-field');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getOneOfContentConfig = (contentConfig, name) => {
    if (name) {
        return contentConfig ? contentConfig[name] : {};
    }
    return contentConfig;
};
const OneOfDynamicField = ({ contentConfig, onUpdate, className, inputConfig, }) => {
    const defaultValue = inputConfig.options[0].value;
    const [oneOfMetaValue, setOneOfMetaValue] = React.useState(defaultValue);
    const oneOfContentConfig = getOneOfContentConfig(contentConfig, inputConfig.name);
    const oneOfChosenOption = React.useMemo(() => inputConfig.options.find(({ value: foundOneOfValue }) => foundOneOfValue === oneOfMetaValue), [inputConfig.options, oneOfMetaValue]);
    const onUpdateOneOf = React.useCallback((value) => {
        setOneOfMetaValue(value);
    }, []);
    return ((0, jsx_runtime_1.jsxs)(FieldBase_1.default, { title: inputConfig.title, className: b(null, className), onRefresh: (value) => onUpdate('', value), expandable: true, children: [inputConfig.options.length < 4 ? ((0, jsx_runtime_1.jsx)(uikit_1.SegmentedRadioGroup, { className: b('radio'), options: inputConfig.options.map((option) => ({
                    content: option.title,
                    value: option.value,
                })), value: oneOfMetaValue, onUpdate: onUpdateOneOf })) : ((0, jsx_runtime_1.jsx)(uikit_1.Select, { options: inputConfig.options.map((option) => ({
                    content: option.title,
                    value: option.value,
                })), value: oneOfMetaValue ? [oneOfMetaValue] : [], onUpdate: ([selectValue]) => onUpdateOneOf(selectValue), className: b('select') })), oneOfChosenOption && ((0, jsx_runtime_1.jsx)(FormGenerator_1.default, { blockConfig: oneOfChosenOption.properties, contentConfig: oneOfContentConfig, onUpdateByKey: onUpdate }))] }));
};
exports.default = OneOfDynamicField;
//# sourceMappingURL=OneOf.js.map
