import React from "react";
import ExpandableText from "../ExpandableText/ExpandableText";
import Table from "../Table/Table";
import FieldType from "../Types/FieldType";
import SortOrder from "../../../Types/BaseDatabase/SortOrder";
// Default condition types where "True" is bad
const defaultNegativeTypes = [
    "MemoryPressure",
    "DiskPressure",
    "PIDPressure",
    "NetworkUnavailable",
];
function getStatusStyle(condition, negativeTypes) {
    const isNegativeType = negativeTypes.includes(condition.type);
    if (condition.status === "True") {
        return isNegativeType
            ? "bg-gradient-to-r from-red-50 to-red-100 text-red-800 ring-1 ring-inset ring-red-200/80"
            : "bg-gradient-to-r from-emerald-50 to-emerald-100 text-emerald-800 ring-1 ring-inset ring-emerald-200/80";
    }
    if (condition.status === "False") {
        return isNegativeType
            ? "bg-gradient-to-r from-emerald-50 to-emerald-100 text-emerald-800 ring-1 ring-inset ring-emerald-200/80"
            : "bg-gradient-to-r from-red-50 to-red-100 text-red-800 ring-1 ring-inset ring-red-200/80";
    }
    return "bg-gradient-to-r from-amber-50 to-amber-100 text-amber-800 ring-1 ring-inset ring-amber-200/80";
}
function formatRelativeTime(timestamp) {
    if (!timestamp) {
        return "-";
    }
    const date = new Date(timestamp);
    const now = new Date();
    const diffMs = now.getTime() - date.getTime();
    if (diffMs < 0) {
        return timestamp;
    }
    const diffSec = Math.floor(diffMs / 1000);
    if (diffSec < 60) {
        return `${diffSec}s ago`;
    }
    const diffMin = Math.floor(diffSec / 60);
    if (diffMin < 60) {
        return `${diffMin}m ago`;
    }
    const diffHrs = Math.floor(diffMin / 60);
    if (diffHrs < 24) {
        return `${diffHrs}h ago`;
    }
    const diffDays = Math.floor(diffHrs / 24);
    return `${diffDays}d ago`;
}
const ConditionsTable = (props) => {
    const negativeTypes = props.negativeTypes || defaultNegativeTypes;
    return (React.createElement("div", { className: props.className || "" },
        React.createElement(Table, { id: "conditions-table", data: props.conditions, singularLabel: "Condition", pluralLabel: "Conditions", isLoading: false, error: "", currentPageNumber: 1, totalItemsCount: props.conditions.length, itemsOnPage: props.conditions.length, disablePagination: true, noItemsMessage: "No conditions available.", onNavigateToPage: () => { }, sortBy: null, sortOrder: SortOrder.Ascending, onSortChanged: () => { }, columns: [
                {
                    title: "Type",
                    type: FieldType.Element,
                    key: "type",
                    disableSort: true,
                    getElement: (condition) => {
                        return (React.createElement("span", { className: "font-medium text-gray-900" }, condition.type));
                    },
                },
                {
                    title: "Status",
                    type: FieldType.Element,
                    key: "status",
                    disableSort: true,
                    getElement: (condition) => {
                        return (React.createElement("span", { className: `inline-flex px-2 py-0.5 text-xs font-semibold rounded-full ${getStatusStyle(condition, negativeTypes)}` }, condition.status));
                    },
                },
                {
                    title: "Reason",
                    type: FieldType.Element,
                    key: "reason",
                    disableSort: true,
                    getElement: (condition) => {
                        return (React.createElement("span", { className: "text-gray-600" }, condition.reason || "-"));
                    },
                },
                {
                    title: "Message",
                    type: FieldType.Element,
                    key: "message",
                    disableSort: true,
                    getElement: (condition) => {
                        return React.createElement(ExpandableText, { text: condition.message || "-" });
                    },
                },
                {
                    title: "Last Transition",
                    type: FieldType.Element,
                    key: "lastTransitionTime",
                    disableSort: true,
                    getElement: (condition) => {
                        return (React.createElement("span", { className: "text-gray-500", title: condition.lastTransitionTime || "" }, formatRelativeTime(condition.lastTransitionTime || "")));
                    },
                },
            ] })));
};
export default ConditionsTable;
//# sourceMappingURL=ConditionsTable.js.map