import { StatusChip } from "../../UI/dataDisplay/status/StatusChip.mjs";
import React, { useMemo } from "react";
import { Box, Chip } from "@material-ui/core";
//#region src/udp/export/VisualSearchObject.jsx
const VisualSearchObject = ({ searchObject }) => {
	const { filterElements = [], filterGroups = [], logicalSearchOperator = 1 } = searchObject || {};
	const filtersByGroup = useMemo(() => {
		return filterElements.reduce((acc, fe) => {
			const gid = fe.groupId ?? 0;
			(acc[gid] = acc[gid] || []).push(fe);
			return acc;
		}, {});
	}, [filterElements]);
	const groupLookup = useMemo(() => {
		const lookup = {};
		filterGroups.forEach((g) => {
			lookup[g.groupId] = {
				...g,
				children: []
			};
		});
		filterGroups.forEach((g_0) => {
			if (g_0.parentGroupId && lookup[g_0.parentGroupId]) lookup[g_0.parentGroupId].children.push(lookup[g_0.groupId]);
		});
		return lookup;
	}, [filterGroups]);
	const rootGroup = {
		groupId: 0,
		logicalSearchOperator,
		children: useMemo(() => {
			return filterGroups.filter((g_1) => !g_1.parentGroupId).map((g_2) => groupLookup[g_2.groupId]);
		}, [filterGroups, groupLookup])
	};
	const OpLabel = {
		1: "AND",
		2: "OR"
	};
	const renderGroup = (group, level = 0) => /* @__PURE__ */ React.createElement(Box, {
		key: group.groupId,
		border: 1,
		borderColor: "grey.300",
		borderRadius: 4,
		p: 1,
		ml: level * 2,
		mb: 1,
		bgcolor: level === 0 ? "grey.100" : "transparent"
	}, /* @__PURE__ */ React.createElement(Box, { mb: 1 }, /* @__PURE__ */ React.createElement(StatusChip, {
		label: OpLabel[group.logicalSearchOperator] || "AND",
		status: "info",
		size: "small"
	})), /* @__PURE__ */ React.createElement(Box, {
		display: "flex",
		flexWrap: "wrap",
		mb: group.children.length ? 1 : 0
	}, (filtersByGroup[group.groupId] || []).map((fe_0, i) => /* @__PURE__ */ React.createElement(Chip, {
		key: `g${group.groupId}-f${i}`,
		size: "small",
		label: `${fe_0.searchField} ${fe_0.searchOperator} ${fe_0.searchValue}`,
		style: {
			marginRight: 4,
			marginBottom: 4
		}
	}))), group.children.map((c) => renderGroup(c, level + 1)));
	if (!(filterElements.length > 0 || filterGroups.length > 0)) return null;
	return /* @__PURE__ */ React.createElement(Box, null, renderGroup(rootGroup));
};
//#endregion
export { VisualSearchObject };

//# sourceMappingURL=VisualSearchObject.mjs.map