"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromSpec = fromSpec;
exports.applyUnitDefaultIgnored = applyUnitDefaultIgnored;
exports.applyDefaultIgnored = applyDefaultIgnored;
exports.classFromSpec = classFromSpec;
exports.graphFromSpec = graphFromSpec;
const bundle_1 = require("../bundle");
const Graph_1 = require("../Class/Graph");
const IOOf_1 = require("../types/IOOf");
const weakMerge_1 = require("../weakMerge");
const bundleClass_1 = require("./bundleClass");
function fromSpec(spec, specs_, classes = {}, branch = {}) {
    const Class = classFromSpec(spec, specs_, classes, branch);
    const { id } = spec;
    if (!id) {
        throw new Error('spec id is required');
    }
    const bundle = (0, bundle_1.unitBundleSpec)({ id }, (0, weakMerge_1.weakMerge)({ [id]: spec }, specs_));
    const Bundle = (0, bundleClass_1.bundleClass)(Class, bundle, specs_);
    return Bundle;
}
function applyUnitDefaultIgnored(unitSpec, specs) {
    unitSpec.input = unitSpec.input || {};
    unitSpec.output = unitSpec.output || {};
    const { id } = unitSpec;
    const spec = specs[id];
    function setIgnored(unitPinSpec, pinSpec) {
        const { ignored } = unitPinSpec;
        if (ignored === undefined) {
            const { defaultIgnored } = pinSpec;
            if (defaultIgnored === true) {
                unitPinSpec.ignored = true;
            }
        }
    }
    (0, IOOf_1.io)((type) => {
        var _a;
        const pins = (_a = spec[`${type}s`]) !== null && _a !== void 0 ? _a : {};
        const pin = unitSpec[type];
        for (const pinId in pins) {
            const pinSpec = pins[pinId];
            pin[pinId] = pin[pinId] || {};
            const unitPinSpec = pin[pinId];
            setIgnored(unitPinSpec, pinSpec);
        }
    });
}
function applyDefaultIgnored(spec, specs) {
    const { name, units } = spec;
    for (const unitId in units) {
        const unitSpec = units[unitId];
    }
}
function classFromSpec(spec, specs, classes, branch = {}) {
    applyDefaultIgnored(spec, specs);
    const { id, name } = spec;
    class Class extends Graph_1.Graph {
        constructor(system) {
            const spec = specs[id];
            super(spec, branch, system, id);
        }
    }
    Object.defineProperty(Class, 'name', {
        value: name,
    });
    return Class;
}
function graphFromSpec(system, spec, specs, branch = {}) {
    applyDefaultIgnored(spec, specs);
    return new Graph_1.Graph(spec, branch, system, spec.id);
}
//# sourceMappingURL=fromSpec.js.map