import React, { useCallback, useEffect, useState } from 'react';
import { apiMutate } from '../../../utilities/useAxiosMutate';
import { formatDataWithTimelineCategoryNameOrderedByDate } from '../functions/mttFunctions';
import { MttTimeline } from './MttTimeline';
import { ConfigService } from '../../../configService';
export const MttMainTimeline = (props) => {
    const { timelineApiMethodInstanceId, params = {} } = props;
    const [timelineThreads, setTimelineThreads] = useState(null);
    const [data, setData] = useState();
    const [loading, setLoading] = useState(false);
    const executeQueryInstance = useCallback(async (apiMethodInstanceId, data = {}) => apiMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryInstance/${apiMethodInstanceId}`, {
        method: 'post'
    }, { data: data }), []);
    useEffect(() => {
        const fetchTimelineThreads = async () => {
            var _a;
            const timelineResponse = await executeQueryInstance(timelineApiMethodInstanceId).catch((e) => e);
            if ((timelineResponse === null || timelineResponse === void 0 ? void 0 : timelineResponse.status) === 200) {
                setTimelineThreads(((_a = timelineResponse === null || timelineResponse === void 0 ? void 0 : timelineResponse.data) === null || _a === void 0 ? void 0 : _a.pageList) || []);
            }
        };
        fetchTimelineThreads();
    }, [executeQueryInstance, timelineApiMethodInstanceId]);
    useEffect(() => {
        const fetchThreadEvents = async () => {
            if (!timelineThreads) {
                return null;
            }
            setLoading(true);
            const promises = [];
            const threadList = [];
            timelineThreads.forEach((thread) => {
                promises.push(executeQueryInstance(thread.queryID, Object.assign({}, params)));
            });
            await Promise.all(promises.map((p) => p.catch((e) => {
                setLoading(false);
                return e;
            }))).then((responses) => {
                responses.forEach((response, index) => {
                    var _a;
                    if (response.status === 200) {
                        const timelineThread = timelineThreads[index];
                        if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.pageList) {
                            response.data.pageList.forEach((evt) => (evt.threadId = timelineThread.name));
                            const thread = Object.assign(Object.assign({}, timelineThread), { events: response.data.pageList });
                            threadList.push(thread);
                        }
                    }
                });
                setLoading(false);
            });
            setData(formatDataWithTimelineCategoryNameOrderedByDate(threadList));
        };
        fetchThreadEvents();
    }, [executeQueryInstance, timelineThreads, params]);
    return React.createElement(MttTimeline, { data: data, loading: loading });
};
//# sourceMappingURL=MttMainTimeline.js.map