import { getStatus as fetchOutlookStatus } from './outlookIntegrationClient';
const DEFAULT_STATUS = {
    connected: false,
    mailboxPrincipal: null,
    expiresAt: null,
    scope: null,
    loaded: false,
    error: null,
};
let cachedStatus = Object.assign({}, DEFAULT_STATUS);
const listeners = new Set();
let inFlight = null;
let hasFetchedOnce = false;
let statusVersion = 0;
export function getOutlookStatus() {
    return cachedStatus;
}
export function isOutlookConnected() {
    return !!cachedStatus.connected;
}
export function setOutlookDisconnected() {
    cachedStatus = Object.assign(Object.assign({}, cachedStatus), { connected: false, mailboxPrincipal: null, expiresAt: null, scope: null, loaded: true, error: null });
    publish();
}
export function resetOutlookStatus() {
    statusVersion += 1;
    hasFetchedOnce = false;
    inFlight = null;
    cachedStatus = Object.assign({}, DEFAULT_STATUS);
    publish();
}
export function subscribeOutlookStatus(handler) {
    listeners.add(handler);
    try {
        handler(cachedStatus);
    }
    catch (err) {
        console.warn('OutlookStatus: failed to publish initial status', err);
    }
    return () => {
        listeners.delete(handler);
    };
}
export async function refreshOutlookStatus() {
    if (inFlight !== null) {
        return inFlight;
    }
    const requestVersion = statusVersion;
    const request = fetchOutlookStatus()
        .then(status => {
        if (requestVersion !== statusVersion) {
            return cachedStatus;
        }
        cachedStatus = mapStatus(status);
        hasFetchedOnce = true;
        publish();
        return cachedStatus;
    })
        .catch(err => {
        if (requestVersion !== statusVersion) {
            return cachedStatus;
        }
        const message = err instanceof Error
            ? err.message
            : 'Unable to load Outlook integration status.';
        cachedStatus = Object.assign(Object.assign({}, cachedStatus), { loaded: true, error: message });
        publish();
        throw err;
    })
        .finally(() => {
        if (inFlight === request) {
            inFlight = null;
        }
    });
    inFlight = request;
    return request;
}
export function ensureOutlookStatusLoaded() {
    return hasFetchedOnce ? Promise.resolve(cachedStatus) : refreshOutlookStatus();
}
function mapStatus(status) {
    var _a, _b, _c;
    return {
        connected: !!(status === null || status === void 0 ? void 0 : status.connected),
        mailboxPrincipal: (_a = status === null || status === void 0 ? void 0 : status.mailboxPrincipal) !== null && _a !== void 0 ? _a : null,
        expiresAt: (_b = status === null || status === void 0 ? void 0 : status.expiresAt) !== null && _b !== void 0 ? _b : null,
        scope: (_c = status === null || status === void 0 ? void 0 : status.scope) !== null && _c !== void 0 ? _c : null,
        loaded: true,
        error: null,
    };
}
function publish() {
    listeners.forEach(handler => {
        try {
            handler(cachedStatus);
        }
        catch (err) {
            console.warn('OutlookStatus: subscriber threw an error', err);
        }
    });
}
//# sourceMappingURL=outlookIntegrationStatus.js.map