"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../Class/Functional");
const fromSpec_1 = require("../../../../spec/fromSpec");
const Async_1 = require("../../../../types/interface/async/Async");
const clone_1 = require("../../../../util/clone");
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, fail) {
        graph = (0, Async_1.Async)(graph, ['G'], this.__system.async);
        try {
            if (!id) {
                throw new Error('invalid unit id');
            }
            const { __bundle } = Class;
            const { id: __id, input: __inputs = {} } = __bundle.unit;
            const specs = (0, weakMerge_1.weakMerge)(__bundle.specs, this.__system.specs);
            const bundle = (0, clone_1.clone)(__bundle);
            (0, fromSpec_1.applyUnitDefaultIgnored)(bundle.unit, specs);
            graph.$addUnit({ unitId: id, bundle });
        }
        catch (err) {
            fail(err.message);
            return;
        }
        done();
    }
}
exports.default = AddUnit;
//# sourceMappingURL=index.js.map