import React, { useEffect, useMemo, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { AmbientAlert } from '../../UI/feedback/AmbientAlert';
import { SubHeaderAction } from '../../utilities/utilityBar/SubHeaderAction';
import { getOutlookStatus, subscribeOutlookStatus, ensureOutlookStatusLoaded, } from './outlookIntegrationStatus';
/**
 * Global warning banner shown when Outlook integration is disconnected.
 * Defaults to enabled so apps get the disconnected warning behavior out of the box.
 */
export const OutlookIntegrationBanner = ({ onConfigure, enabled = true, hideOnPaths = [], title, message, actionLabel, dismissible = true, renderInSubHeaderAction = false, }) => {
    const { t } = useTranslation();
    const location = useLocation();
    const [status, setStatus] = useState(() => getOutlookStatus());
    const [show, setShow] = useState(true);
    const shouldHideForPath = useMemo(() => {
        const pathname = (location === null || location === void 0 ? void 0 : location.pathname) || '';
        return hideOnPaths.some(path => path && pathname.includes(path));
    }, [hideOnPaths, location === null || location === void 0 ? void 0 : location.pathname]);
    useEffect(() => {
        const unsub = subscribeOutlookStatus(next => {
            setStatus(next);
            // Restore banner when status updates and integration is disconnected.
            if (next.connected) {
                setShow(false);
            }
            else {
                setShow(true);
            }
        });
        ensureOutlookStatusLoaded().catch(() => { });
        return () => unsub();
    }, []);
    if (!enabled || shouldHideForPath || !show || !status.loaded || status.connected) {
        return null;
    }
    const alert = (React.createElement(AmbientAlert, { alertTitle: title || t('outlook.banner.title', 'Outlook Integration Required'), alertMessage: status.error ||
            message ||
            t('outlook.banner.message', 'Setup Outlook integration to enable email functionality.'), showAlert: true, alertSeverity: status.error ? 'error' : 'warning', alertAction: typeof onConfigure === 'function', alertActionButton: actionLabel ||
            t('outlook.banner.action', 'Setup Outlook integration'), alertHandleAction: onConfigure, closeAlert: dismissible ? () => setShow(false) : undefined }));
    if (renderInSubHeaderAction) {
        return React.createElement(SubHeaderAction, null, alert);
    }
    return alert;
};
export default OutlookIntegrationBanner;
//# sourceMappingURL=OutlookIntegrationBanner.js.map