"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onFetchEvents = void 0;
const tslib_1 = require("tslib");
const common_1 = require("../../../common");
const api_1 = require("../../../common/api");
const apis_1 = require("../../services/apis");
const parseHeaders = (headers) => {
    const severities = {
        error: 0,
        info: 0,
        warning: 0,
        critical: 0,
    };
    common_1.EVENT_SEVERITIES.forEach((severity) => {
        const count = Number(headers[`severity-count-${severity}`]);
        severities[severity] = count;
    });
    return {
        totalEvents: Number(headers['event-count']),
        severities: severities,
    };
};
const fetchEventsAsync = (...args) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    const [props, onSuccess, onError] = args;
    try {
        apis_1.EventsAPI.abort();
        const response = yield apis_1.EventsAPI.list(props);
        onSuccess(Object.assign({ data: response.data }, parseHeaders(response.headers)));
    }
    catch (error) {
        const { code } = error;
        if (!code || code !== 'ERR_CANCELED') {
            (0, api_1.handleApiError)(error, () => {
                onError('Failed to load events');
            });
        }
    }
});
const onFetchEvents = (props, onSuccess, onError) => {
    void fetchEventsAsync(props, onSuccess, onError);
};
exports.onFetchEvents = onFetchEvents;
//# sourceMappingURL=fetchEvents.js.map