"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 evaluateMemorySpec_1 = require("./evaluate/evaluateMemorySpec");
const fromId_1 = require("./fromId");
function unitFromBundleSpec(system, bundle, specs, branch = {}) {
    const { unit: { id, input, output }, } = bundle;
    const { classes } = system;
    if (bundle.specs) {
        system.injectSpecs(bundle.specs);
    }
    const unit = (0, fromId_1.unitFromId)(system, id, specs, classes, branch);
    (0, f_1.default)(input || {}, (pinSpec = {}, pinId) => {
        const { constant, ignored } = pinSpec;
        if (constant !== undefined && constant !== null) {
            unit.setInputConstant(pinId, constant);
        }
        if (typeof ignored === 'boolean') {
            unit.setInputIgnored(pinId, ignored);
        }
    });
    (0, f_1.default)(output || {}, (pinSpec = {}, pinId) => {
        const { ignored } = pinSpec;
        if (typeof ignored === 'boolean') {
            unit.setOutputIgnored(pinId, ignored);
        }
    });
    const { unit: { memory }, } = bundle;
    if (memory) {
        (0, evaluateMemorySpec_1.evaluateMemorySpec)(memory, specs, classes);
        unit.restore(memory);
    }
    (0, f_1.default)(input || {}, (unitPinSpec, pinId) => {
        var _a, _b;
        const { data } = unitPinSpec !== null && unitPinSpec !== void 0 ? unitPinSpec : {};
        if (data !== undefined) {
            const input = unit.getInput(pinId);
            const _data = (0, evaluate_1.evaluate)(data, system.specs, classes);
            const isClass = _data instanceof Function;
            if (isClass) {
                system.injectSpecs((_b = (_a = _data.__bundle) === null || _a === void 0 ? void 0 : _a.specs) !== null && _b !== void 0 ? _b : {});
            }
            if (input.ref()) {
                if (isClass) {
                    const __data = new _data(system);
                    __data.play();
                    input.push(__data);
                }
                else {
                    //
                }
            }
            else {
                input.push(_data);
            }
        }
    });
    return unit;
}
exports.unitFromBundleSpec = unitFromBundleSpec;
//# sourceMappingURL=unitFromSpec.js.map