"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldCard = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const uikit_1 = require("@gravity-ui/uikit");
const FormContext_1 = require("../../hooks/FormContext.js");
const cn_1 = require("../../utils/cn.js");
const ArrayFieldRenderer_1 = require("../ArrayFieldRenderer/ArrayFieldRenderer.js");
const ConfigRow_1 = require("../ConfigRow/ConfigRow.js");
const FieldHeader_1 = require("../FieldHeader/FieldHeader.js");
const ObjectFieldRenderer_1 = require("../ObjectFieldRenderer/ObjectFieldRenderer.js");
const OptionsRenderer_1 = require("../OptionsRenderer/OptionsRenderer.js");
const SelectFieldRenderer_1 = require("../SelectFieldRenderer/SelectFieldRenderer.js");
const b = (0, cn_1.formBuilderCn)('field-card');
const FieldCard = ({ field, inputTypeMenuItems }) => {
    const { removeField, updateField, removeObjectProperty, removeOptionProperty, updateObjectProperty, updateOptionProperty, } = (0, FormContext_1.useFormContext)();
    const renderNestedField = (nestedField, index, parentId, optionIndex) => {
        const isInOption = optionIndex !== undefined;
        const handleRemove = () => {
            if (isInOption) {
                removeOptionProperty(parentId, optionIndex, index);
            }
            else {
                removeObjectProperty(parentId, index);
            }
        };
        const handleUpdate = (updates) => {
            if (isInOption) {
                updateOptionProperty(parentId, optionIndex, index, updates);
            }
            else {
                updateObjectProperty(parentId, index, updates);
            }
        };
        return ((0, jsx_runtime_1.jsxs)("div", { className: b('nested-field'), children: [(0, jsx_runtime_1.jsx)(FieldHeader_1.FieldHeader, { title: nestedField.type.toUpperCase(), onRemove: handleRemove, variant: "subheader-3", buttonSize: "xs" }), (0, jsx_runtime_1.jsxs)("div", { className: b('config'), children: [(0, jsx_runtime_1.jsx)(ConfigRow_1.ConfigRow, { label: "Name", value: nestedField.name, onUpdate: (value) => handleUpdate({ name: value }) }), (0, jsx_runtime_1.jsx)(ConfigRow_1.ConfigRow, { label: "Title", value: nestedField.title, onUpdate: (value) => handleUpdate({ title: value }) }), renderFieldTypeSpecificContent(nestedField, parentId)] })] }, `${parentId}_${index}`));
    };
    const renderFieldTypeSpecificContent = (fieldConfig, parentId) => {
        switch (fieldConfig.type) {
            case 'object':
                return ((0, jsx_runtime_1.jsx)(ObjectFieldRenderer_1.ObjectFieldRenderer, { field: fieldConfig, fieldName: parentId, inputTypeMenuItems: inputTypeMenuItems, renderNestedField: renderNestedField }));
            case 'oneOf':
                return ((0, jsx_runtime_1.jsx)(OptionsRenderer_1.OptionsRenderer, { field: fieldConfig, fieldName: parentId, inputTypeMenuItems: inputTypeMenuItems, renderNestedField: renderNestedField }));
            case 'anyOf':
                return ((0, jsx_runtime_1.jsx)(OptionsRenderer_1.OptionsRenderer, { field: fieldConfig, fieldName: parentId, inputTypeMenuItems: inputTypeMenuItems, renderNestedField: renderNestedField }));
            case 'select':
                return (0, jsx_runtime_1.jsx)(SelectFieldRenderer_1.SelectFieldRenderer, { field: fieldConfig });
            case 'array':
                return ((0, jsx_runtime_1.jsx)(ArrayFieldRenderer_1.ArrayFieldRenderer, { field: fieldConfig, fieldName: parentId, inputTypeMenuItems: inputTypeMenuItems, renderNestedField: renderNestedField }));
            default:
                return null;
        }
    };
    return ((0, jsx_runtime_1.jsxs)(uikit_1.Card, { className: b(), children: [(0, jsx_runtime_1.jsx)(FieldHeader_1.FieldHeader, { title: field.type.toUpperCase(), onRemove: () => removeField(field.id) }), (0, jsx_runtime_1.jsxs)("div", { className: b('config'), children: [(0, jsx_runtime_1.jsx)(ConfigRow_1.ConfigRow, { label: "Name", value: field.name, onUpdate: (value) => updateField(field.id, { name: value }) }), (0, jsx_runtime_1.jsx)(ConfigRow_1.ConfigRow, { label: "Title", value: field.title, onUpdate: (value) => updateField(field.id, { title: value }) }), renderFieldTypeSpecificContent(field, field.id)] })] }, field.id));
};
exports.FieldCard = FieldCard;
//# sourceMappingURL=FieldCard.js.map
