"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HostDetail = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const react_table_1 = require("@patternfly/react-table");
const ui_1 = require("../ui");
const constants_1 = require("../constants");
const hardwareInfo_1 = require("./hardwareInfo");
const utils_1 = require("./utils");
const NtpValidationStatus_1 = tslib_1.__importDefault(require("./NtpValidationStatus"));
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const SectionTitle_1 = tslib_1.__importDefault(require("../ui/SectionTitle"));
const StorageDetail_1 = tslib_1.__importDefault(require("../storage/StorageDetail"));
const utils_2 = require("../../utils");
const SectionColumn = ({ children }) => (react_1.default.createElement(react_core_1.GridItem, { span: 4 },
    react_1.default.createElement(ui_1.DetailList, null, children)));
const nicsColumns = (t) => [
    { title: t('ai:Name') },
    { title: t('ai:MAC address') },
    { title: t('ai:IPv4 address') },
    { title: t('ai:IPv6 address') },
    { title: t('ai:Speed') },
    // { title: 'Vendor' }, TODO(mlibra): search HW database for humanized values
    // { title: 'Product' },
];
const NicsTable = ({ interfaces, testId }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const rows = interfaces
        .sort((nicA, nicB) => { var _a; return ((_a = nicA.name) === null || _a === void 0 ? void 0 : _a.localeCompare(nicB.name || '')) || 0; })
        .map((nic) => ({
        cells: [
            { title: nic.name, props: { 'data-testid': 'nic-name' } },
            { title: nic.macAddress, props: { 'data-testid': 'nic-mac' } },
            {
                title: (nic.ipv4Addresses || []).join(', '),
                props: { 'data-testid': 'nic-ipv4' },
            },
            {
                title: (nic.ipv6Addresses || []).join(', '),
                props: { 'data-testid': 'nic-ipv6' },
            },
            {
                title: nic.speedMbps && nic.speedMbps > 0 ? `${nic.speedMbps} Mbps` : 'N/A',
                props: { 'data-testid': 'nic-speed' },
            },
        ],
        key: nic.name,
    }));
    return (react_1.default.createElement(react_table_1.Table, { "data-testid": testId, variant: react_table_1.TableVariant.compact, "aria-label": t("ai:Host's network interfaces table"), borders: false },
        react_1.default.createElement(react_table_1.Thead, null,
            react_1.default.createElement(react_table_1.Tr, null, nicsColumns(t).map((col, i) => (react_1.default.createElement(react_table_1.Th, { key: `col-${i}` }, col.title))))),
        react_1.default.createElement(react_table_1.Tbody, null, rows.map((row, i) => {
            var _a;
            return (react_1.default.createElement(react_table_1.Tr, { key: `nic-row:${row.key}` }, (_a = row.cells) === null || _a === void 0 ? void 0 : _a.map((cell, j) => (react_1.default.createElement(react_table_1.Td, Object.assign({ key: `cell-${i}-${j}` }, cell.props), cell.title)))));
        }))));
};
const gpusColumns = (t) => [
    { title: t('ai:Vendor') },
    { title: t('ai:Vendor ID') },
    { title: t('ai:Model') },
    { title: t('ai:Device ID') },
    { title: t('ai:Address') },
];
const GpusTable = ({ gpus: gpus, testId }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const rows = gpus
        .sort((gpuA, gpuB) => { var _a; return ((_a = gpuA.name) === null || _a === void 0 ? void 0 : _a.localeCompare(gpuB.name || '')) || 0; })
        .map((gpu) => ({
        cells: [
            { title: gpu.vendor, props: { 'data-testid': 'gpu-vendor' } },
            { title: gpu.vendorId, props: { 'data-testid': 'gpu-vendor-id' } },
            { title: gpu.name, props: { 'data-testid': 'gpu-name' } },
            { title: gpu.deviceId, props: { 'data-testid': 'gpu-device-id' } },
            { title: gpu.address, props: { 'data-testid': 'gpu-address' } },
        ],
        key: gpu.name,
    }));
    return (react_1.default.createElement(react_table_1.Table, { "data-testid": testId, variant: react_table_1.TableVariant.compact, "aria-label": t("ai:Host's graphics processing units table"), borders: false },
        react_1.default.createElement(react_table_1.Thead, null,
            react_1.default.createElement(react_table_1.Tr, null, gpusColumns(t).map((col, i) => (react_1.default.createElement(react_table_1.Th, { key: `col-${i}` }, col.title))))),
        react_1.default.createElement(react_table_1.Tbody, null, rows.map((row, i) => {
            var _a;
            return (react_1.default.createElement(react_table_1.Tr, { key: `gpu-row:${row.key}` }, (_a = row.cells) === null || _a === void 0 ? void 0 : _a.map((cell, j) => (react_1.default.createElement(react_table_1.Td, Object.assign({ key: `cell-${i}-${j}` }, cell.props), cell.title)))));
        }))));
};
const HostDetail = ({ host, canEditDisks, onDiskRole, AdditionalNTPSourcesDialogToggleComponent, hideNTPStatus = false, updateDiskSkipFormatting, }) => {
    var _a, _b, _c, _d, _e, _f, _g;
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const { id, validationsInfo: hostValidationsInfo } = host;
    const inventory = (0, utils_1.getInventory)(host);
    const validationsInfo = react_1.default.useMemo(() => (0, utils_2.stringToJSON)(hostValidationsInfo) || {}, [hostValidationsInfo]);
    const rowInfo = (0, hardwareInfo_1.getHostRowHardwareInfo)(inventory);
    const nics = inventory.interfaces || [];
    const gpus = inventory.gpus || [];
    let bmcAddress = inventory.bmcAddress;
    if (inventory.bmcV6address) {
        bmcAddress = bmcAddress ? `${bmcAddress}, ${inventory.bmcV6address}` : inventory.bmcV6address;
    }
    bmcAddress = bmcAddress || constants_1.DASH;
    const hardwareType = (0, utils_1.getHardwareTypeText)(inventory, t);
    const ntpValidationStatus = react_1.default.useMemo(() => (react_1.default.createElement(NtpValidationStatus_1.default, { AdditionalNTPSourcesDialogToggleComponent: AdditionalNTPSourcesDialogToggleComponent, validationsInfo: validationsInfo })), [AdditionalNTPSourcesDialogToggleComponent, validationsInfo]);
    return (react_1.default.createElement(react_core_1.Grid, { hasGutter: true },
        react_1.default.createElement(SectionTitle_1.default, { testId: 'host-details-section', title: t('ai:Host details') }),
        react_1.default.createElement(SectionColumn, null,
            react_1.default.createElement(ui_1.DetailItem, { testId: 'uuid', title: t('ai:UUID'), value: id }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'manufacturer', title: t('ai:Manufacturer'), value: ((_a = inventory.systemVendor) === null || _a === void 0 ? void 0 : _a.manufacturer) || constants_1.DASH }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'product', title: t('ai:Product'), value: ((_b = inventory.systemVendor) === null || _b === void 0 ? void 0 : _b.productName) || constants_1.DASH }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'serial-number', title: t('ai:Serial number'), value: rowInfo.serialNumber })),
        react_1.default.createElement(SectionColumn, null,
            react_1.default.createElement(ui_1.DetailItem, { testId: 'memory-capacity', title: t('ai:Memory capacity'), value: rowInfo.memory.title }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'cpu-model', title: t('ai:CPU model name'), value: ((_c = inventory.cpu) === null || _c === void 0 ? void 0 : _c.modelName) || constants_1.DASH }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'cpu-cores-and-clock', title: t('ai:CPU cores and clock speed'), value: rowInfo.cpuSpeed }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'cpu-arch', title: t('ai:CPU architecture'), value: ((_d = inventory.cpu) === null || _d === void 0 ? void 0 : _d.architecture) || constants_1.DASH })),
        react_1.default.createElement(SectionColumn, null,
            react_1.default.createElement(ui_1.DetailItem, { testId: 'hw-type', title: t('ai:Hardware type'), value: hardwareType }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'bmc-address', title: t('ai:BMC address'), value: bmcAddress }),
            react_1.default.createElement(ui_1.DetailItem, { testId: 'boot-mode', title: t('ai:Boot mode'), value: ((_e = inventory.boot) === null || _e === void 0 ? void 0 : _e.currentBootMode) || constants_1.DASH }),
            ((_f = inventory.boot) === null || _f === void 0 ? void 0 : _f.pxeInterface) && (react_1.default.createElement(ui_1.DetailItem, { testId: 'pxe-interface', title: t('ai:PXE interface'), value: (_g = inventory.boot) === null || _g === void 0 ? void 0 : _g.pxeInterface })),
            react_1.default.createElement(ui_1.DetailItem, { isHidden: hideNTPStatus, testId: 'ntp-status', title: t('ai:NTP status'), value: ntpValidationStatus })),
        react_1.default.createElement(StorageDetail_1.default, { host: host, onDiskRole: onDiskRole, canEditDisks: canEditDisks, updateDiskSkipFormatting: updateDiskSkipFormatting }),
        react_1.default.createElement(SectionTitle_1.default, { testId: 'nics-section', title: t('ai:NIC', { count: nics.length }) }),
        react_1.default.createElement(react_core_1.GridItem, null,
            react_1.default.createElement(NicsTable, { interfaces: nics, testId: 'nics-table' })),
        gpus.length > 0 && (react_1.default.createElement("div", null,
            react_1.default.createElement(SectionTitle_1.default, { testId: 'gpus-section', title: t('ai:GPU', { count: gpus.length }) }),
            react_1.default.createElement(react_core_1.GridItem, null,
                react_1.default.createElement(GpusTable, { gpus: gpus, testId: 'gpus-table' }))))));
};
exports.HostDetail = HostDetail;
//# sourceMappingURL=HostRowDetail.js.map