"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bundleFromId = bundleFromId;
exports.classFromId = classFromId;
exports.unitFromId = unitFromId;
const Lazy_1 = require("./Lazy");
const SpecNotFoundError_1 = require("./SpecNotFoundError");
const bundleClass_1 = require("./bundleClass");
const fromSpec_1 = require("./fromSpec");
const util_1 = require("./util");
function bundleFromId(id, specs, classes, branch = {}) {
    const Class = classFromId(id, specs, classes, branch);
    const bundle = { unit: { id }, specs: {} };
    const Bundle = (0, bundleClass_1.bundleClass)(Class, bundle, specs);
    return Bundle;
}
function classFromId(id, specs, classes, branch = {}) {
    let spec = specs[id];
    if (!spec) {
        throw new SpecNotFoundError_1.SpecNotFoundError();
    }
    let Class = classes[id];
    if (Class === undefined) {
        spec = spec;
        if (branch[id]) {
            Class = (0, Lazy_1.lazyFromSpec)(spec, specs, branch, fromSpec_1.fromSpec);
        }
        else {
            Class = (0, fromSpec_1.classFromSpec)(spec, specs, classes, {
                ...branch,
                [id]: true,
            });
        }
    }
    return Class;
}
function unitFromId(system, id, specs, classes, branch, push) {
    let spec = (0, util_1.getSpec)(specs, id);
    let unit;
    let Class = classes[id];
    if (Class === undefined) {
        spec = spec;
        if (branch[id]) {
            const Class = (0, Lazy_1.lazyFromSpec)(spec, specs, branch, fromSpec_1.fromSpec);
            unit = new Class(system, id);
        }
        else {
            unit = (0, fromSpec_1.graphFromSpec)(system, spec, specs, {
                ...branch,
                [id]: true,
            }, push);
        }
    }
    else {
        unit = new Class(system, id);
    }
    return unit;
}
//# sourceMappingURL=fromId.js.map