"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 cn_1 = require("../../utils/cn.js");
const DragContext_1 = require("./DragContext.js");
const TreeContent_1 = require("./TreeContent.js");
const useMainEditorStore_1 = require("../../hooks/useMainEditorStore.js");
const uikit_1 = require("@gravity-ui/uikit");
const icons_1 = require("@gravity-ui/icons");
const utils_1 = require("../../utils/index.js");
const b = (0, cn_1.editorCn)('tree');
const generateTree = (items) => {
    return items.map((item) => {
        let children;
        if ('children' in item && item.children?.length) {
            children = generateTree(item.children);
        }
        return {
            type: item.type,
            children,
            treeTitle: (0, utils_1.getItemTitle)(item),
        };
    });
};
const Tree = ({ className }) => {
    const { content, resetBlocks, selectedBlock, duplicateBlock, deleteBlock, setSelectedBlock, reorderBlock, } = (0, useMainEditorStore_1.useMainEditorStore)();
    const selectedBlockPath = React.useMemo(() => {
        return (0, utils_1.generateChildrenPathFromArray)(selectedBlock || []);
    }, [selectedBlock]);
    return ((0, jsx_runtime_1.jsx)(DragContext_1.DragContextProvider, { children: (0, jsx_runtime_1.jsxs)("div", { className: b(null, className), children: [(0, jsx_runtime_1.jsx)("div", { className: b('head'), children: (0, jsx_runtime_1.jsxs)(uikit_1.Button, { view: "outlined-danger", onClick: () => resetBlocks(), children: [(0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.TrashBin }), "Clear all"] }) }), (0, jsx_runtime_1.jsx)(TreeContent_1.TreeContent, { reorderBlock: reorderBlock, onCopy: duplicateBlock, onDelete: deleteBlock, onSelect: setSelectedBlock, blockTree: generateTree(content.blocks), selectedBlockPath: selectedBlockPath })] }) }));
};
exports.default = Tree;
//# sourceMappingURL=Tree.js.map
