"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unitFromBundleSpec = void 0;
const f_1 = require("../system/core/object/ForEachKeyValue/f");
const evaluate_1 = require("./evaluate");
const fromId_1 = require("./fromId");
function unitFromBundleSpec(unitBundleSpec, branch = {}, system) {
    const { unit: { id, input, output }, specs = {}, } = unitBundleSpec;
    const { classes } = system;
    system.injectSpecs(specs);
    const Bundle = (0, fromId_1.fromId)(id, system.specs, classes, branch);
    const unit = new Bundle(system);
    (0, f_1.default)(input || {}, ({ constant }, pinId) => {
        if (constant !== undefined && constant !== null) {
            unit.setInputConstant(pinId, constant);
        }
    });
    (0, f_1.default)(input || {}, ({ ignored }, pinId) => {
        if (typeof ignored === 'boolean') {
            unit.setInputIgnored(pinId, ignored);
        }
    });
    (0, f_1.default)(output || {}, ({ ignored }, pinId) => {
        if (typeof ignored === 'boolean') {
            unit.setOutputIgnored(pinId, ignored);
        }
    });
    const { unit: { memory }, } = unitBundleSpec;
    if (memory) {
        unit.restore(memory);
    }
    (0, f_1.default)(input || {}, ({ data }, pinId) => {
        if (data !== undefined) {
            const input = unit.getInput(pinId);
            data = (0, evaluate_1.evaluate)(data, system.specs, classes);
            input.push(data);
        }
    });
    return unit;
}
exports.unitFromBundleSpec = unitFromBundleSpec;
//# sourceMappingURL=unitFromSpec.js.map