"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMaxStage = exports.loadTriggerConfig = exports.loadRunConfig = exports.loadConfig = exports.allowedTypes = void 0;
var schema_1 = require("@hestia-earth/schema");
var runConfig = require("../config/run-calculations.json");
var triggerConfig = require("../config/trigger-calculations.json");
var Cycle = require("../config/Cycle.json");
var ImpactAssessment = require("../config/ImpactAssessment.json");
var Site = require("../config/Site.json");
exports.allowedTypes = [
    schema_1.NodeType.Cycle,
    schema_1.NodeType.Site,
    schema_1.NodeType.ImpactAssessment
];
var validateType = function (nodeType) {
    return exports.allowedTypes.includes(nodeType) ||
        (function () {
            throw new Error("Invalid type ".concat(nodeType, ". Allowed types: ").concat(exports.allowedTypes.join(', ')));
        })();
};
var typeToConfig = (_a = {},
    _a[schema_1.NodeType.Cycle] = Cycle,
    _a[schema_1.NodeType.ImpactAssessment] = ImpactAssessment,
    _a[schema_1.NodeType.Site] = Site,
    _a);
/**
 * Load orchestrator configuration.
 *
 * @param nodeType NodeType associated with configuration.
 * @returns
 */
var loadConfig = function (nodeType) {
    return validateType(nodeType) && typeToConfig[nodeType];
};
exports.loadConfig = loadConfig;
var validateStage = function (config, nodeType, stage) {
    return "stage-".concat(stage) in config[nodeType]
        ? config[nodeType]["stage-".concat(stage)]
        : (function () {
            throw new Error("Invalid stage configuration for ".concat(nodeType, ": ").concat(stage));
        })();
};
var loadRunConfig = function (nodeType, stage) {
    return validateType(nodeType) &&
        validateStage(runConfig, nodeType, stage);
};
exports.loadRunConfig = loadRunConfig;
var loadTriggerConfig = function (nodeType, stage) {
    return validateType(nodeType) &&
        validateStage(triggerConfig, nodeType, stage);
};
exports.loadTriggerConfig = loadTriggerConfig;
var getMaxStage = function (nodeType) {
    return validateType(nodeType)
        ? Math.max.apply(Math.max, Object.keys(runConfig[nodeType]).map(function (key) {
            return Number(key.replace('stage-', ''));
        }))
        : null;
};
exports.getMaxStage = getMaxStage;
