"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StoryData = exports.StoryNode = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
class StoryNode {
    id = DataUtilities_1.DataUtilities.generateUniqueId();
    path = [];
    name;
    renderStory;
    constructor(renderStory, ...path) {
        this.name = path.pop() ?? "";
        this.path = path;
        this.renderStory =
            renderStory ??
                (() => {
                    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "No node here" });
                });
    }
    _subNodes = [];
    get subNodes() {
        return this._subNodes;
    }
    addSubNode(node) {
        this._subNodes.push(node);
    }
    render() {
        if (this.renderStory) {
            let story = this.renderStory();
            if (story)
                return story;
        }
        return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "Select Sub Node" });
    }
    getSubNode(name) {
        for (let n of this._subNodes) {
            if (n.name === name)
                return n;
        }
        return undefined;
    }
}
exports.StoryNode = StoryNode;
class StoryData {
    static rootNode = new StoryNode(() => {
        return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "root node" });
    }, "/");
    static nodes = {};
    static addNodes(nodes) {
        for (let n of nodes) {
            this.addNode(n);
        }
    }
    static addNode(node) {
        if (!this.nodes[this.rootNode.id])
            this.nodes[this.rootNode.id] = this.rootNode;
        this.nodes[node.id] = node;
        let currentNode = this.rootNode;
        let currentPath = [];
        currentPath.push(currentNode.name);
        for (let p of node.path) {
            currentPath.push(p);
            let sub = currentNode.getSubNode(p);
            if (sub) {
                currentNode = sub;
            }
            else {
                sub = new StoryNode(() => {
                    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "Select Node" });
                }, ...currentPath);
                currentNode.addSubNode(sub);
                currentNode = sub;
            }
        }
        if (!currentNode.getSubNode(node.name))
            currentNode.addSubNode(node);
    }
    static getStoryNode(id) {
        return this.nodes[id];
    }
}
exports.StoryData = StoryData;
//# sourceMappingURL=StoryData.js.map