import { __exportAll } from "../../../_virtual/_rolldown/runtime.mjs";
//#region src/componentSystems/multiThreadTimeline/functions/mttFunctions.js
var mttFunctions_exports = /* @__PURE__ */ __exportAll({
	formatDataWithTimelineCategoryName: () => formatDataWithTimelineCategoryName,
	formatDataWithTimelineCategoryNameOrderedByDate: () => formatDataWithTimelineCategoryNameOrderedByDate,
	plotCategoryBar: () => plotCategoryBar,
	plotX: () => plotX,
	plotY: () => plotY
});
const plotY = (item, data, nodeWidth) => {
	const plotDate = [item];
	let dates = [];
	data && data.map((item) => {
		dates.push(item.date);
	});
	let unique = [...new Set(dates)];
	const mappedToObjects = unique.map(function(value) {
		return { date: value };
	});
	const plots = [];
	for (let i = 1; i <= unique.length; i++) plots.push(i);
	return mappedToObjects.map((o, i) => ({
		...o,
		plots: plots[i] || null
	})).filter(({ date }) => plotDate.includes(date)).map(({ plots }) => plots).map((item) => {
		return item;
	}) * nodeWidth - nodeWidth;
};
const plotX = (timeline, subThread, data, threadHeight) => {
	const xLine = data && data.map((item) => {
		return item.timeline.toString() + "." + item.subThread.toString();
	});
	const rowIndex = [...new Set(xLine)].reduce(function(s, a) {
		s.push({ thread: a });
		return s;
	}, []);
	const concatIndex = timeline + "." + subThread;
	const results = rowIndex.map((v, index) => ({
		...v,
		rowIndex: index
	})).filter(function(entry) {
		return entry.thread === concatIndex;
	});
	return results[0] && results[0].rowIndex * threadHeight;
};
function formatDate(date) {
	return new Date(date).toLocaleDateString("en-US");
}
function getChildObject(childObj) {
	const date = formatDate(childObj.date);
	return {
		date,
		details: {
			id: childObj.id,
			eventTypeID: childObj.eventTypeID,
			primaryContextID: childObj.primaryContextID,
			secondaryContextID: childObj.secondaryContextID,
			date,
			transactionType: childObj.transactionType,
			description: childObj.description,
			shortDescription: childObj.shortDescription,
			data2: childObj.data2,
			data1: childObj.data1,
			data3: childObj.data3,
			serializeFields: childObj.serializeFields,
			mttEventsNewEventType: childObj.mttEventsNewEventType,
			threadId: childObj.threadId,
			timelineCategory: childObj.timelineCategory,
			color: childObj.color,
			icon: childObj.icon
		}
	};
}
function getEventsObjects(object, timeline) {
	return object.map((obj) => {
		const date = formatDate(obj.date);
		return {
			date,
			details: {
				id: obj.id,
				eventTypeID: obj.eventTypeID,
				primaryContextID: obj.primaryContextID,
				secondaryContextID: obj.secondaryContextID,
				date,
				transactionType: obj.transactionType,
				description: obj.description,
				shortDescription: obj.shortDescription,
				data2: obj.data2,
				data1: obj.data1,
				data3: obj.data3,
				serializeFields: obj.serializeFields,
				mttEventsNewEventType: obj.mttEventsNewEventType,
				threadId: obj.threadId,
				timelineCategory: obj.timelineCategory,
				timeline,
				color: obj.color,
				icon: obj.icon
			}
		};
	});
}
const formatDataWithTimelineCategoryName = (object) => {
	let tempObject = [];
	object.forEach((obj) => {
		obj.events.forEach((event) => {
			const foundThreadEvent = (obj.mttThreadMttThreadEvents || obj.udpMttThreadUdpMttThreadEvents || []).find((threadEvent) => event.transactionType?.toString() === threadEvent.transactionType?.toString());
			if (foundThreadEvent) {
				event.color = foundThreadEvent.color === null || foundThreadEvent.color === void 0 || foundThreadEvent.color === "" || foundThreadEvent.color === " " ? obj.color : foundThreadEvent.color;
				event.icon = foundThreadEvent.icon;
			} else {
				event.color = obj.color;
				event.icon = obj.iconID;
			}
		});
	});
	object.forEach((obj) => {
		let existsTimelineFlag = false;
		const mttThreadMttConfig = obj.mttThreadMttConfig || obj.udpMttThreadUdpMttConfig;
		tempObject.forEach((tmpObj) => {
			if (mttThreadMttConfig?.sequence === tmpObj.timeline) {
				tmpObj.dates.push(...getEventsObjects(obj.events, mttThreadMttConfig?.sequence));
				existsTimelineFlag = true;
			}
		});
		if (!existsTimelineFlag) tempObject.push({
			timeline: mttThreadMttConfig?.sequence,
			timelineCategoryName: mttThreadMttConfig?.name,
			color: obj.color,
			iconID: obj.iconID,
			dates: getEventsObjects(obj.events, mttThreadMttConfig?.sequence)
		});
	});
	let cleanObjects = [];
	tempObject.forEach((obj) => {
		let subThreadCount = 1;
		obj.dates.forEach((dateObj) => {
			let existsSubThreadFlag = false;
			const foundIndex = cleanObjects.findIndex((searchObj) => {
				return searchObj.timeline === dateObj.details.timeline && searchObj.dates[0].details.secondaryContextID === dateObj.details.secondaryContextID && formatDate(searchObj.date) === formatDate(dateObj.details.date);
			});
			if (foundIndex > -1) {
				cleanObjects[foundIndex].dates.push(getChildObject(dateObj.details));
				existsSubThreadFlag = true;
			} else {
				const foundIndex2 = cleanObjects.findIndex((searchObj) => searchObj.timeline === dateObj.details.timeline && searchObj.dates[0].details.secondaryContextID === dateObj.details.secondaryContextID && formatDate(searchObj.date) !== formatDate(dateObj.details.date));
				if (foundIndex2 > -1) {
					cleanObjects.push({
						timeline: cleanObjects[foundIndex2].timeline,
						subThread: cleanObjects[foundIndex2].subThread,
						timelineCategoryName: cleanObjects[foundIndex2].timelineCategoryName,
						color: dateObj?.details?.color ? dateObj.details.color : cleanObjects[foundIndex2].color,
						iconID: dateObj?.details?.icon ? dateObj.details.icon : cleanObjects[foundIndex2].iconID,
						date: formatDate(dateObj.details.date),
						dates: [getChildObject(dateObj.details)]
					});
					existsSubThreadFlag = true;
				}
			}
			if (!existsSubThreadFlag) cleanObjects.push({
				timeline: obj.timeline,
				subThread: subThreadCount++,
				timelineCategoryName: obj.timelineCategoryName,
				color: dateObj?.details?.color ? dateObj.details.color : obj.color,
				iconID: dateObj?.details?.icon ? dateObj.details.icon : obj.iconID,
				date: formatDate(dateObj.details.date),
				dates: [getChildObject(dateObj.details)]
			});
		});
	});
	return cleanObjects;
};
const plotCategoryBar = (timelineNumber, subThread, data, timeline, threadHeight) => {
	const xLine = data && formatDataWithTimelineCategoryName(timeline).map((item) => {
		return item.timeline.toString() + "." + item.subThread.toString();
	});
	const rowIndex = [...new Set(xLine)].reduce(function(s, a) {
		s.push({ thread: a });
		return s;
	}, []);
	const concatIndex = timelineNumber + "." + subThread;
	const results = rowIndex.map((v, index) => ({
		...v,
		rowIndex: index + 1
	})).filter(function(entry) {
		return entry.thread === concatIndex;
	});
	return (results[0] && results[0].rowIndex * threadHeight) - threadHeight;
};
function orderByDate(a, b) {
	if (new Date(a.date) < new Date(b.date)) return -1;
	if (new Date(a.date) > new Date(b.date)) return 1;
	return 0;
}
function orderByDateTimeline(a, b) {
	if (!a?.length || !b?.length) return 0;
	if (new Date(a[0].date) < new Date(b[0].date)) return -1;
	if (new Date(a[0].date) > new Date(b[0].date)) return 1;
	return 0;
}
function orderByDateSubThread(a, b) {
	if (!a?.length || !b?.length) return 0;
	if (new Date(a[0].date) < new Date(b[0].date)) return -1;
	if (new Date(a[0].date) > new Date(b[0].date)) return 1;
	return 0;
}
const formatDataWithTimelineCategoryNameOrderedByDate = (inputData) => {
	let data = formatDataWithTimelineCategoryName(inputData).sort(orderByDate);
	let maxElement = 1;
	data.forEach((obj) => {
		if (maxElement < obj.timeline) maxElement = obj.timeline;
	});
	let objArr = [...Array(maxElement)].map((e) => e = []);
	data.forEach((obj) => {
		objArr.forEach((val, index) => {
			if (obj.timeline === index + 1) objArr[index].push(obj);
		});
	});
	objArr.sort(orderByDateTimeline);
	let objArr2 = [...Array(maxElement)].map((e) => e = []);
	let maxElements;
	objArr.forEach((objs, i) => {
		maxElements = 1;
		objs.forEach((obj) => {
			if (maxElements < obj.subThread) maxElements = obj.subThread;
		});
		objArr2[i] = maxElements;
	});
	let objArr3 = [...Array(maxElement)].map((e) => e = []);
	objArr2.forEach((obj, index) => objArr3[index] = [...Array(obj)].map((e) => e = []));
	objArr.forEach((ob) => {
		ob.forEach((o) => {
			objArr3.forEach((objs, j) => {
				objs.forEach((obj, k) => {
					if (o.timeline === j + 1 && o.subThread === k + 1) obj.push(o);
				});
			});
		});
	});
	objArr3.forEach((objs) => {
		objs.sort(orderByDateSubThread).forEach((obj, index) => obj.forEach((obj) => obj.subThread = index + 1));
	});
	return objArr3.flat(2);
};
//#endregion
export { formatDataWithTimelineCategoryName, formatDataWithTimelineCategoryNameOrderedByDate, mttFunctions_exports };

//# sourceMappingURL=mttFunctions.mjs.map