"use strict";
/**
 * Compare semver-style strings
 *
 * Ported from [condition.compareVersion@d138b50](https://github.com/optimizely/optimizely/blob/d138b50/src/www/js/client/condition.js#L209)
 *
 * @module
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareVersion = void 0;
/**
 * Compare an actual version against a targetedVersion; return -1 if the actual version is "semantically less"
 * than the targetedVersion, 1 if it is "semantically greater", and 0 if they are "semantically identical"
 * (similar to Array.prototype.sort compareFns).
 *
 * "Semantically" means the following: given both version numbers expressed in x.y.z... format, to the level of
 * precision of the targetedVersion, compare the corresponding version parts (e.g. major to major, minor to minor).
 *
 * @private
 * @param {string} version expressed as a string x.y.z...
 * @param {string} targetedVersion expressed as a string x.y.z...
 * @returns {number} -1 if version < targetedVersion, 1 if version > targetedVersion, 0 if they are approx. equal
 */
function compareVersion(version, targetedVersion) {
    if (!targetedVersion) {
        // Any version.
        return 0;
    }
    // Expect a version string of the form x.y.z
    var targetedVersionParts = targetedVersion.toString().split('.');
    var versionParts = version.toString().split('.');
    // Up to the precision of targetedVersion, expect version to match exactly.
    for (var i = 0; i < targetedVersionParts.length; i++) {
        if (typeof versionParts[i] === 'undefined') {
            return -1;
        }
        else if (isNaN(Number(versionParts[i]))) {
            //Compare strings
            if (versionParts[i] !== targetedVersionParts[i]) {
                return -1;
            }
        }
        else if (Number(versionParts[i]) < Number(targetedVersionParts[i])) {
            return -1;
        }
        else if (Number(versionParts[i]) > Number(targetedVersionParts[i])) {
            return 1;
        }
    }
    return 0;
}
exports.compareVersion = compareVersion;
