import API from "../../Utils/API/API";
import ModelAPI from "../../Utils/ModelAPI/ModelAPI";
import HeaderAlert from "./HeaderAlert";
import React, { useEffect, useState } from "react";
const HeaderModelAlert = (props) => {
    const [isLoading, setIsLoading] = useState(true);
    const [error, setError] = useState("");
    const [count, setCount] = useState(0);
    useEffect(() => {
        fetchCount().catch((err) => {
            setError(API.getFriendlyMessage(err));
        });
    }, [props.refreshToggle]);
    const fetchCount = async () => {
        setError("");
        setIsLoading(true);
        if (props.onCountFetchInit) {
            props.onCountFetchInit();
        }
        try {
            const count = await ModelAPI.count({
                modelType: props.modelType,
                query: props.query,
                requestOptions: props.requestOptions,
            });
            setCount(count);
        }
        catch (err) {
            setError(API.getFriendlyMessage(err));
        }
        setIsLoading(false);
    };
    useEffect(() => {
        setIsLoading(true);
        fetchCount().catch((err) => {
            setError(API.getFriendlyMessage(err));
        });
        setIsLoading(false);
    }, []);
    if (error) {
        return React.createElement(React.Fragment, null);
    }
    if (isLoading) {
        return React.createElement(React.Fragment, null);
    }
    if (count === 0) {
        return React.createElement(React.Fragment, null);
    }
    return (React.createElement(HeaderAlert, { title: `${count}`, suffix: `${count > 1 ? props.pluralName : props.singularName}`, icon: props.icon, onClick: props.onClick, className: props.className, alertType: props.alertType, tooltip: props.tooltip }));
};
export default HeaderModelAlert;
//# sourceMappingURL=HeaderModelAlert.js.map