"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__fromSpec = exports._fromSpec = exports.fromSpec = exports.extractGraphSpecs = void 0;
const Graph_1 = require("../Class/Graph");
const bundleClass_1 = require("./bundleClass");
function extractGraphSpecs(spec, specs) {
    const graphs = {};
    const { units } = spec;
    for (const unit_id in units) {
        const unit = units[unit_id];
        const { id } = unit;
        const unit_spec = specs[id];
        const { system } = unit_spec;
        if (!system) {
            graphs[id] = unit_spec;
        }
    }
    return graphs;
}
exports.extractGraphSpecs = extractGraphSpecs;
function fromSpec(spec, _specs, branch = {}) {
    const Class = _fromSpec(spec, _specs, branch);
    const { id } = spec;
    if (!id) {
        throw new Error('');
    }
    const specs = extractGraphSpecs(spec, _specs);
    const Bundle = (0, bundleClass_1.bundleClass)(Class, { unit: { id }, specs });
    return Bundle;
}
exports.fromSpec = fromSpec;
function _fromSpec(spec, specs, branch = {}) {
    const { name, units } = spec;
    for (const unitId in units) {
        const unitSpec = units[unitId];
        unitSpec.input = unitSpec.input || {};
        unitSpec.output = unitSpec.output || {};
        const { id, input, output } = unitSpec;
        const spec = specs[id];
        const { inputs, outputs } = spec;
        function setIgnored(unitPinSpec, pinSpec) {
            const { ignored } = unitPinSpec;
            if (ignored === undefined) {
                const { defaultIgnored } = pinSpec;
                if (defaultIgnored === true) {
                    unitPinSpec.ignored = true;
                }
            }
        }
        for (const inputId in inputs) {
            const inputPinSpec = inputs[inputId];
            input[inputId] = input[inputId] || {};
            const unitInputPinSpec = input[inputId];
            setIgnored(unitInputPinSpec, inputPinSpec);
        }
        for (const outputId in outputs) {
            const outputPinSpec = outputs[outputId];
            output[outputId] = output[outputId] || {};
            const unitOutputPinSpec = output[outputId];
            setIgnored(unitOutputPinSpec, outputPinSpec);
        }
    }
    return __fromSpec(spec, branch);
}
exports._fromSpec = _fromSpec;
function __fromSpec(spec, branch = {}) {
    const { id, name } = spec;
    if (!id) {
        throw new Error('');
    }
    class Class extends Graph_1.Graph {
        constructor(system) {
            super(spec, branch, system, id);
        }
    }
    Object.defineProperty(Class, 'name', {
        value: name,
    });
    return Class;
}
exports.__fromSpec = __fromSpec;
//# sourceMappingURL=fromSpec.js.map