import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { SegmentedRadioGroup, Select } from '@gravity-ui/uikit';
import DynamicForm from "../../../FormGenerator.js";
import { formGeneratorCn } from "../../../utils/cn.js";
import FieldBase from "../../FieldBase/FieldBase.js";
import './OneOf.css';
const b = 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 (_jsxs(FieldBase, { title: inputConfig.title, className: b(null, className), onRefresh: (value) => onUpdate('', value), expandable: true, children: [inputConfig.options.length < 4 ? (_jsx(SegmentedRadioGroup, { className: b('radio'), options: inputConfig.options.map((option) => ({
                    content: option.title,
                    value: option.value,
                })), value: oneOfMetaValue, onUpdate: onUpdateOneOf })) : (_jsx(Select, { options: inputConfig.options.map((option) => ({
                    content: option.title,
                    value: option.value,
                })), value: oneOfMetaValue ? [oneOfMetaValue] : [], onUpdate: ([selectValue]) => onUpdateOneOf(selectValue), className: b('select') })), oneOfChosenOption && (_jsx(DynamicForm, { blockConfig: oneOfChosenOption.properties, contentConfig: oneOfContentConfig, onUpdateByKey: onUpdate }))] }));
};
export default OneOfDynamicField;
//# sourceMappingURL=OneOf.js.map
