"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildIdSetFromBundle = exports.buildIdSet = void 0;
const evaluate_1 = require("../spec/evaluate");
const parser_1 = require("../spec/parser");
const _classes_1 = require("../system/_classes");
const _specs_1 = require("../system/_specs");
function buildIdSet(spec, idSet = new Set()) {
    var _a, _b, _c;
    const { units = {} } = spec;
    if (idSet.has(spec.id)) {
        return idSet;
    }
    idSet.add(spec.id);
    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 : {};
            const { data } = _input;
            if (data) {
                const tree = (0, parser_1.getTree)(data);
                if (tree.type === parser_1.TreeNodeType.Unit) {
                    const str = tree.value.substring(1);
                    const bundle = (0, evaluate_1.evaluate)(str, _specs_1.default, _classes_1.default);
                    const unitSpec = (_b = _specs_1.default[bundle.unit.id]) !== null && _b !== void 0 ? _b : bundle.specs[bundle.unit.id];
                    buildIdSet(unitSpec, idSet);
                    for (const specId in bundle.specs) {
                        const spec = bundle.specs[specId];
                        buildIdSet(spec, idSet);
                    }
                }
            }
        }
        const unit_spec = _specs_1.default[id];
        if (unit_spec) {
            buildIdSet(unit_spec, idSet);
        }
        else {
            //
        }
    }
    ;
    (_c = spec.deps) === null || _c === void 0 ? void 0 : _c.forEach((id) => {
        const dep_spec = _specs_1.default[id];
        buildIdSet(dep_spec, idSet);
    });
    return idSet;
}
exports.buildIdSet = buildIdSet;
function buildIdSetFromBundle(bundle, idSet = new Set()) {
    const { spec = {}, specs = {} } = bundle;
    buildIdSet(spec, idSet);
    for (const specId in specs) {
        const spec = specs[specId];
        buildIdSet(spec, idSet);
    }
    return idSet;
}
exports.buildIdSetFromBundle = buildIdSetFromBundle;
//# sourceMappingURL=buildIdSetFromBundle.js.map