"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDateTimeCell = exports.toSentence = exports.rowSorter = exports.getColSpanRow = exports.genericTableRowKey = void 0;
const tslib_1 = require("tslib");
const upperFirst_js_1 = tslib_1.__importDefault(require("lodash-es/upperFirst.js"));
const endsWith_js_1 = tslib_1.__importDefault(require("lodash-es/endsWith.js"));
const react_table_1 = require("@patternfly/react-table");
const utils_1 = require("../utils");
const genericTableRowKey = (rowId) => `row-key${rowId}`;
exports.genericTableRowKey = genericTableRowKey;
/**
 * Generates rows array for item which spans across all table columns.
 * Used to put EmptyState etc. components into the table body.
 */
const getColSpanRow = (content, columnCount) => [
    {
        heightAuto: true,
        cells: [
            {
                props: { colSpan: columnCount },
                title: content,
            },
        ],
    },
];
exports.getColSpanRow = getColSpanRow;
const rowSorter = (sortBy, getCell) => (a, b) => {
    const coefficient = sortBy.direction === react_table_1.SortByDirection.asc ? 1 : -1;
    const cellA = getCell(a, sortBy.index);
    const cellB = getCell(b, sortBy.index);
    let valA = typeof cellA === 'string' ? cellA : cellA === null || cellA === void 0 ? void 0 : cellA.sortableValue;
    let valB = typeof cellB === 'string' ? cellB : cellB === null || cellB === void 0 ? void 0 : cellB.sortableValue;
    if (typeof valA === 'string' || typeof valB === 'string') {
        valA = (valA || ''); // handle undefined
        return valA.localeCompare(valB) * coefficient;
    }
    // numeric (like timestamp or memory)
    valA = valA || 0; // handle undefined
    valB = valB || 0;
    return (valA - valB) * coefficient;
};
exports.rowSorter = rowSorter;
/** Converts string into a sentence by capitalizing first letter and appending with . */
const toSentence = (s) => `${(0, upperFirst_js_1.default)(s)}${(0, endsWith_js_1.default)(s, '.') || s === '' ? '' : '.'}`;
exports.toSentence = toSentence;
const getDateTimeCell = (time) => {
    const date = (0, utils_1.getHumanizedDateTime)(time);
    return {
        title: date,
        sortableValue: time ? new Date(time).getTime() : 0,
    };
};
exports.getDateTimeCell = getDateTimeCell;
//# sourceMappingURL=utils.js.map