"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onAgentChangeHostname = exports.getClusterNameOfAgent = exports.getInfraEnvNameOfAgent = exports.getAgentProgress = exports.getAgentRole = exports.getAgentsForSelection = exports.hostToAgent = void 0;
const status_1 = require("./status");
const constants_1 = require("../common/constants");
const AGENT_FOR_SELECTION_STATUSES = [
    'known',
    'known-unbound',
    'insufficient',
    'pending-for-input',
    'binding',
    'unbinding',
    'discovering',
    'reclaiming',
    'reclaiming-rebooting',
];
const hostToAgent = (agents = [], host) => agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
exports.hostToAgent = hostToAgent;
const getAgentsForSelection = (agents, isSubmitting) => agents.filter((agent) => {
    const key = (0, status_1.getAgentStatusKey)(agent);
    if (isSubmitting) {
        return [...AGENT_FOR_SELECTION_STATUSES, 'specSyncErr'].includes(key);
    }
    return AGENT_FOR_SELECTION_STATUSES.includes(key);
});
exports.getAgentsForSelection = getAgentsForSelection;
const getAgentRole = (agent) => { var _a; return agent.spec.role || ((_a = agent.status) === null || _a === void 0 ? void 0 : _a.role); };
exports.getAgentRole = getAgentRole;
const getAgentProgress = (agent) => { var _a; return (_a = agent.status) === null || _a === void 0 ? void 0 : _a.progress; };
exports.getAgentProgress = getAgentProgress;
const getInfraEnvNameOfAgent = (resource) => { var _a, _b; return (_b = (_a = resource === null || resource === void 0 ? void 0 : resource.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b[constants_1.INFRAENV_AGENTINSTALL_LABEL_KEY]; };
exports.getInfraEnvNameOfAgent = getInfraEnvNameOfAgent;
const getClusterNameOfAgent = (agent) => { var _a, _b; return (_b = (_a = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _a === void 0 ? void 0 : _a.clusterDeploymentName) === null || _b === void 0 ? void 0 : _b.name; };
exports.getClusterNameOfAgent = getClusterNameOfAgent;
const onAgentChangeHostname = (agents, bmhs, onChangeHostname, onChangeBMHHostname) => (host, hostname) => {
    var _a, _b;
    const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
    let bmh;
    if (agent) {
        const bmhName = (_b = (_a = agent.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b[constants_1.AGENT_BMH_NAME_LABEL_KEY];
        if (!bmhName) {
            return onChangeHostname(agent, hostname);
        }
        // If Agent is backed by BMH, we need to update hostname on BMH
        bmh = bmhs.find((bmh) => { var _a, _b, _c; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.namespace) === ((_b = agent.metadata) === null || _b === void 0 ? void 0 : _b.namespace) && ((_c = bmh.metadata) === null || _c === void 0 ? void 0 : _c.name) === bmhName; });
    }
    else {
        bmh = bmhs.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
    }
    return bmh ? onChangeBMHHostname(bmh, hostname) : Promise.resolve(undefined);
};
exports.onAgentChangeHostname = onAgentChangeHostname;
//# sourceMappingURL=agents.js.map