"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../../Class/Functional");
const bundle_1 = require("../../../../../bundle");
const fromBundle_1 = require("../../../../../spec/fromBundle");
const spec_1 = require("../../../../../spec/reducers/spec");
const util_1 = require("../../../../../spec/util");
const clone_1 = require("../../../../../util/clone");
const weakMerge_1 = require("../../../../../weakMerge");
const _ids_1 = require("../../../../_ids");
class SetUnitPinConstant extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['id', 'type', 'pinId', 'constant', 'graph'],
            o: ['graph'],
        }, {}, system, _ids_1.ID_SET_UNIT_PIN_CONSTANT);
    }
    f({ id, type, pinId, constant, graph }, done, fail) {
        let new_graph;
        try {
            const specs = (0, weakMerge_1.weakMerge)(graph.__bundle.specs ?? {}, this.__system.specs);
            const new_spec_id = (0, util_1.newSpecId)(specs);
            const spec = (0, util_1.getSpec)(specs, graph.__bundle.unit.id);
            const new_spec = (0, clone_1.clone)(spec);
            new_spec.id = new_spec_id;
            delete new_spec.system;
            (0, spec_1.setUnitPinConstant)({ unitId: id, type, pinId, constant }, new_spec);
            const specs_ = (0, weakMerge_1.weakMerge)(specs, { [new_spec.id]: new_spec });
            const new_bundle = (0, bundle_1.bundleSpec)(new_spec, specs_);
            new_graph = (0, fromBundle_1.fromBundle)(new_bundle, specs_, this.__system.classes);
        }
        catch (err) {
            fail(err.message);
            return;
        }
        done({ graph: new_graph });
    }
}
exports.default = SetUnitPinConstant;
//# sourceMappingURL=index.js.map