'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSortedVersions = getSortedVersions;
exports.getVersionsForUpgrade = getVersionsForUpgrade;
exports.hasUpgradesRequired = hasUpgradesRequired;
exports.getDirectiveByVersion = getDirectiveByVersion;
/**
 * The directive controller serves as an interface between directive files and HDB.  Since we don't maintain file
 * structure in the installed version, we need to require all directive files in order to make them accessible.
 *
 * Any time a directive file is added to the project, it must be required in this manager.
 */
const hdbUtils = __importStar(require("../../utility/common_utils.js"));
const hdbTerms = __importStar(require("../../utility/hdbTerms.js"));
const harper_logger_ts_1 = __importDefault(require("../../utility/logging/harper_logger.js"));
const _5_1_0_ts_1 = __importDefault(require("./5-1-0.js"));
const { DATA_VERSION, UPGRADE_VERSION } = hdbTerms.UPGRADE_JSON_FIELD_NAMES_ENUM;
let versions = new Map();
// All directive modules export an array of { version, sync_functions, async_functions }.
// New directives must be imported above and registered here.
for (const directive of _5_1_0_ts_1.default) {
    versions.set(directive.version, directive);
}
/**
 * Returns all HDB versions w/ upgrade directives
 * Note: this does NOT return a list of all versions of HDB
 *
 * @returns {this}
 */
function getSortedVersions() {
    return [...versions.keys()].sort(hdbUtils.compareVersions);
}
/**
 * Returns an array of version numbers that include/require an upgrade directive be run - this is basically the ordered list
 * of upgrades that will need to be run for the HDB instance to be able to run on the currently installed software version
 *
 * @param upgradeObj
 * @returns {any[]|*[]}
 */
function getVersionsForUpgrade(upgradeObj) {
    let currVersion = upgradeObj[DATA_VERSION];
    let newVersion = upgradeObj[UPGRADE_VERSION];
    if (hdbUtils.isEmptyOrZeroLength(currVersion) || hdbUtils.isEmptyOrZeroLength(newVersion)) {
        //we should never get to this scenario but if so, we will return empty array so that server can try to start
        // with current install and data
        harper_logger_ts_1.default.info(`There is an issue with the version data in your instance of HDB.  Current version data: ${upgradeObj}`);
        harper_logger_ts_1.default.error('There was an error when trying to evaluate the version information for your instance.  Trying to ' +
            'start the server anyways but it may fail. If you continue to have this problem, please contact support@harperdb.io.');
        return [];
    }
    return [...versions.keys()].sort(hdbUtils.compareVersions).filter(function (thisVersion) {
        return (hdbUtils.compareVersions(thisVersion, currVersion) > 0 && hdbUtils.compareVersions(thisVersion, newVersion) <= 0);
    });
}
/**
 * Helper function for determining if there are version upgrades required based on the current status of the data and hdb software
 * versions.  If there are not, it will return false.
 *
 * @param upgradeObj
 * @returns {boolean} - returns true if an upgrade/s is/are required
 */
function hasUpgradesRequired(upgradeObj) {
    const validVersions = getVersionsForUpgrade(upgradeObj);
    return validVersions.length > 0;
}
/**
 * Returns the upgrade directive object for a specific version, if present.
 *
 * @param version
 * @returns {null|any}
 */
function getDirectiveByVersion(version) {
    if (hdbUtils.isEmptyOrZeroLength(version)) {
        return null;
    }
    if (versions.has(version)) {
        return versions.get(version);
    }
    return null;
}
//# sourceMappingURL=directivesController.js.map