"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDiskLimitation = exports.DisksTable = 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 DiskRole_1 = tslib_1.__importDefault(require("../hosts/DiskRole"));
const DiskLimitations_1 = tslib_1.__importDefault(require("../hosts/DiskLimitations"));
const exclamation_triangle_icon_1 = require("@patternfly/react-icons/dist/js/icons/exclamation-triangle-icon");
const FormatDiskCheckbox_1 = tslib_1.__importStar(require("../hosts/FormatDiskCheckbox"));
const utils_1 = require("../../utils");
const ui_1 = require("../ui");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const diskColumns = (t, showFormat) => [
    { title: t('ai:Name') },
    { title: t('ai:Role') },
    { title: t('ai:Limitations') },
    showFormat ? { title: t('ai:Format?') } : '',
    { title: t('ai:Drive type') },
    { title: t('ai:Size') },
    { title: t('ai:Serial') },
    { title: t('ai:Model') },
    {
        title: (react_1.default.createElement(react_1.default.Fragment, null,
            "WWN",
            ' ',
            react_1.default.createElement(ui_1.PopoverIcon, { bodyContent: t('ai:World Wide Name (WWN) is a unique disk identifier.') }))),
    },
].filter(Boolean);
const SkipFormattingDisk = () => (react_1.default.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.p }, "This bootable disk will skip formatting"));
const getDiskLimitation = (diskName, hostName, holder) => {
    if (holder.driveType) {
        switch (holder.driveType) {
            case 'LVM':
                return `LVM logical volumes were found on the installation disk ${diskName} selected for host ${hostName} and will be deleted during installation.`;
            case 'RAID':
                return `The installation disk ${diskName} selected for host ${hostName}, is part of a software RAID that will be deleted during the installation.`;
            case 'Multipath':
                return `The installation disk ${diskName} selected for host ${hostName} is managed by multipath. We strongly recommend using the multipath device ${holder.name} to improve reliability.`;
        }
    }
};
exports.getDiskLimitation = getDiskLimitation;
const DiskName = ({ disk, disks, host, installationDiskId, }) => {
    var _a;
    const isIndented = ((_a = disk.holders) === null || _a === void 0 ? void 0 : _a.split(',').length) === 1;
    let diskLimitations = null;
    if (disk.id === installationDiskId) {
        const parentDisk = disks.find((d) => { var _a; return (_a = disk.holders) === null || _a === void 0 ? void 0 : _a.split(',').includes(d.name); });
        if (parentDisk) {
            diskLimitations = getDiskLimitation(disk.name, host.requestedHostname, parentDisk);
        }
    }
    return (react_1.default.createElement(react_1.default.Fragment, null,
        isIndented && react_1.default.createElement("span", { style: { width: '1rem', display: 'inline-block' } }),
        (0, FormatDiskCheckbox_1.isInDiskSkipFormattingList)(host, disk.id) && (react_1.default.createElement(react_core_1.Popover, { bodyContent: react_1.default.createElement(SkipFormattingDisk, null), minWidth: "20rem", maxWidth: "30rem" },
            react_1.default.createElement(react_core_1.Icon, { size: "sm", status: "warning" },
                react_1.default.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null)))),
        '   ',
        disk.bootable ? `${disk.name || ''} (bootable)` : disk.name,
        diskLimitations && (react_1.default.createElement(react_1.default.Fragment, null,
            '    ',
            react_1.default.createElement(react_core_1.Popover, { headerContent: "Warning", bodyContent: react_1.default.createElement(react_core_1.Alert, { variant: react_core_1.AlertVariant.warning, isInline: true, title: diskLimitations }), minWidth: "20rem", maxWidth: "30rem", "data-testid": "disk-limitations-popover" },
                react_1.default.createElement(react_core_1.Icon, { size: "sm", status: "warning" },
                    react_1.default.createElement(exclamation_triangle_icon_1.ExclamationTriangleIcon, null)))))));
};
const DisksTable = ({ canEditDisks, host, disks, installationDiskId, testId, onDiskRole, updateDiskSkipFormatting, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const isEditable = !!(canEditDisks === null || canEditDisks === void 0 ? void 0 : canEditDisks(host));
    const diskColumnTitles = diskColumns(t, isEditable);
    const rows = disks
        .filter((disk) => disk.driveType !== 'LVM')
        .sort((a, b) => (a.name && a.name.localeCompare(b.name)) || 0)
        .sort((a, b) => {
        const aVal = (a.holders || a.name);
        const bVal = (b.holders || b.name);
        return (aVal === null || aVal === void 0 ? void 0 : aVal.localeCompare(bVal)) || 0;
    })
        .map((disk, index) => {
        const rowCells = [
            {
                title: (react_1.default.createElement(DiskName, { disk: disk, disks: disks, host: host, installationDiskId: installationDiskId })),
                props: { 'data-testid': 'disk-name' },
            },
            {
                title: (react_1.default.createElement(DiskRole_1.default, { host: host, disk: disk, installationDiskId: installationDiskId, isEditable: isEditable, onDiskRole: onDiskRole })),
                props: { 'data-testid': 'disk-role' },
            },
            { title: react_1.default.createElement(DiskLimitations_1.default, { disk: disk }), props: { 'data-testid': 'disk-limitations' } },
            isEditable
                ? {
                    title: (react_1.default.createElement(FormatDiskCheckbox_1.default, { host: host, diskId: disk.id, installationDiskId: installationDiskId, index: index, updateDiskSkipFormatting: updateDiskSkipFormatting })),
                    props: { 'data-testid': 'disk-formatted' },
                }
                : null,
            { title: disk.driveType, props: { 'data-testid': 'drive-type' } },
            { title: (0, utils_1.fileSize)(disk.sizeBytes || 0, 2, 'si'), props: { 'data-testid': 'disk-size' } },
            { title: disk.serial, props: { 'data-testid': 'disk-serial' } },
            { title: disk.model, props: { 'data-testid': 'disk-model' } },
            { title: disk.wwn, props: { 'data-testid': 'disk-wwn' } },
        ].filter(Boolean); // Remove null values to keep alignment
        return { key: disk.path, cells: rowCells };
    });
    return (react_1.default.createElement(react_table_1.Table, { "data-testid": testId, variant: react_table_1.TableVariant.compact, "aria-label": "Host's disks table" },
        react_1.default.createElement(react_table_1.Thead, null,
            react_1.default.createElement(react_table_1.Tr, null, diskColumnTitles.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) => (
        // eslint-disable-next-line no-console
        react_1.default.createElement(react_table_1.Tr, { key: `disk-row-${row.key}`, "data-testid": `disk-row-${row.key}-host-${host.id}` }, row.cells.map((cell, j) => (react_1.default.createElement(react_table_1.Td, Object.assign({ key: `cell-${i}-${j}` }, cell.props), cell.title)))))))));
};
exports.DisksTable = DisksTable;
//# sourceMappingURL=DisksTable.js.map