"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectUnitIdSet = collectUnitIdSet;
exports.collectIdSetFromSpec = collectIdSetFromSpec;
exports.collectIdSetFromBundle = collectIdSetFromBundle;
const object_1 = require("../util/object");
const evaluateDataValue_1 = require("../spec/evaluateDataValue");
const _classes_1 = require("../system/_classes");
const _specs_1 = require("../system/_specs");
function collectUnitIdSet(unit, specs, set) {
    var _a;
    const { id, input = {} } = unit;
    for (const inputId in input) {
        const _input = (_a = input[inputId]) !== null && _a !== void 0 ? _a : {};
        let { data } = _input;
        const buildBundleIdSet = (bundle) => {
            var _a, _b;
            const unitSpec = (_b = (_a = _specs_1.default[bundle.unit.id]) !== null && _a !== void 0 ? _a : specs[bundle.unit.id]) !== null && _b !== void 0 ? _b : bundle.specs[bundle.unit.id];
            collectUnitIdSet(bundle.unit, specs, set);
            collectIdSetFromSpec(unitSpec, specs, set);
            for (const specId in bundle.specs) {
                const spec = bundle.specs[specId];
                collectIdSetFromSpec(spec, specs, set);
            }
        };
        if (data !== undefined) {
            const dataRef = (0, evaluateDataValue_1.evaluateDataValue)(data, _specs_1.default, _classes_1.default);
            for (const path of dataRef.ref) {
                const bundle = (0, object_1.deepGet)(dataRef.data, path);
                buildBundleIdSet(bundle);
            }
        }
    }
    const unit_spec = _specs_1.default[id];
    if (unit_spec) {
        collectIdSetFromSpec(unit_spec, specs, set);
    }
    else {
        //
    }
}
function collectIdSetFromSpec(spec, specs, set = new Set()) {
    var _a;
    const { units = {} } = spec;
    if (set.has(spec.id)) {
        return set;
    }
    set.add(spec.id);
    for (const unitId in units) {
        const unit = units[unitId];
        collectUnitIdSet(unit, specs, set);
    }
    ;
    (_a = spec.deps) === null || _a === void 0 ? void 0 : _a.forEach((id) => {
        const dep_spec = _specs_1.default[id];
        collectIdSetFromSpec(dep_spec, specs, set);
    });
    return set;
}
function collectIdSetFromBundle(bundle, set = new Set()) {
    const { spec = {}, specs = {} } = bundle;
    collectIdSetFromSpec(spec, specs, set);
    for (const specId in specs) {
        const spec = specs[specId];
        collectIdSetFromSpec(spec, specs, set);
    }
    return set;
}
//# sourceMappingURL=collectIdSet.js.map