// LICENSE : MIT
"use strict";
import npm from "npm";
import semver from "semver";
export function npmLoad() {
    if (npm.config.loaded) {
        return Promise.resolve(npm);
    }
    return new Promise((resolve, reject) => {
        npm.load((error, npm) => {
            if (error) {
                reject(error);
            } else {
                resolve(npm);
            }
        });
    });
}
export function getVersion(pkg) {
    if (pkg["version"] == null) {
        throw new Error("not found `version` in the package: " + pkg);
    }
    return pkg["version"];
}
export function getName(pkg) {
    if (pkg["name"] == null) {
        throw new Error("not found `name` in the package: " + pkg);
    }
    return pkg["name"];
}

export function getVersionsAsync(pkg) {
    var pkgName = getName(pkg);
    var version = getVersion(pkg);
    return npmLoad().then(npm => {
        return new Promise((resolve, reject) => {
            npm.commands.view([`${pkgName}@${version}`, "versions"], true, (error, result) => {
                if (error) {
                    return reject(error);
                }
                // pick up [version]"versions"
                resolve(result[version]["versions"]);
            });
        });
    });
}

var diffTypes = ["patch", "minor", "major"];
export function findVersionMatchDiff(currentVersion, versions, diffType) {
    var diffTypeIndex = diffTypes.indexOf(diffType);
    for (; diffTypeIndex < diffType.length; diffTypeIndex++) {
        var diffTypeForCheck = diffTypes[diffTypeIndex];
        var matchVersions = versions.filter(version => {
            if (semver.diff(version, currentVersion) === diffTypeForCheck) {
                return true;
            }
        });
        if (matchVersions.length > 0) {
            return matchVersions[matchVersions.length - 1];
        }
    }
}
/**
 * get relative version of the package.
 * relative version is specified by the options.
 * @param {object} pkg
 * @param {object} options
 * @returns {Promise.<T>}
 */
export function getRelativeVersion(pkg, options = {}) {
    var isLtr = typeof options.isLtr !== "undefined" ? options.isLtr : true;
    var type = typeof options.type !== "undefined" ? options.type : "path";
    var currentVersion = getVersion(pkg);
    return getVersionsAsync(pkg).then(versions => {
        let indexOfCurrent = versions.indexOf(currentVersion);
        if (isLtr) {
            return findVersionMatchDiff(currentVersion, versions.slice(0, indexOfCurrent), type);
        } else {
            return findVersionMatchDiff(currentVersion, versions.slice(indexOfCurrent, versions.length), type);
        }
    });
}
@@ -100,16 +100,28 @@
 npmLoad(
+options = %7B%7D
 ) %7B%0A    
@@ -251,16 +251,25 @@
 pm.load(
+options, 
 (error, 
@@ -996,19 +996,8 @@
 ame%7D
-@$%7Bversion%7D
 %60, %22
@@ -1145,89 +1145,612 @@
    /
-/ pick up %5Bversion%5D%22versions%22%0A                resolve(result%5Bversion%5D%5B%22versions%22%5D
+*%0A                 result is following object:%0A                %7B%0A                '2.27.0':%0A                     %7B versions:%0A                         %5B '0.8.3',%0A                         '0.9.0',%0A                         '0.9.1',%0A                         ...%0A                         '2.26.0',%0A                         '2.27.0' %5D%0A                     %7D%0A                 %7D%0A                 */%0A                var remoteVersion = Object.keys(result)%5B0%5D;%0A                var remoteVersions = result%5BremoteVersion%5D.versions;%0A                // pick up %5Bversion%5D%22versions%22%0A                resolve(remoteVersions
 );%0A 
@@ -2926,16 +2926,183 @@
 rsion);%0A
+        // if not found current version in npm, return %60latest%60 version%0A        if(indexOfCurrent == -1) %7B%0A            return versions%5Bversions.length - 1%5D;%0A        %7D%0A
         

support the case that not found current version in npm