"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 icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const MessageCard_1 = require("../../components/MessageCard/MessageCard.js");
const messages_1 = require("../../constants/messages.js");
const useMainEditorStore_1 = require("../../hooks/useMainEditorStore.js");
const utils_1 = require("../../utils/index.js");
const cn_1 = require("../../utils/cn.js");
const UpdateModal_1 = require("./UpdateModal/UpdateModal.js");
const b = (0, cn_1.editorCn)('source-code');
const formatOptions = [
    { value: 'yaml', content: 'YAML' },
    { value: 'json', content: 'JSON' },
];
const SourceCode = ({ className, showSelectedBlockOnly = false }) => {
    const { content, setContent, selectedBlock } = (0, useMainEditorStore_1.useMainEditorStore)();
    const [isOpen, setIsOpen] = React.useState(false);
    const [format, setFormat] = React.useState('yaml');
    const handleUpdate = (tempConfig) => {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let object;
        try {
            if (tempConfig.trim().startsWith('{') && tempConfig.trim().endsWith('}')) {
                object = JSON.parse(tempConfig);
            }
            else {
                object = js_yaml_1.default.load(tempConfig);
            }
        }
        catch {
            // eslint-disable-next-line no-console
            console.error('JSON.parse failed');
            return;
        }
        if (showSelectedBlockOnly && selectedBlock) {
            // Update only the selected block
            const currentBlockPath = (0, utils_1.generateChildrenPathFromArray)(selectedBlock);
            const newContent = lodash_1.default.cloneDeep(content);
            lodash_1.default.set(newContent.blocks, currentBlockPath, object);
            setContent(newContent);
        }
        else if (object) {
            // Update the entire content
            setContent(object);
        }
        setIsOpen(false);
    };
    const textContent = React.useMemo(() => {
        if (showSelectedBlockOnly && selectedBlock) {
            const currentBlockPath = (0, utils_1.generateChildrenPathFromArray)(selectedBlock);
            const currentConfig = lodash_1.default.get(content.blocks, currentBlockPath || '');
            if (currentConfig) {
                return format === 'yaml'
                    ? js_yaml_1.default.dump(currentConfig)
                    : JSON.stringify(currentConfig, null, 2);
            }
            return 'No block selected';
        }
        return format === 'yaml' ? js_yaml_1.default.dump(content) : JSON.stringify(content, null, 2);
    }, [format, content, showSelectedBlockOnly, selectedBlock]);
    if (!selectedBlock && showSelectedBlockOnly) {
        return ((0, jsx_runtime_1.jsx)("div", { className: b(null, className), children: (0, jsx_runtime_1.jsx)(MessageCard_1.MessageCard, { title: messages_1.MESSAGES.NO_BLOCK_SELECTED.title, description: messages_1.MESSAGES.NO_BLOCK_SELECTED.description, theme: "info" }) }));
    }
    return ((0, jsx_runtime_1.jsxs)("div", { className: b(null, className), children: [(0, jsx_runtime_1.jsxs)("div", { className: b('header'), children: [(0, jsx_runtime_1.jsx)(uikit_1.SegmentedRadioGroup, { size: "m", value: format, options: formatOptions, onUpdate: (value) => setFormat(value) }), (0, jsx_runtime_1.jsxs)("div", { className: b('controls'), children: [(0, jsx_runtime_1.jsxs)(uikit_1.Button, { view: "outlined", size: "m", onClick: () => setIsOpen(true), children: [(0, jsx_runtime_1.jsx)(uikit_1.Icon, { size: 14, data: icons_1.Pencil }), "Edit"] }), (0, jsx_runtime_1.jsx)(uikit_1.ClipboardButton, { view: "outlined", size: "m", text: textContent })] })] }), (0, jsx_runtime_1.jsx)("div", { className: b('code'), children: (0, jsx_runtime_1.jsx)("div", { className: b('content'), children: textContent }) }), (0, jsx_runtime_1.jsx)(UpdateModal_1.UpdateModal, { onApply: handleUpdate, onClose: () => setIsOpen(false), isOpen: isOpen, initialConfig: textContent })] }));
};
exports.default = SourceCode;
//# sourceMappingURL=SourceCode.js.map
