"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.specComplexity = exports.specComplexityById = exports.graphUnitComplexity = exports.graphMergeComplexity = exports.graphComplexity = exports.graphComplexityById = exports.treeComplexity = exports.treeComplexityById = void 0;
const f_1 = require("../system/f/object/Keys/f");
const HIRC = 1; // Human Information Retrieval Cost
const GFC = 2; // Graph Fundamental Complexity
// tree complexity
function treeComplexityById(specs, id, known = {}) {
    if (known[id]) {
        return HIRC;
    }
    known[id] = true;
    const spec = specs[id];
    return treeComplexity(specs, spec, known);
}
exports.treeComplexityById = treeComplexityById;
function treeComplexity(specs, spec, known = {}) {
    var _a;
    let c = 0;
    const { base } = spec;
    if (base) {
        const { metadata = {} } = spec;
        const { complexity = 1 } = metadata;
        return complexity;
    }
    else {
        c += GFC;
        const { units = {} } = spec;
        for (const unitId in units) {
            const unit = units[unitId];
            const { id, input = {} } = unit;
            for (const inputId in input) {
                const _input = (_a = input[inputId]) !== null && _a !== void 0 ? _a : {};
                if (_input.data !== undefined) {
                    const l = _input.data.length;
                }
            }
            c += treeComplexityById(specs, id, known);
        }
    }
    return c;
}
exports.treeComplexity = treeComplexity;
// layer complexity
function graphComplexityById(specs, path) {
    const graph = specs[path];
    return graphComplexity(specs, graph);
}
exports.graphComplexityById = graphComplexityById;
function graphComplexity(specs, graph) {
    let c = specComplexity(graph);
    c += graphMergeComplexity(graph);
    c += graphUnitComplexity(specs, graph);
    return c;
}
exports.graphComplexity = graphComplexity;
function graphMergeComplexity(graph) {
    const { merges = {} } = graph;
    return (0, f_1.keys)(merges).length;
}
exports.graphMergeComplexity = graphMergeComplexity;
function graphUnitComplexity(specs, graph) {
    const known = {};
    const { units = {} } = graph;
    let c = 0;
    for (const unitId in units) {
        const { id } = units[unitId];
        // if you already know a unit, just grab from memory
        if (!known[id]) {
            const spec = specs[id];
            c += specComplexity(spec);
            known[id] = true;
        }
        else {
            c += HIRC;
        }
    }
    return c;
}
exports.graphUnitComplexity = graphUnitComplexity;
// interface complexity
function specComplexityById(specs, id) {
    const spec = specs[id];
    return specComplexity(spec);
}
exports.specComplexityById = specComplexityById;
function specComplexity(spec) {
    return HIRC + (0, f_1.keys)(spec.inputs || {}).length + (0, f_1.keys)(spec.outputs || {}).length;
}
exports.specComplexity = specComplexity;
//# sourceMappingURL=complexity.js.map