"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventListFetch = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const omit_js_1 = tslib_1.__importDefault(require("lodash-es/omit.js"));
const react_core_1 = require("@patternfly/react-core");
const config_1 = require("../../config");
const hooks_1 = require("../../hooks");
const uiState_1 = require("../ui/uiState");
const EventsList_1 = tslib_1.__importDefault(require("../ui/EventsList"));
const ClusterEventsToolbar_1 = tslib_1.__importStar(require("../ui/ClusterEventsToolbar"));
const initialSeverityCounts = {
    critical: 0,
    error: 0,
    info: 0,
    warning: 0,
};
const EventListFetch = (_a) => {
    var { onFetchEvents, disablePagination } = _a, props = tslib_1.__rest(_a, ["onFetchEvents", "disablePagination"]);
    const { cluster, hostId, className, entityKind, setLoading } = props;
    const [lastPolling, setLastPolling] = (0, hooks_1.useStateSafely)(0);
    const [error, setError] = (0, hooks_1.useStateSafely)('');
    const [events, setEvents] = (0, hooks_1.useStateSafely)(undefined);
    const [pageNum, setPageNum] = (0, hooks_1.useStateSafely)(1);
    const [perPage, setPerPage] = (0, hooks_1.useStateSafely)(10);
    const [totalEvents, setTotalEvents] = (0, hooks_1.useStateSafely)(undefined);
    const [filters, setFilters] = (0, hooks_1.useStateSafely)((0, ClusterEventsToolbar_1.getInitialClusterEventsFilters)(entityKind, hostId));
    const [severityCounts, setSeverityCount] = (0, hooks_1.useStateSafely)(initialSeverityCounts);
    const onSetPage = (_event, newPage) => {
        setPageNum(newPage);
    };
    const onPerPageSelect = (_event, newPerPage, newPage) => {
        setPerPage(newPerPage);
        setPageNum(newPage);
    };
    react_1.default.useEffect(() => {
        let timer;
        setLoading(true);
        onFetchEvents(Object.assign(Object.assign({ clusterId: cluster.id }, (0, omit_js_1.default)(filters, 'selectAll')), { offset: perPage * (pageNum - 1), limit: perPage }), ({ data: newEvents, severities, totalEvents }) => {
            setError('');
            setEvents(newEvents);
            setLoading(false);
            setTotalEvents(totalEvents);
            setSeverityCount(severities);
            timer = setTimeout(() => setLastPolling(Date.now()), config_1.REDUCED_POLLING_INTERVAL);
        }, (err) => {
            setError(err);
            setLoading(false);
        });
        return () => clearTimeout(timer);
    }, [
        lastPolling,
        cluster.id,
        hostId,
        onFetchEvents,
        perPage,
        pageNum,
        filters,
        setLoading,
        setError,
        setEvents,
        setLastPolling,
        setTotalEvents,
        setSeverityCount,
    ]);
    const forceRefetch = react_1.default.useCallback(() => {
        setLastPolling(Date.now());
    }, [setLastPolling]);
    let eventList = (react_1.default.createElement(EventsList_1.default, { events: events || [], resetFilters: () => setFilters((0, ClusterEventsToolbar_1.getInitialClusterEventsFilters)(entityKind, hostId)) }));
    if (error) {
        eventList = react_1.default.createElement(uiState_1.ErrorState, { title: error, fetchData: forceRefetch });
    }
    if (!events) {
        eventList = react_1.default.createElement(uiState_1.LoadingState, null);
    }
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(ClusterEventsToolbar_1.default, { filters: filters, setFilters: (filters) => {
                setFilters(filters);
                setPageNum(1);
            }, cluster: cluster, hostId: hostId, entityKind: entityKind, events: events || [], severityCounts: severityCounts }),
        react_1.default.createElement("div", { className: className }, eventList),
        !disablePagination && (react_1.default.createElement(react_core_1.Pagination, { itemCount: totalEvents, widgetId: "events-pagination", perPage: perPage, page: pageNum, variant: react_core_1.PaginationVariant.bottom, onSetPage: onSetPage, onPerPageSelect: onPerPageSelect }))));
};
exports.EventListFetch = EventListFetch;
//# sourceMappingURL=EventListFetch.js.map