"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonViewer = void 0;
const tslib_1 = require("tslib");
const tree_list_1 = require("@stoplight/tree-list");
const React = tslib_1.__importStar(require("react"));
const renderNode_1 = require("../../../utils/renderNode");
const JsonRow_1 = require("./JsonRow");
exports.JsonViewer = ({ node, expanded, expandedDepth = 2, maxRows = 10, }) => {
    const tree = React.useRef(new tree_list_1.Tree());
    const treeStore = React.useRef(new tree_list_1.TreeStore(tree.current, new tree_list_1.TreeState(), {
        defaultExpandedDepth: expanded ? Infinity : expandedDepth,
    }));
    React.useEffect(() => {
        tree.current.setRoot(tree_list_1.Tree.toTree(renderNode_1.renderTree(node)));
    }, [node]);
    treeStore.current.events.on(tree_list_1.TreeListEvents.NodeClick, (e, n) => {
        if (tree_list_1.isParentNode(n)) {
            treeStore.current.toggleExpand(n);
        }
    });
    const rowRenderer = React.useCallback((n, rowOptions) => React.createElement(JsonRow_1.JsonRow, { node: n, isExpanded: !!rowOptions.isExpanded }), []);
    const content = typeof node === 'object' ? (React.createElement(tree_list_1.TreeList, { striped: true, maxRows: maxRows, rowRenderer: rowRenderer, store: treeStore.current })) : (String(node));
    return React.createElement("div", { className: "w-full h-full RequestMaker__JsonViewer" }, content);
};
//# sourceMappingURL=JsonViewer.js.map