"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.$renderComponent = $renderComponent;
exports.renderComponent = renderComponent;
const NOOP_1 = require("../../NOOP");
const callAll_1 = require("../../util/call/callAll");
const weakMerge_1 = require("../../weakMerge");
const componentFromUnitSpec_1 = require("../componentFromUnitSpec");
const context_1 = require("../context");
const renderFrame_1 = require("../renderFrame");
function $renderComponent(system, context, root, $component, callback = NOOP_1.NOOP) {
    let unlisten = NOOP_1.NOOP;
    $component.$getUnitBundleSpec({}, (bundle) => {
        const { unit } = bundle;
        const specs = (0, weakMerge_1.weakMerge)(system.specs, bundle.specs ?? {});
        const component = (0, componentFromUnitSpec_1.componentFromUnitSpec)(system, specs, unit);
        unlisten = renderComponent(system, context, root, component);
        component.connect($component);
        callback(component);
    });
    return unlisten;
}
function renderComponent(system, context, root, component) {
    let unlisten = NOOP_1.NOOP;
    const context_ = (0, renderFrame_1.renderFrame)(system, context, root, {});
    const removeChild = (0, context_1.appendChild)(context_, component);
    (0, context_1.mount)(context_);
    unlisten = (0, callAll_1.callAll)([
        () => {
            (0, context_1.unmount)(context_);
        },
        removeChild,
    ]);
    return unlisten;
}
//# sourceMappingURL=renderComponent.js.map