import React, { useState, useCallback, useMemo } from 'react';
import PropTypes from 'prop-types';
import { useAxiosGet } from '../../../utilities/useAxiosGet';
import { CircularProgress } from '@material-ui/core';
import { AggregateChip } from '../AggregateChip';
import { SeverityTypeEnums } from '../../../enums/unitySystemEnums';
import { useActions } from '../../../hooks/useActions';
import { ConfigService } from '../../../configService';
export const UdpAlert = (props) => {
    const { alertId } = props;
    const [status, setStatus] = useState(null);
    const [loading, setLoading] = useState(false);
    const { triggerAction } = useActions();
    const [{ data: alert, loading: loadingAlert }] = useAxiosGet(ConfigService.tenantV1ApiUrl, `alert/${alertId}`, {}, !!!alertId);
    const threshold = useMemo(() => {
        if (!alert || !status)
            return;
        let threshold = null;
        switch (status) {
            case SeverityTypeEnums.Success:
                threshold = alert[`${SeverityTypeEnums.Success}Threshold`];
                break;
            case SeverityTypeEnums.Error:
                threshold = alert[`${SeverityTypeEnums.Error}Threshold`];
                break;
            case SeverityTypeEnums.Warning:
                threshold = alert[`${SeverityTypeEnums.Warning}Threshold`];
                break;
            case SeverityTypeEnums.Info:
                threshold = alert[`${SeverityTypeEnums.Info}Threshold`];
                break;
            default:
                threshold = alert[`${SeverityTypeEnums.Default}Threshold`];
                break;
        }
        return threshold;
    }, [status, alert]);
    const handleAlertAction = useCallback(() => {
        if (!threshold)
            return;
        if (threshold === null || threshold === void 0 ? void 0 : threshold.actionId) {
            // execute action
            triggerAction(threshold === null || threshold === void 0 ? void 0 : threshold.actionId);
        }
    }, [threshold, triggerAction]);
    if (loading || loadingAlert) {
        return React.createElement(CircularProgress, { size: 18 });
    }
    return (React.createElement("div", null, React.createElement(AggregateChip, { aggregateThresholdId: alert === null || alert === void 0 ? void 0 : alert.aggregateThresholdId, label: threshold === null || threshold === void 0 ? void 0 : threshold.label, setStatus: setStatus, isActionable: (threshold === null || threshold === void 0 ? void 0 : threshold.actionId) || false, onAlertClick: handleAlertAction, isAlert: true, hideAlert: (threshold === null || threshold === void 0 ? void 0 : threshold.hide) || false })));
};
UdpAlert.propTypes = {
    /**
     * Id of AggregateThreshold
     */
    alertId: PropTypes.oneOf([PropTypes.string, undefined, null])
};
//# sourceMappingURL=UdpAlert.js.map