import { LogicalSearchOperatorEnums } from "../../enums/unitySystemEnums.mjs";
import { getSanitizedName } from "../../utilities/entities/entityUtilities.mjs";
import { getSearchIdentifiers, getSearchObject } from "../../utilities/search/SearchUtilities.mjs";
import { AmbientCard } from "../../UI/surfaces/cards/AmbientCard/AmbientCard.mjs";
import { useSearchFilterParamsMap } from "../../hooks/useSearchFilterParamsMap.mjs";
import { InquirySearchTable } from "./InquirySearchTable.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { LinearProgress, makeStyles } from "@material-ui/core";
import clsx from "clsx";
//#region src/inquiry/search/InquirySearchContainer.jsx
const useStyles = makeStyles((theme) => ({
	root: { overflowX: "auto" },
	searchCard: { paddingBottom: theme.spacing(2) },
	emptyStateWrapper: {
		display: "flex",
		justifyContent: "center",
		paddingTop: "25vh"
	}
}));
const InquirySearchContainer = (props) => {
	const $ = c(31);
	const { advancedSearchItems, catalogObjects, classes: classProps, executeQueryAdHoc, isAdvancedSearch, loadSingleNode, onRowDoubleClick, searchItems, setSelectedSearchRows, apiMethodInstanceId } = props;
	let t0;
	if ($[0] !== classProps) {
		t0 = classProps || {};
		$[0] = classProps;
		$[1] = t0;
	} else t0 = $[1];
	const { root } = t0;
	const classes = useStyles();
	const [searchList, setSearchList] = useState(null);
	const [loading, setLoading] = useState(false);
	const { getQueryFilterElements, getQueryFilterGroups, getQuerySearchOperator } = useSearchFilterParamsMap();
	const getQueryColumnsData = _temp;
	let t1;
	let t2;
	if ($[2] !== advancedSearchItems || $[3] !== apiMethodInstanceId || $[4] !== catalogObjects || $[5] !== executeQueryAdHoc || $[6] !== getQueryFilterElements || $[7] !== getQueryFilterGroups || $[8] !== getQuerySearchOperator || $[9] !== isAdvancedSearch || $[10] !== loadSingleNode || $[11] !== searchItems) {
		t1 = () => {
			if (!catalogObjects) setLoading(true);
			if (!searchItems || !catalogObjects?.length) return;
			setLoading(true);
			const promises = [];
			const list = [];
			const promiseIndexMap = {};
			(!isAdvancedSearch ? searchItems : advancedSearchItems || []).forEach((item, index) => {
				let filterElementsFromQuery = getQueryFilterElements(item.apiMethodInstance);
				let filterGroups = getQueryFilterGroups(item.apiMethodInstance);
				const logicalOperatorFromQuery = getQuerySearchOperator(item.apiMethodInstance);
				item.queryColumnsData = getQueryColumnsData(item.apiMethodInstance);
				let outerQueryGroupId = 0;
				let queryOuterGroup = null;
				if (!!filterElementsFromQuery.length) {
					outerQueryGroupId = filterGroups.length + 1;
					queryOuterGroup = {
						groupId: outerQueryGroupId,
						logicalSearchOperator: logicalOperatorFromQuery
					};
				}
				let outerTreeBuilderGroupId = 0;
				let inquiryOuterGroup = null;
				if (!!item?.searchFilterElements?.length) {
					outerTreeBuilderGroupId = outerQueryGroupId + 1;
					inquiryOuterGroup = {
						groupId: outerTreeBuilderGroupId,
						logicalSearchOperator: isAdvancedSearch ? LogicalSearchOperatorEnums.And : LogicalSearchOperatorEnums.Or
					};
				}
				filterGroups = filterGroups.map((group) => {
					if (!group.parentGroupId) group.parentGroupId = outerQueryGroupId;
					return group;
				});
				filterElementsFromQuery = filterElementsFromQuery.map((element) => {
					if (!element.groupId) element.groupId = outerQueryGroupId;
					return element;
				});
				item.searchFilterElements = item.searchFilterElements.map((element_0) => {
					element_0.groupId = outerTreeBuilderGroupId;
					return element_0;
				});
				const searchFilterElements = filterElementsFromQuery.concat(item.searchFilterElements || []);
				item.searchFilterElements = searchFilterElements;
				if (!!inquiryOuterGroup) filterGroups.push(inquiryOuterGroup);
				if (!!queryOuterGroup) filterGroups.push(queryOuterGroup);
				item.searchFilterGroups = filterGroups;
				const searchObject = getSearchObject(searchFilterElements, 1, null, false, LogicalSearchOperatorEnums.And, filterGroups);
				let shouldSearch = true;
				if (!searchFilterElements.some(_temp2)) {
					const equalsSearchFilterElements = searchFilterElements.filter(_temp3);
					if (!!equalsSearchFilterElements.length) {
						const coProperties = catalogObjects.find((co) => co.catalogObjectName === item.catalogReturnObject)?.properties || [];
						if (!!coProperties.length) equalsSearchFilterElements.forEach((esfe) => {
							if (!shouldSearch) return;
							const matchingProperty = coProperties.find((cop) => cop.name?.toLowerCase() === esfe.searchField?.toLowerCase());
							if (matchingProperty?.type) {
								let compareNumber = true;
								let parsedValue = esfe.searchValue;
								bb119: switch (matchingProperty.type) {
									case "int":
										if (/[^0-9]/.test(esfe.searchValue)) parsedValue = NaN;
										else parsedValue = parseInt(esfe.searchValue);
										break bb119;
									case "double":
									case "decimal":
									case "float":
										if (/[^0-9.]/.test(esfe.searchValue)) parsedValue = NaN;
										else parsedValue = parseFloat(esfe.searchValue);
										break bb119;
									default: compareNumber = false;
								}
								if (compareNumber && isNaN(parsedValue)) shouldSearch = false;
							}
						});
					}
				}
				if (shouldSearch && executeQueryAdHoc) {
					const data = { data: searchObject };
					promiseIndexMap[promises.length] = index;
					promises.push(executeQueryAdHoc(data, item.apiMethodId));
				}
				list.push({
					queryData: null,
					searchItem: item
				});
			});
			Promise.all(promises.map(_temp5)).then((responses) => {
				responses.forEach((response, i) => {
					const index_0 = promiseIndexMap[i];
					if (response.status === 200) {
						const querySearchObj = list[index_0];
						querySearchObj.queryData = response.data;
					} else if (!response.status) {
						const querySearchObj_0 = list[index_0];
						console.error(`Retrieval Error Occured for ${querySearchObj_0?.searchItem?.catalogReturnObject}`);
					}
				});
				let matchingItem = list.find((item_0) => item_0?.searchItem?.apiMethodInstance?.apiMethodInstanceId === apiMethodInstanceId && item_0?.queryData?.pageList?.length === 1);
				if (!matchingItem) matchingItem = list.find(_temp6);
				if (matchingItem) {
					const querySearchObj_1 = matchingItem;
					const node = {};
					const searchItem = querySearchObj_1?.searchItem;
					const identifiers = getSearchIdentifiers(catalogObjects, searchItem.catalogReturnObject) || [];
					node[searchItem.catalogReturnObject] = {
						rows: matchingItem?.queryData?.pageList,
						apiMethodId: searchItem.apiMethodId,
						searchIdentifier: identifiers.toString(),
						treeStructure: searchItem.treeStructure
					};
					loadSingleNode(node);
				}
				setSearchList(list);
				setLoading(false);
			});
		};
		t2 = [
			searchItems,
			isAdvancedSearch,
			advancedSearchItems,
			loadSingleNode,
			catalogObjects,
			executeQueryAdHoc,
			getQueryFilterElements,
			getQueryColumnsData,
			getQueryFilterGroups,
			getQuerySearchOperator,
			apiMethodInstanceId
		];
		$[2] = advancedSearchItems;
		$[3] = apiMethodInstanceId;
		$[4] = catalogObjects;
		$[5] = executeQueryAdHoc;
		$[6] = getQueryFilterElements;
		$[7] = getQueryFilterGroups;
		$[8] = getQuerySearchOperator;
		$[9] = isAdvancedSearch;
		$[10] = loadSingleNode;
		$[11] = searchItems;
		$[12] = t1;
		$[13] = t2;
	} else {
		t1 = $[12];
		t2 = $[13];
	}
	useEffect(t1, t2);
	let t3;
	if ($[14] !== classes.root || $[15] !== root) {
		t3 = clsx(classes.root, root);
		$[14] = classes.root;
		$[15] = root;
		$[16] = t3;
	} else t3 = $[16];
	let t4;
	if ($[17] !== loading) {
		t4 = loading && /* @__PURE__ */ React.createElement(LinearProgress, null);
		$[17] = loading;
		$[18] = t4;
	} else t4 = $[18];
	let t5;
	if ($[19] !== catalogObjects || $[20] !== classes.searchCard || $[21] !== executeQueryAdHoc || $[22] !== isAdvancedSearch || $[23] !== onRowDoubleClick || $[24] !== searchList || $[25] !== setSelectedSearchRows) {
		t5 = searchList?.map((searchObj) => /* @__PURE__ */ React.createElement("div", {
			key: searchObj.searchItem.catalogReturnObject,
			className: classes.searchCard
		}, /* @__PURE__ */ React.createElement(AmbientCard, {
			key: `${searchObj.searchItem.catalogReturnObject}-${searchObj?.queryData?.total}`,
			title: `${getSanitizedName(searchObj.searchItem.catalogReturnObject)} - ${searchObj.error ? searchObj.error : searchObj?.queryData?.total || "0"}`,
			moreMessage: "Top 10 results...",
			moreButton: "Load More",
			moreButtonExpanded: "Minimize",
			more: !!searchObj?.queryData?.pageList?.length,
			header: true,
			fullWidth: true,
			globalToggle: true,
			collapseChildrenWhenExpanded: true,
			toggleGlobalExpanded: !!searchObj?.queryData?.total,
			extendedList: /* @__PURE__ */ React.createElement(InquirySearchTable, {
				key: searchObj.searchItem.id,
				queryData: searchObj.queryData || [],
				columns: searchObj.searchItem.columns,
				apiMethodId: searchObj.searchItem.apiMethodId,
				searchFilterElements: searchObj.searchItem.searchFilterElements,
				searchFilterGroups: searchObj.searchItem.searchFilterGroups,
				catalogReturnObject: searchObj.searchItem.catalogReturnObject,
				setSelectedSearchRows,
				catalogObjects,
				treeStructure: searchObj.searchItem.treeStructure,
				associatedInquiryTree: searchObj.searchItem.associatedInquiryTree || null,
				onRowDoubleClick,
				isAdvancedSearch,
				executeQuery: executeQueryAdHoc,
				queryColumnsData: searchObj.searchItem.queryColumnsData
			})
		}, !!searchObj?.queryData?.pageList?.length && /* @__PURE__ */ React.createElement(InquirySearchTable, {
			key: searchObj.searchItem.id,
			queryData: searchObj.queryData || [],
			columns: searchObj.searchItem.columns,
			apiMethodId: searchObj.searchItem.apiMethodId,
			searchFilterElements: searchObj.searchItem.searchFilterElements,
			searchFilterGroups: searchObj.searchItem.searchFilterGroups,
			catalogReturnObject: searchObj.searchItem.catalogReturnObject,
			setSelectedSearchRows,
			catalogObjects,
			treeStructure: searchObj.searchItem.treeStructure,
			associatedInquiryTree: searchObj.searchItem.associatedInquiryTree || null,
			onRowDoubleClick,
			topResultsOnly: true,
			topResultsSize: 10,
			isAdvancedSearch,
			executeQuery: executeQueryAdHoc,
			queryColumnsData: searchObj.searchItem.queryColumnsData
		}))));
		$[19] = catalogObjects;
		$[20] = classes.searchCard;
		$[21] = executeQueryAdHoc;
		$[22] = isAdvancedSearch;
		$[23] = onRowDoubleClick;
		$[24] = searchList;
		$[25] = setSelectedSearchRows;
		$[26] = t5;
	} else t5 = $[26];
	let t6;
	if ($[27] !== t3 || $[28] !== t4 || $[29] !== t5) {
		t6 = /* @__PURE__ */ React.createElement("div", { className: t3 }, t4, t5);
		$[27] = t3;
		$[28] = t4;
		$[29] = t5;
		$[30] = t6;
	} else t6 = $[30];
	return t6;
};
function _temp(apiMethodInstance) {
	if (!apiMethodInstance) return [];
	let catalogMods = [];
	try {
		const catalogModificationStr = apiMethodInstance.catalogModification;
		catalogMods = JSON.parse(catalogModificationStr);
	} catch {
		console.warn("Error parsing apiMethodInstance catalogModification");
	}
	return catalogMods;
}
function _temp2(sfe_0) {
	return sfe_0.searchOperator !== "=";
}
function _temp3(sfe) {
	return sfe.searchOperator === "=";
}
function _temp4(e) {
	return e;
}
function _temp5(p) {
	return p.catch(_temp4);
}
function _temp6(item_1) {
	return item_1?.queryData?.pageList?.length === 1;
}
//#endregion
export { InquirySearchContainer };

//# sourceMappingURL=InquirySearchContainer.mjs.map