"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.labelsColumn = exports.macAddressColumn = exports.ipv6Column = exports.ipv4Column = exports.activeNICColumn = exports.countColumn = exports.gpusColumn = exports.disksColumn = exports.memoryColumn = exports.cpuCoresColumn = exports.cpuArchitectureColumn = exports.discoveredAtColumn = exports.statusColumn = exports.roleColumn = exports.hostnameColumn = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const ip_address_1 = require("ip-address");
const clusterConfiguration_1 = require("../clusterConfiguration");
const react_core_1 = require("@patternfly/react-core");
const selectors_1 = require("../../selectors");
const utils_1 = require("../../utils");
const constants_1 = require("../constants");
const ui_1 = require("../ui");
const hardwareInfo_1 = require("./hardwareInfo");
const Hostname_1 = tslib_1.__importDefault(require("./Hostname"));
const HostPropertyValidationPopover_1 = tslib_1.__importDefault(require("./HostPropertyValidationPopover"));
const HostsCount_1 = tslib_1.__importDefault(require("./HostsCount"));
const HostStatus_1 = tslib_1.__importDefault(require("./HostStatus"));
const RoleCell_1 = tslib_1.__importDefault(require("./RoleCell"));
const status_1 = require("./status");
const utils_2 = require("./utils");
const getSelectedNic = (nics, currentSubnet) => {
    return nics.find((nic) => {
        const ipv4Addresses = (nic.ipv4Addresses || []).reduce((addresses, address) => {
            if (ip_address_1.Address4.isValid(address)) {
                addresses.push(new ip_address_1.Address4(address));
            }
            return addresses;
        }, []);
        if (ipv4Addresses.find((address) => address.isInSubnet(currentSubnet))) {
            return true;
        }
        const ipv6Addresses = (nic.ipv6Addresses || []).reduce((addresses, address) => {
            if (ip_address_1.Address6.isValid(address)) {
                addresses.push(new ip_address_1.Address6(address));
            }
            return addresses;
        }, []);
        return ipv6Addresses.find((address) => address.isInSubnet(currentSubnet));
    });
};
const hostnameColumn = (t, onEditHostname, hosts, canEditHostname) => {
    return {
        header: {
            title: t('ai:Hostname'),
            props: {
                id: 'col-header-hostname', // ACM jest tests require id over testId
            },
            sort: true,
        },
        cell: (host) => {
            const inventory = (0, utils_2.getInventory)(host);
            const editHostname = onEditHostname ? () => onEditHostname(host) : undefined;
            const computedHostname = (0, utils_2.getHostname)(host, inventory);
            return {
                title: (react_1.default.createElement(Hostname_1.default, { host: host, inventory: inventory, onEditHostname: editHostname, hosts: hosts, readonly: canEditHostname ? !canEditHostname(host) : false })),
                props: { 'data-testid': 'host-name' },
                sortableValue: computedHostname || '',
            };
        },
    };
};
exports.hostnameColumn = hostnameColumn;
const roleColumn = (t, canEditRole, onEditRole, schedulableMasters, clusterKind) => {
    return {
        header: {
            title: t('ai:Role'),
            props: {
                id: 'col-header-role',
            },
            sort: true,
        },
        cell: (host) => {
            const editRole = onEditRole
                ? (role) => onEditRole(host, role)
                : undefined;
            const isRoleEditable = canEditRole === null || canEditRole === void 0 ? void 0 : canEditRole(host);
            const hostRole = (0, utils_2.getHostRole)(host, t, schedulableMasters, clusterKind);
            return {
                title: (react_1.default.createElement(RoleCell_1.default, { host: host, readonly: !isRoleEditable, role: hostRole, onEditRole: editRole })),
                props: { 'data-testid': 'host-role' },
                sortableValue: hostRole,
            };
        },
    };
};
exports.roleColumn = roleColumn;
const statusColumn = (t, clusterStatus, AdditionalNTPSourcesDialogToggleComponent, onEditHostname, UpdateDay2ApiVipDialogToggleComponent) => {
    return {
        header: {
            title: t('ai:Status'),
            props: {
                id: 'col-header-status',
            },
            sort: true,
        },
        cell: (host) => {
            const validationsInfo = (0, utils_1.stringToJSON)(host.validationsInfo) || {};
            const editHostname = onEditHostname ? () => onEditHostname(host) : undefined;
            const sublabel = (0, utils_2.areOnlySoftValidationsFailing)(validationsInfo) &&
                ['known', 'known-unbound'].includes(host.status)
                ? t('ai:Some validations failed')
                : undefined;
            const actualHostStatus = (0, utils_2.getHostStatus)(host.status, clusterStatus);
            return {
                title: (react_1.default.createElement(HostStatus_1.default, { host: host, status: Object.assign(Object.assign({}, (0, status_1.hostStatus)(t)[actualHostStatus]), { sublabel }), onEditHostname: editHostname, validationsInfo: validationsInfo, AdditionalNTPSourcesDialogToggleComponent: AdditionalNTPSourcesDialogToggleComponent, UpdateDay2ApiVipDialogToggleComponent: UpdateDay2ApiVipDialogToggleComponent })),
                props: { 'data-testid': 'host-status' },
                sortableValue: host.status,
            };
        },
    };
};
exports.statusColumn = statusColumn;
const discoveredAtColumn = (t) => ({
    header: {
        title: t('ai:Discovered on'),
        props: {
            id: 'col-header-discoveredat',
        },
        sort: true,
    },
    cell: (host) => {
        const { createdAt } = host;
        const dateTimeCell = (0, ui_1.getDateTimeCell)(createdAt);
        return {
            title: dateTimeCell.title,
            props: { 'data-testid': 'host-discovered-time' },
            sortableValue: dateTimeCell.sortableValue,
        };
    },
});
exports.discoveredAtColumn = discoveredAtColumn;
const cpuArchitectureColumn = (t) => ({
    header: {
        title: t('ai:CPU Architecture'),
        props: {
            id: 'col-header-cpuarchitecture',
        },
        sort: true,
    },
    cell: (host) => {
        var _a, _b;
        const inventory = (0, utils_2.getInventory)(host);
        return {
            title: (_a = inventory.cpu) === null || _a === void 0 ? void 0 : _a.architecture,
            props: { 'data-testid': 'host-cpu-architecture' },
            sortableValue: (_b = inventory.cpu) === null || _b === void 0 ? void 0 : _b.architecture,
        };
    },
});
exports.cpuArchitectureColumn = cpuArchitectureColumn;
const cpuCoresColumn = (t) => ({
    header: {
        title: t('ai:CPU Cores'),
        props: {
            id: 'col-header-cpucores',
        },
        sort: true,
    },
    cell: (host) => {
        var _a;
        const inventory = (0, utils_2.getInventory)(host);
        const { cores } = (0, hardwareInfo_1.getHostRowHardwareInfo)(inventory);
        const validationsInfo = (0, utils_1.stringToJSON)(host.validationsInfo) || {};
        const cpuCoresValidation = (_a = validationsInfo === null || validationsInfo === void 0 ? void 0 : validationsInfo.hardware) === null || _a === void 0 ? void 0 : _a.find((v) => v.id === 'has-cpu-cores-for-role');
        return {
            title: (react_1.default.createElement(HostPropertyValidationPopover_1.default, { validation: cpuCoresValidation }, cores.title)),
            props: { 'data-testid': 'host-cpu-cores' },
            sortableValue: cores.sortableValue,
        };
    },
});
exports.cpuCoresColumn = cpuCoresColumn;
const memoryColumn = (t) => ({
    header: {
        title: t('ai:Memory'),
        props: {
            id: 'col-header-memory',
        },
        sort: true,
    },
    cell: (host) => {
        var _a;
        const inventory = (0, utils_2.getInventory)(host);
        const { memory } = (0, hardwareInfo_1.getHostRowHardwareInfo)(inventory);
        const validationsInfo = (0, utils_1.stringToJSON)(host.validationsInfo) || {};
        const memoryValidation = (_a = validationsInfo === null || validationsInfo === void 0 ? void 0 : validationsInfo.hardware) === null || _a === void 0 ? void 0 : _a.find((v) => v.id === 'has-memory-for-role');
        return {
            title: (react_1.default.createElement(HostPropertyValidationPopover_1.default, { validation: memoryValidation }, memory.title)),
            props: { 'data-testid': 'host-memory' },
            sortableValue: memory.sortableValue,
        };
    },
});
exports.memoryColumn = memoryColumn;
const disksColumn = (t) => ({
    header: {
        title: t('ai:Total storage'),
        props: {
            id: 'col-header-disk',
        },
        sort: true,
    },
    cell: (host) => {
        var _a;
        const inventory = (0, utils_2.getInventory)(host);
        const { disk } = (0, hardwareInfo_1.getHostRowHardwareInfo)(inventory);
        const validationsInfo = (0, utils_1.stringToJSON)(host.validationsInfo) || {};
        const diskValidation = (_a = validationsInfo === null || validationsInfo === void 0 ? void 0 : validationsInfo.hardware) === null || _a === void 0 ? void 0 : _a.find((v) => v.id === 'has-min-valid-disks');
        return {
            title: (react_1.default.createElement(HostPropertyValidationPopover_1.default, { validation: diskValidation }, disk.title)),
            props: { 'data-testid': 'host-disks' },
            sortableValue: disk.sortableValue,
        };
    },
});
exports.disksColumn = disksColumn;
const gpusColumn = (t) => ({
    header: {
        title: t('ai:GPUs'),
        props: {
            id: 'col-header-gpus',
        },
        sort: true,
    },
    cell: (host) => {
        var _a, _b;
        const { gpus } = (0, utils_2.getInventory)(host);
        return {
            title: react_1.default.createElement(react_1.default.Fragment, null, (_a = gpus === null || gpus === void 0 ? void 0 : gpus.length) !== null && _a !== void 0 ? _a : '--'),
            props: { 'data-testid': 'host-gpus' },
            sortableValue: (_b = gpus === null || gpus === void 0 ? void 0 : gpus.length) !== null && _b !== void 0 ? _b : 0,
        };
    },
});
exports.gpusColumn = gpusColumn;
const countColumn = (cluster) => ({
    header: { title: react_1.default.createElement(HostsCount_1.default, { cluster: cluster, inParenthesis: true }) },
});
exports.countColumn = countColumn;
const activeNICColumn = (cluster, t) => ({
    header: {
        title: t('ai:Active NIC'),
        sort: true,
    },
    cell: (host) => {
        const inventory = (0, utils_2.getInventory)(host);
        const nics = inventory.interfaces || [];
        const machineNetworkCidr = (0, selectors_1.selectMachineNetworkCIDR)(cluster);
        const currentSubnet = machineNetworkCidr ? (0, clusterConfiguration_1.getSubnet)(machineNetworkCidr) : null;
        const selectedNic = currentSubnet ? getSelectedNic(nics, currentSubnet) : null;
        return {
            title: (selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.name) || constants_1.DASH,
            props: { 'data-testid': 'nic-name' },
            sortableValue: (selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.name) || constants_1.DASH,
        };
    },
});
exports.activeNICColumn = activeNICColumn;
const ipv4Column = (cluster) => ({
    header: {
        title: 'IPv4 address',
        sort: true,
    },
    cell: (host) => {
        const inventory = (0, utils_2.getInventory)(host);
        const nics = inventory.interfaces || [];
        const machineNetworkCidr = (0, selectors_1.selectMachineNetworkCIDR)(cluster);
        const currentSubnet = machineNetworkCidr ? (0, clusterConfiguration_1.getSubnet)(machineNetworkCidr) : null;
        const selectedNic = currentSubnet ? getSelectedNic(nics, currentSubnet) : null;
        return {
            title: ((selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.ipv4Addresses) || []).join(', ') || constants_1.DASH,
            props: { 'data-testid': 'nic-ipv4' },
            sortableValue: ((selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.ipv4Addresses) || []).join(', ') || constants_1.DASH,
        };
    },
});
exports.ipv4Column = ipv4Column;
const ipv6Column = (cluster) => ({
    header: {
        title: 'IPv6 address',
        sort: true,
    },
    cell: (host) => {
        const inventory = (0, utils_2.getInventory)(host);
        const nics = inventory.interfaces || [];
        const machineNetworkCidr = (0, selectors_1.selectMachineNetworkCIDR)(cluster);
        const currentSubnet = machineNetworkCidr ? (0, clusterConfiguration_1.getSubnet)(machineNetworkCidr) : null;
        const selectedNic = currentSubnet ? getSelectedNic(nics, currentSubnet) : null;
        return {
            title: ((selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.ipv6Addresses) || []).join(', ') || constants_1.DASH,
            props: { 'data-testid': 'nic-ipv6' },
            sortableValue: ((selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.ipv6Addresses) || []).join(', ') || constants_1.DASH,
        };
    },
});
exports.ipv6Column = ipv6Column;
const macAddressColumn = (cluster) => ({
    header: {
        title: 'MAC address',
        sort: true,
    },
    cell: (host) => {
        const inventory = (0, utils_2.getInventory)(host);
        const nics = inventory.interfaces || [];
        const machineNetworkCidr = (0, selectors_1.selectMachineNetworkCIDR)(cluster);
        const currentSubnet = machineNetworkCidr ? (0, clusterConfiguration_1.getSubnet)(machineNetworkCidr) : null;
        const selectedNic = currentSubnet ? getSelectedNic(nics, currentSubnet) : null;
        return {
            title: (selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.macAddress) || constants_1.DASH,
            props: { 'data-testid': 'nic-mac-address' },
            sortableValue: (selectedNic === null || selectedNic === void 0 ? void 0 : selectedNic.macAddress) || constants_1.DASH,
        };
    },
});
exports.macAddressColumn = macAddressColumn;
const labelsColumn = (t) => ({
    header: {
        title: t('ai:Labels'),
        props: {
            id: 'col-header-labels',
        },
        sort: false,
    },
    cell: (host) => {
        const labels = (0, utils_2.getHostLabels)(host);
        return {
            title: Object.entries(labels).length > 0 ? (react_1.default.createElement(react_core_1.LabelGroup, null, Object.entries(labels).map(([key, value], index) => (react_1.default.createElement(react_core_1.Label, { key: `${host.id}-host-label-${index}`, isCompact: true },
                key,
                " = ",
                value))))) : ('--'),
            props: { 'data-testid': 'host-labels' },
        };
    },
});
exports.labelsColumn = labelsColumn;
//# sourceMappingURL=tableColumns.js.map