"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateConfig = void 0;
var config_1 = require("./config");
var utils_1 = require("./utils");
// models not included in the links
var skipModels = ['emissions.deleted', 'transformations'];
var isHestiaResidueCyclicModel = function (_a) {
    var model = _a.model, value = _a.value;
    return model === 'hestia' && (value === null || value === void 0 ? void 0 : value.startsWith('residue'));
};
var isOrganicCarbonCyclicModel = function (_a) {
    var model = _a.model, value = _a.value;
    return model === 'hestia' && (value === null || value === void 0 ? void 0 : value.startsWith('organicCarbonPer'));
};
var includeModel = function (model) {
    return [
        !skipModels.includes(model.model),
        // cyclic dependencies
        !isHestiaResidueCyclicModel(model),
        !isOrganicCarbonCyclicModel(model)
    ].every(Boolean);
};
// dependencies that cannot be validated
var skipDependencies = [
    // completeness gap-fills after other models run
    'completeness'
];
var matchDependencies = function (models) { return function (model) {
    var _a, _b, _c;
    var dependencies = (0, utils_1.findMatchingModelFromConfig)(model).dependencies;
    var previousModels = models.slice(0, models.indexOf(model));
    var existing = (_a = dependencies === null || dependencies === void 0 ? void 0 : dependencies.filter(function (value) {
        return previousModels.some(function (config) { var _a; return ((_a = config.value) === null || _a === void 0 ? void 0 : _a.split('.').pop()) === value; });
    })) !== null && _a !== void 0 ? _a : [];
    return {
        model: model,
        existing: existing,
        missing: (_c = (_b = dependencies === null || dependencies === void 0 ? void 0 : dependencies
        // skip dependencies that dont exist at all
        .filter(function (value) { return models.some(function (config) { return config.value === value; }); })) === null || _b === void 0 ? void 0 : _b.filter(function (v) {
            return !existing.includes(v) &&
                !skipDependencies.includes(v) &&
                v !== model.value;
        })) !== null && _c !== void 0 ? _c : []
    };
}; };
var validateConfig = function (nodeType) {
    var config = (0, config_1.loadConfig)(nodeType);
    // flatten all models to process one by one, starting with the last one
    var models = config.models.flat();
    var results = models.filter(includeModel).map(matchDependencies(models));
    return results.filter(function (r) { return r.missing.length > 0; });
};
exports.validateConfig = validateConfig;
