"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unitFromId = exports.classFromId = exports.bundleFromId = void 0;
const Lazy_1 = require("./Lazy");
const SpecNotFoundError_1 = require("./SpecNotFoundError");
const bundleClass_1 = require("./bundleClass");
const fromSpec_1 = require("./fromSpec");
function bundleFromId(id, specs, classes, branch = {}) {
    let spec = specs[id];
    if (!spec) {
        throw new SpecNotFoundError_1.SpecNotFoundError();
    }
    const Class = classFromId(id, specs, classes, branch);
    const bundle = { unit: { id }, specs: {} }; // TODO
    const Bundle = (0, bundleClass_1.bundleClass)(Class, bundle);
    return Bundle;
}
exports.bundleFromId = bundleFromId;
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, Object.assign(Object.assign({}, branch), { [id]: true }));
        }
    }
    return Class;
}
exports.classFromId = classFromId;
function unitFromId(system, id, specs, classes, branch = {}) {
    let spec = specs[id];
    if (!spec) {
        throw new SpecNotFoundError_1.SpecNotFoundError();
    }
    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, Object.assign(Object.assign({}, branch), { [id]: true }));
        }
    }
    else {
        unit = new Class(system, id);
    }
    return unit;
}
exports.unitFromId = unitFromId;
//# sourceMappingURL=fromId.js.map