"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromId = void 0;
const bundle_1 = require("../bundle");
const object_1 = require("../util/object");
const bundleClass_1 = require("./bundleClass");
const fromSpec_1 = require("./fromSpec");
const Lazy_1 = require("./Lazy");
const SpecNotFoundError_1 = require("./SpecNotFoundError");
function fromId(id, specs, classes, branch = {} // TODO branch should be an in memory tree
) {
    let spec = specs[id];
    if (!spec) {
        throw new SpecNotFoundError_1.SpecNotFoundError();
    }
    let Class = classes[id];
    if (Class === undefined) {
        spec = spec;
        const bundle = (0, object_1.clone)((0, bundle_1.bundleSpec)(spec, specs));
        spec.specs = bundle.specs;
        if (branch[id]) {
            Class = (0, Lazy_1.lazyFromSpec)(spec, specs, branch);
        }
        else {
            Class = (0, fromSpec_1._fromSpec)(spec, specs, Object.assign(Object.assign({}, branch), { [id]: true }));
        }
    }
    const bundle = { unit: { id } };
    const Bundle = (0, bundleClass_1.bundleClass)(Class, bundle);
    return Bundle;
}
exports.fromId = fromId;
//# sourceMappingURL=fromId.js.map