import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { Card, SegmentedRadioGroup, Select } from '@gravity-ui/uikit';
import DynamicForm from "../../../FormGenerator.js";
import { formGeneratorCn } from "../../../utils/cn.js";
import FieldBase from "../../FieldBase/FieldBase.js";
import './AnyOf.css';
const b = formGeneratorCn('anyof-dynamic-field');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getAnyOfContentConfig = (contentConfig, name) => {
    if (name) {
        return contentConfig ? contentConfig[name] : {};
    }
    return contentConfig;
};
const AnyOfDynamicField = ({ contentConfig, onUpdate, className, inputConfig, }) => {
    const defaultValue = inputConfig.options[0].value;
    const [anyOfMetaValue, setAnyOfMetaValue] = React.useState(defaultValue);
    const anyOfContentConfig = getAnyOfContentConfig(contentConfig, inputConfig.name);
    const anyOfChosenOption = React.useMemo(() => inputConfig.options.find(({ value: foundAnyOfValue }) => foundAnyOfValue === anyOfMetaValue), [inputConfig.options, anyOfMetaValue]);
    const onUpdateAnyOf = React.useCallback((value) => {
        setAnyOfMetaValue(value);
    }, []);
    return (_jsx(FieldBase, { title: inputConfig.title, className: b(null, className), onRefresh: (value) => onUpdate('', value), expandable: true, children: _jsxs(Card, { children: [inputConfig.options.length < 4 ? (_jsx(SegmentedRadioGroup, { className: b('radio'), options: inputConfig.options.map((option) => ({
                        content: option.title,
                        value: option.value,
                    })), value: anyOfMetaValue, onUpdate: onUpdateAnyOf })) : (_jsx(Select, { options: inputConfig.options.map((option) => ({
                        content: option.title,
                        value: option.value,
                    })), value: anyOfMetaValue ? [anyOfMetaValue] : [], onUpdate: ([selectValue]) => onUpdateAnyOf(selectValue), className: b('select') })), anyOfChosenOption && (_jsx(DynamicForm, { blockConfig: anyOfChosenOption.properties, contentConfig: anyOfContentConfig, onUpdateByKey: onUpdate }))] }) }));
};
export default AnyOfDynamicField;
//# sourceMappingURL=AnyOf.js.map
