"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../../Class/Functional");
const deepSet_1 = require("../../../../../deepSet");
const fromUnitBundle_1 = require("../../../../../spec/fromUnitBundle");
const util_1 = require("../../../../../spec/util");
const clone_1 = require("../../../../../util/clone");
const weakMerge_1 = require("../../../../../weakMerge");
const _ids_1 = require("../../../../_ids");
class SetPinConstant extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['unit', 'type', 'name', 'constant'],
            o: ['unit'],
        }, {}, system, _ids_1.ID_SET_PIN_CONSTANT);
    }
    f({ unit, type, name, constant }, done, fail) {
        const { __bundle } = unit;
        const { unit: { id }, } = __bundle;
        const specs = (0, weakMerge_1.weakMerge)(this.__system.specs, __bundle.specs ?? {});
        const spec = (0, util_1.getSpec)(specs, id);
        const { inputs = {}, outputs = {} } = spec;
        if (type === 'input' && !inputs[name]) {
            fail('input not found');
            return;
        }
        if (type === 'output' && !outputs[name]) {
            fail('output not found');
            return;
        }
        const bundleClone = (0, clone_1.clone)(__bundle);
        (0, deepSet_1.deepSet_)(bundleClone, ['unit', type, name, 'constant'], constant);
        const NewBundle = (0, fromUnitBundle_1.fromUnitBundle)(bundleClone, specs, this.__system.classes);
        done({ unit: NewBundle });
    }
}
exports.default = SetPinConstant;
//# sourceMappingURL=index.js.map