import {memo, ReactNode} from 'react';

import {broker_status} from "../client";
import {Alert, AlertColor} from "@mui/material";

type MyProps = {
    s?: broker_status
}

function severity(input?: broker_status['message_severity']) : AlertColor {
    if (input === undefined)
        return "error";

    switch (input) {
        case 'info':
            return "info";
        case 'warning':
            return "warning";
        case 'error':
            return "error";
        case 'success':
            return "success";
    }
}

function alert(input: AlertColor, message: string) : ReactNode {
    return <Alert severity={input}>{message}</Alert>
}

function ErrorMessage({s}: MyProps) {
    if (s === undefined)
        return alert("error", "Not connected to Jungfraujoch instance; check if jfjoch_broker is running");

    if (s.message === undefined)
        return alert("info", "");

    return alert(severity(s.message_severity), s.message);
}

export default memo(ErrorMessage);
