"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderGraph = renderGraph;
const NOOP_1 = require("../../NOOP");
const evaluateMemorySpec_1 = require("../../spec/evaluate/evaluateMemorySpec");
const Component_1 = require("../../system/platform/component/Frame/Component");
const callAll_1 = require("../../util/call/callAll");
const clone_1 = require("../../util/clone");
const weakMerge_1 = require("../../weakMerge");
const componentFromSpec_1 = require("../componentFromSpec");
const context_1 = require("../context");
const renderFrame_1 = require("../renderFrame");
function renderGraph(root, system, $graph) {
    // console.log('renderGraph')
    let unlisten = NOOP_1.NOOP;
    $graph.$getBundle({ deep: true }, async (bundle) => {
        var _a;
        const { spec, specs } = (0, clone_1.clone)(bundle);
        for (const unitId in spec.units) {
            const unit = spec.units[unitId];
            (0, evaluateMemorySpec_1.evaluateMemorySpec)(unit.memory, (0, weakMerge_1.weakMerge)(specs, system.specs), system.classes);
        }
        const component = (0, componentFromSpec_1.componentFromSpec)(system, spec, (0, weakMerge_1.weakMerge)(system.specs, (_a = bundle.specs) !== null && _a !== void 0 ? _a : {}));
        const context = (0, renderFrame_1.renderFrame)(system, null, root, {});
        const frame = new Component_1.default({}, system);
        component.connect($graph);
        frame.appendChild(component);
        root.appendChild(frame.$element);
        const removeChild = (0, context_1.appendChild)(context, frame);
        (0, context_1.mount)(context);
        component.focus();
        const unlistenRender = () => {
            component.disconnect();
            frame.removeChild(component);
            (0, context_1.unmount)(context);
        };
        unlisten = (0, callAll_1.callAll)([removeChild, unlistenRender]);
    });
    return unlisten;
}
//# sourceMappingURL=renderGraph.js.map