"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)('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 ((0, jsx_runtime_1.jsx)(FieldBase_1.default, { title: inputConfig.title, className: b(null, className), onRefresh: (value) => onUpdate('', value), expandable: true, children: (0, jsx_runtime_1.jsxs)(uikit_1.Card, { 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: anyOfMetaValue, onUpdate: onUpdateAnyOf })) : ((0, jsx_runtime_1.jsx)(uikit_1.Select, { options: inputConfig.options.map((option) => ({
                        content: option.title,
                        value: option.value,
                    })), value: anyOfMetaValue ? [anyOfMetaValue] : [], onUpdate: ([selectValue]) => onUpdateAnyOf(selectValue), className: b('select') })), anyOfChosenOption && ((0, jsx_runtime_1.jsx)(FormGenerator_1.default, { blockConfig: anyOfChosenOption.properties, contentConfig: anyOfContentConfig, onUpdateByKey: onUpdate }))] }) }));
};
exports.default = AnyOfDynamicField;
//# sourceMappingURL=AnyOf.js.map
