"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareAgentExternalVersions = exports.compareSemVer = exports.AgentsHealthStatus = exports.AgentStatus = exports.getAgentTypeName = exports.AgentType = void 0;
const semver = __importStar(require("semver"));
var AgentType;
(function (AgentType) {
    AgentType[AgentType["MULTITENANT"] = 0] = "MULTITENANT";
    AgentType[AgentType["DEDICATED"] = 1] = "DEDICATED";
    AgentType[AgentType["ONPREMISE"] = 2] = "ONPREMISE";
})(AgentType || (exports.AgentType = AgentType = {}));
// This function returns a descriptive name for each known agent type
const getAgentTypeName = (agentType) => {
    switch (agentType) {
        case AgentType.DEDICATED:
        case AgentType.MULTITENANT:
            return 'Cloud';
        case AgentType.ONPREMISE:
            return 'On-premise';
    }
};
exports.getAgentTypeName = getAgentTypeName;
var AgentStatus;
(function (AgentStatus) {
    AgentStatus["ACTIVE"] = "Active";
    AgentStatus["DISCONNECTED"] = "Disconnected";
    AgentStatus["BROWSER_UNREACHABLE"] = "Browser Unreachable";
    // TODO: remove PENDING_REGISTRATION after the DB migration
    AgentStatus["PENDING_REGISTRATION"] = "Pending Registration";
    AgentStatus["STALE"] = "Stale";
})(AgentStatus || (exports.AgentStatus = AgentStatus = {}));
var AgentsHealthStatus;
(function (AgentsHealthStatus) {
    AgentsHealthStatus["HEALTHY"] = "HEALTHY";
    /* No connected agents exist */
    AgentsHealthStatus["NO_AGENTS"] = "NO_AGENTS";
    /* There are connected agents, but none for the current profile */
    AgentsHealthStatus["NO_AGENTS_FOR_PROFILE"] = "NO_AGENTS_FOR_PROFILE";
    /* There are connected agents for the current profile, but none are reachable from the browser */
    AgentsHealthStatus["NO_REACHABLE_AGENTS"] = "NO_REACHABLE_AGENTS";
    /* The health of the agents is unknown */
    AgentsHealthStatus["UNKNOWN"] = "UNKNOWN";
})(AgentsHealthStatus || (exports.AgentsHealthStatus = AgentsHealthStatus = {}));
const compareSemVer = (version1, version2) => {
    if (!semver.valid(version1)) {
        throw new Error(`Invalid semver ${version1}.`);
    }
    if (!semver.valid(version2)) {
        throw new Error(`Invalid semver ${version2}.`);
    }
    return semver.compare(version1, version2);
};
exports.compareSemVer = compareSemVer;
/**
 * Return a negative number if version1 is lower than version2;
 * positive if the version1 is greater than version2;
 * 0 if they are equivalent.
 *
 * If the versions are both invalid, return 0.
 * Otherwise, the valid version is treated as greater than an invalid version.
 */
const compareAgentExternalVersions = ({ currentVersion, minVersion }) => {
    const isCurrentVersionValid = semver.valid(currentVersion);
    const isMinVersionValid = semver.valid(minVersion);
    // If both versions are invalid, return 0.
    if (!isCurrentVersionValid && !isMinVersionValid) {
        return 0;
    }
    if (!isCurrentVersionValid) {
        // If the current version is invalid, treat it as lower than the min version.
        return -1;
    }
    else if (!isMinVersionValid) {
        // If the min version is invalid, treat it as lower than the current version.
        return 1;
    }
    return semver.compare(currentVersion, minVersion);
};
exports.compareAgentExternalVersions = compareAgentExternalVersions;
//# sourceMappingURL=agent.js.map