import { ConfigService } from "../../../configService/index.mjs";
import { apiMutate } from "../../../utilities/useAxiosMutate.mjs";
import { formatDataWithTimelineCategoryNameOrderedByDate } from "../functions/mttFunctions.mjs";
import { MttTimeline } from "./MttTimeline.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
//#region src/componentSystems/multiThreadTimeline/mttMainTimeline/MttMainTimeline.jsx
const MttMainTimeline = (props) => {
	const $ = c(12);
	const { timelineApiMethodInstanceId, params: t0 } = props;
	let t1;
	if ($[0] !== t0) {
		t1 = t0 === void 0 ? {} : t0;
		$[0] = t0;
		$[1] = t1;
	} else t1 = $[1];
	const params = t1;
	const [timelineThreads, setTimelineThreads] = useState(null);
	const [data, setData] = useState();
	const [loading, setLoading] = useState(false);
	const executeQueryInstance = _temp;
	let t2;
	let t3;
	if ($[2] !== timelineApiMethodInstanceId) {
		t2 = () => {
			const fetchTimelineThreads = async () => {
				const timelineResponse = await executeQueryInstance(timelineApiMethodInstanceId).catch(_temp2);
				if (timelineResponse?.status === 200) setTimelineThreads(timelineResponse?.data?.pageList || []);
			};
			fetchTimelineThreads();
		};
		t3 = [executeQueryInstance, timelineApiMethodInstanceId];
		$[2] = timelineApiMethodInstanceId;
		$[3] = t2;
		$[4] = t3;
	} else {
		t2 = $[3];
		t3 = $[4];
	}
	useEffect(t2, t3);
	let t4;
	let t5;
	if ($[5] !== params || $[6] !== timelineThreads) {
		t4 = () => {
			const fetchThreadEvents = async () => {
				if (!timelineThreads) return null;
				setLoading(true);
				const promises = [];
				const threadList = [];
				timelineThreads.forEach((thread) => {
					promises.push(executeQueryInstance(thread.queryID, { ...params }));
				});
				await Promise.all(promises.map((p) => p.catch((e_0) => {
					setLoading(false);
					return e_0;
				}))).then((responses) => {
					responses.forEach((response, index) => {
						if (response.status === 200) {
							const timelineThread = timelineThreads[index];
							if (response?.data?.pageList) {
								response.data.pageList.forEach((evt) => evt.threadId = timelineThread.name);
								const thread_0 = {
									...timelineThread,
									events: response.data.pageList
								};
								threadList.push(thread_0);
							}
						}
					});
					setLoading(false);
				});
				setData(formatDataWithTimelineCategoryNameOrderedByDate(threadList));
			};
			fetchThreadEvents();
		};
		t5 = [
			executeQueryInstance,
			timelineThreads,
			params
		];
		$[5] = params;
		$[6] = timelineThreads;
		$[7] = t4;
		$[8] = t5;
	} else {
		t4 = $[7];
		t5 = $[8];
	}
	useEffect(t4, t5);
	let t6;
	if ($[9] !== data || $[10] !== loading) {
		t6 = /* @__PURE__ */ React.createElement(MttTimeline, {
			data,
			loading
		});
		$[9] = data;
		$[10] = loading;
		$[11] = t6;
	} else t6 = $[11];
	return t6;
};
async function _temp(apiMethodInstanceId, t0) {
	const data_0 = t0 === void 0 ? {} : t0;
	return apiMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryInstance/${apiMethodInstanceId}`, { method: "post" }, { data: data_0 });
}
function _temp2(e) {
	return e;
}
//#endregion
export { MttMainTimeline };

//# sourceMappingURL=MttMainTimeline.mjs.map