"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../Class/Functional");
const spec_1 = require("../../../../client/spec");
const weakMerge_1 = require("../../../../weakMerge");
const _ids_1 = require("../../../_ids");
class AddUnit extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['id', 'class', 'graph'],
            o: [],
        }, {
            input: {
                graph: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_ADD_UNIT);
    }
    f({ id, class: Class, graph }, done) {
        try {
            const { __bundle } = Class;
            const { unit: __unit } = __bundle;
            const { id: __id, input: __inputs = {} } = __unit;
            const specs = (0, weakMerge_1.weakMerge)(__bundle.specs, this.__system.specs);
            const spec = (0, spec_1.getSpec)(specs, __id);
            const unit_spec = { id: __id, input: {}, output: {} };
            const { inputs = {}, outputs = {} } = spec;
            for (const inputId in inputs) {
                unit_spec.input[inputId] = unit_spec.input[inputId] || {};
                const __input = __inputs[inputId];
                if (__input) {
                    const { data } = __input;
                    unit_spec.input[inputId].data = data;
                }
                const input = inputs[inputId];
                const { defaultIgnored } = input;
                if (defaultIgnored) {
                    unit_spec.input[inputId].ignored = defaultIgnored;
                }
            }
            for (const outputId in outputs) {
                const output = outputs[outputId];
                const { defaultIgnored } = output;
                if (defaultIgnored) {
                    unit_spec.output[outputId] = unit_spec.output[outputId] || {};
                    unit_spec.output[outputId].ignored = defaultIgnored;
                }
            }
            graph.addUnitSpec(id, { unit: unit_spec });
        }
        catch (err) {
            done(undefined, err.message);
            return;
        }
        done();
    }
}
exports.default = AddUnit;
//# sourceMappingURL=index.js.map