"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __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.extractLatestRubyPackageVersion = exports.extractLatestPythonPackageVersion = exports.extractLatestDotNetPackageVersion = exports.extractLatestJavaPackageVersion = exports.extractLatestJsPackageVersion = exports.validateSdkVersion = void 0;
const req_1 = require("@applitools/req");
const lt_1 = __importDefault(require("semver/functions/lt"));
const chalk_1 = __importDefault(require("chalk"));
const utils = __importStar(require("@applitools/utils"));
const lang = __importStar(require("../lang"));
const extractors = {
    js: utils.general.cachify(extractLatestJsPackageVersion),
    java: utils.general.cachify(extractLatestJavaPackageVersion),
    dotnet: utils.general.cachify(extractLatestDotNetPackageVersion),
    python: utils.general.cachify(extractLatestPythonPackageVersion),
    ruby: utils.general.cachify(extractLatestRubyPackageVersion),
};
async function validateSdkVersion(sdk, { logger }) {
    try {
        if (!sdk.latestVersion) {
            logger.log(`Extracting latest version of ${sdk.lang}/${sdk.name}`);
            sdk.latestVersion = await extractors[sdk.lang](sdk.name);
        }
        logger.log(`Latest version of ${sdk.lang}/${sdk.name} is ${sdk.latestVersion}`);
        if ((0, lt_1.default)(sdk.currentVersion, sdk.latestVersion)) {
            const message = lang.outdatedSdkVersionWarning(sdk);
            logger.console.warn(chalk_1.default.yellow(message));
            logger.warn(message);
        }
    }
    catch (error) {
        logger.error(`Failed to extract latest version of ${sdk.lang}/${sdk.name} due to the error: `, error);
    }
}
exports.validateSdkVersion = validateSdkVersion;
async function extractLatestJsPackageVersion(name) {
    const response = await (0, req_1.req)(`https://registry.npmjs.org/${name}/latest`);
    const info = await response.json();
    return info.version;
}
exports.extractLatestJsPackageVersion = extractLatestJsPackageVersion;
async function extractLatestJavaPackageVersion(name) {
    const response = await (0, req_1.req)(`https://search.maven.org/solrsearch/select?q=a:${name}&wt=json`);
    const info = await response.json();
    return info.response.docs[0].latestVersion;
}
exports.extractLatestJavaPackageVersion = extractLatestJavaPackageVersion;
async function extractLatestDotNetPackageVersion(name) {
    const response = await (0, req_1.req)(`https://api.nuget.org/v3-flatcontainer/${name.toLowerCase()}/index.json`);
    const info = await response.json();
    return info.versions[info.versions.length - 1];
}
exports.extractLatestDotNetPackageVersion = extractLatestDotNetPackageVersion;
async function extractLatestPythonPackageVersion(name) {
    const response = await (0, req_1.req)(`https://pypi.org/pypi/${name}/json`);
    const info = await response.json();
    return info.info.version;
}
exports.extractLatestPythonPackageVersion = extractLatestPythonPackageVersion;
async function extractLatestRubyPackageVersion(name) {
    const response = await (0, req_1.req)(`https://rubygems.org/api/v1/gems/${name}.json`);
    const info = await response.json();
    return info.version;
}
exports.extractLatestRubyPackageVersion = extractLatestRubyPackageVersion;
