"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVersions = exports.isSupportedApiVersion = exports.isSupportedDebugApiVersion = exports.isSupportedMainApiVersion = exports.isSupportedExactVersion = exports.isSupportedVersion = exports.getNodeInfo = exports.getHealth = exports.SUPPORTED_BEE_VERSION = exports.SUPPORTED_DEBUG_API_VERSION = exports.SUPPORTED_API_VERSION = exports.SUPPORTED_BEE_VERSION_EXACT = void 0;
const http_1 = require("../../utils/http");
const major_js_1 = __importDefault(require("semver/functions/major.js"));
// Following lines bellow are automatically updated with GitHub Action when Bee version is updated
// so if you are changing anything about them change the `update_bee` action accordingly!
exports.SUPPORTED_BEE_VERSION_EXACT = '1.6.0-6ceadd35';
exports.SUPPORTED_API_VERSION = '3.0.1';
exports.SUPPORTED_DEBUG_API_VERSION = '2.0.1';
exports.SUPPORTED_BEE_VERSION = exports.SUPPORTED_BEE_VERSION_EXACT.substring(0, exports.SUPPORTED_BEE_VERSION_EXACT.indexOf('-'));
const NODE_INFO_URL = 'node';
const HEALTH_URL = 'health';
/**
 * Get health of node
 *
 * @param ky Ky debug instance
 */
function getHealth(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        const response = yield (0, http_1.http)(ky, {
            method: 'get',
            path: HEALTH_URL,
            responseType: 'json',
        });
        return response.data;
    });
}
exports.getHealth = getHealth;
/**
 * Get information about Bee node
 *
 * @param ky Ky debug instance
 */
function getNodeInfo(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        const response = yield (0, http_1.http)(ky, {
            method: 'get',
            path: NODE_INFO_URL,
            responseType: 'json',
        });
        return response.data;
    });
}
exports.getNodeInfo = getNodeInfo;
/**
 * Connects to a node and checks if it is a supported Bee version by the bee-js
 *
 * @param ky Ky debug instance
 *
 * @returns true if the Bee node version is supported
 * @deprecated Use `isSupportedExactVersion` instead
 */
function isSupportedVersion(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        return isSupportedExactVersion(ky);
    });
}
exports.isSupportedVersion = isSupportedVersion;
/**
 * Connects to a node and checks if its version matches with the one that bee-js supports.
 *
 * Be aware that this is the most strict version check and most probably
 * you will want to use more relaxed API-versions based checks like
 * `isSupportedApiVersion`, `isSupportedMainApiVersion` or `isSupportedDebugApiVersion`
 * based on your use-case.
 *
 * @param ky
 */
function isSupportedExactVersion(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        const { version } = yield getHealth(ky);
        return version === exports.SUPPORTED_BEE_VERSION_EXACT;
    });
}
exports.isSupportedExactVersion = isSupportedExactVersion;
/**
 * Connects to a node and checks if its main's API version matches with the one that bee-js supports.
 *
 * This is useful if you are not using `BeeDebug` class (for anything else then this check)
 * and want to make sure about compatibility.
 *
 * @param ky
 */
function isSupportedMainApiVersion(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        const { apiVersion } = yield getHealth(ky);
        return (0, major_js_1.default)(apiVersion) === (0, major_js_1.default)(exports.SUPPORTED_API_VERSION);
    });
}
exports.isSupportedMainApiVersion = isSupportedMainApiVersion;
/**
 * Connects to a node and checks if its Debug API version matches with the one that bee-js supports.
 *
 * This is useful if you are not using `Bee` class in your application and want to make sure
 * about compatibility.
 *
 * @param ky
 */
function isSupportedDebugApiVersion(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        const { debugApiVersion } = yield getHealth(ky);
        return (0, major_js_1.default)(debugApiVersion) === (0, major_js_1.default)(exports.SUPPORTED_DEBUG_API_VERSION);
    });
}
exports.isSupportedDebugApiVersion = isSupportedDebugApiVersion;
/**
 * Connects to a node and checks if its Main and Debug API versions matches with the one that bee-js supports.
 *
 * This should be the main way how to check compatibility for your app and Bee node.
 *
 * @param ky
 */
function isSupportedApiVersion(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        const { apiVersion, debugApiVersion } = yield getHealth(ky);
        return ((0, major_js_1.default)(apiVersion) === (0, major_js_1.default)(exports.SUPPORTED_API_VERSION) &&
            (0, major_js_1.default)(debugApiVersion) === (0, major_js_1.default)(exports.SUPPORTED_DEBUG_API_VERSION));
    });
}
exports.isSupportedApiVersion = isSupportedApiVersion;
/**
 * Returns object with all versions specified by the connected Bee node (properties prefixed with `bee*`)
 * and versions that bee-js supports (properties prefixed with `supported*`).
 *
 * @param ky
 */
function getVersions(ky) {
    return __awaiter(this, void 0, void 0, function* () {
        const { version, apiVersion, debugApiVersion } = yield getHealth(ky);
        return {
            supportedBeeVersion: exports.SUPPORTED_BEE_VERSION_EXACT,
            supportedBeeApiVersion: exports.SUPPORTED_API_VERSION,
            supportedBeeDebugApiVersion: exports.SUPPORTED_DEBUG_API_VERSION,
            beeVersion: version,
            beeApiVersion: apiVersion,
            beeDebugApiVersion: debugApiVersion,
        };
    });
}
exports.getVersions = getVersions;
