import { LogicalSearchOperatorEnums } from "../../enums/unitySystemEnums.mjs";
import { camelToPascalCaseWithSpace } from "../../utilities/input/StringUtilities.mjs";
import { getSearchIdentifiers } from "../../utilities/search/SearchUtilities.mjs";
import { PrimaryActionHeader } from "../../UI/utilityDisplay/utilityHeader/PrimaryActionHeader.mjs";
import { AgTable, useAgGridApi } from "../../utilities/aggrid/AgTable.mjs";
import { useExport, useGridFormat } from "../../utilities/aggrid/GridFunctions.mjs";
import { InquirySearchDatasource } from "./InquirySearchDatasource.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { makeStyles } from "@material-ui/core";
import { sortBy } from "lodash-es";
import { ServerSideRowModelModule } from "@ag-grid-enterprise/server-side-row-model";
//#region src/inquiry/search/InquirySearchTable.jsx
const useStyles = makeStyles((theme) => ({ root: { height: (props) => {
	const rowCount = props?.queryData?.pageList?.length > 10 ? 10 : props?.queryData?.pageList?.length ?? 0;
	const topResultsOnlyHeight = rowCount * 48 + 60;
	const moreResultsHeight = rowCount * 48 + 116;
	return props.topResultsOnly ? topResultsOnlyHeight : moreResultsHeight;
} } }));
const InquirySearchTable = (props) => {
	const $ = c(64);
	const { apiMethodId, associatedInquiryTree, catalogReturnObject, catalogObjects, columns, executeQuery, queryColumnsData, isAdvancedSearch, onRowDoubleClick, queryData, searchFilterElements, searchFilterGroups, setSelectedSearchRows, treeStructure, topResultsOnly: t0, topResultsSize: t1 } = props;
	const topResultsOnly = t0 === void 0 ? false : t0;
	const topResultsSize = t1 === void 0 ? 50 : t1;
	const classes = useStyles(props);
	const { gridApi, onGridReady } = useAgGridApi();
	let t2;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = {};
		$[0] = t2;
	} else t2 = $[0];
	const [agGridOptions] = useState(t2);
	const [gridFormat] = useGridFormat();
	const [exportGrid] = useExport();
	let t3;
	if ($[1] !== catalogObjects || $[2] !== catalogReturnObject) {
		t3 = (getSearchIdentifiers(catalogObjects, catalogReturnObject) || []).toString();
		$[1] = catalogObjects;
		$[2] = catalogReturnObject;
		$[3] = t3;
	} else t3 = $[3];
	const searchIdentifier = t3;
	let commonCols;
	let t4;
	if ($[4] !== catalogReturnObject || $[5] !== columns || $[6] !== queryColumnsData || $[7] !== treeStructure) {
		let t5;
		if ($[10] !== queryColumnsData) {
			t5 = (column) => {
				let matchingColumnIndex = null;
				const matchingColumn = queryColumnsData.find((c, index) => {
					if (c.name === column?.name) matchingColumnIndex = index;
					return c.name === column?.name;
				});
				let def = {
					headerName: column.alias || camelToPascalCaseWithSpace(column.name),
					field: column.name
				};
				if (matchingColumn) def = {
					headerName: matchingColumn.alias || camelToPascalCaseWithSpace(matchingColumn.name),
					field: matchingColumn.name
				};
				if (column?.extendedProperties?.searchable) def = {
					...def,
					filter: "agTextColumnFilter",
					filterParams: {
						suppressAndOrCondition: true,
						filterOptions: [
							"equals",
							"contains",
							"notEqual",
							"greaterThan",
							"lessThan",
							"greaterThanOrEqual",
							"lessThanOrEqual"
						]
					},
					enableRowGroup: true
				};
				if (column?.extendedProperties?.orderable) def = {
					...def,
					sortable: true
				};
				def.hide = !matchingColumn;
				def.index = matchingColumnIndex;
				return def;
			};
			$[10] = queryColumnsData;
			$[11] = t5;
		} else t5 = $[11];
		commonCols = columns.map(t5);
		let t6;
		if ($[12] !== catalogReturnObject) {
			t6 = (node) => node.properties?.catalogReturnObject === catalogReturnObject;
			$[12] = catalogReturnObject;
			$[13] = t6;
		} else t6 = $[13];
		const searchConfiguration = treeStructure.find(t6)?.properties?.searchConfiguration || null;
		let requiresOrdering = false;
		if (searchConfiguration) requiresOrdering = searchConfiguration.some(_temp);
		if (requiresOrdering) {
			const searchConfigWithResults = searchConfiguration.filter(_temp2);
			commonCols.forEach((col) => {
				col.index = null;
				const foundSearchConfig = searchConfigWithResults.find((config_1) => config_1.name === col.field);
				if (foundSearchConfig) col.index = parseFloat(foundSearchConfig.results);
			});
		}
		t4 = sortBy(commonCols, _temp3);
		$[4] = catalogReturnObject;
		$[5] = columns;
		$[6] = queryColumnsData;
		$[7] = treeStructure;
		$[8] = commonCols;
		$[9] = t4;
	} else {
		commonCols = $[8];
		t4 = $[9];
	}
	commonCols = t4;
	const columnDefs = commonCols;
	let t5;
	if ($[14] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = {
			resizable: true,
			editable: false,
			enableValue: true,
			allowedAggFuncs: [
				"sum",
				"min",
				"max",
				"count",
				"avg",
				"first",
				"last"
			]
		};
		$[14] = t5;
	} else t5 = $[14];
	const defaultColDef = t5;
	let t6;
	bb0: {
		if (!queryData) {
			let t7;
			if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
				t7 = [];
				$[15] = t7;
			} else t7 = $[15];
			t6 = t7;
			break bb0;
		}
		let t7;
		if ($[16] !== queryData?.pageList || $[17] !== topResultsSize) {
			t7 = queryData?.pageList?.slice(0, topResultsSize);
			$[16] = queryData?.pageList;
			$[17] = topResultsSize;
			$[18] = t7;
		} else t7 = $[18];
		t6 = t7;
	}
	const rowData = t6;
	let t7;
	if ($[19] !== apiMethodId || $[20] !== associatedInquiryTree || $[21] !== catalogReturnObject || $[22] !== gridApi || $[23] !== searchIdentifier || $[24] !== setSelectedSearchRows || $[25] !== treeStructure) {
		t7 = () => {
			setSelectedSearchRows(catalogReturnObject, gridApi.getSelectedRows(), searchIdentifier, apiMethodId, treeStructure, associatedInquiryTree);
		};
		$[19] = apiMethodId;
		$[20] = associatedInquiryTree;
		$[21] = catalogReturnObject;
		$[22] = gridApi;
		$[23] = searchIdentifier;
		$[24] = setSelectedSearchRows;
		$[25] = treeStructure;
		$[26] = t7;
	} else t7 = $[26];
	const handleSelectionChanged = t7;
	let t8;
	if ($[27] !== catalogReturnObject || $[28] !== gridApi || $[29] !== onRowDoubleClick) {
		t8 = (event) => {
			if (!gridApi) return;
			if (onRowDoubleClick) onRowDoubleClick(event.node.data, catalogReturnObject);
		};
		$[27] = catalogReturnObject;
		$[28] = gridApi;
		$[29] = onRowDoubleClick;
		$[30] = t8;
	} else t8 = $[30];
	const handleRowDoubleClicked = t8;
	let t10;
	let t9;
	if ($[31] !== apiMethodId || $[32] !== executeQuery || $[33] !== gridApi || $[34] !== isAdvancedSearch || $[35] !== queryData || $[36] !== searchFilterElements || $[37] !== searchFilterGroups) {
		t9 = () => {
			if (!gridApi) return;
			gridApi.sizeColumnsToFit();
			const datasource = InquirySearchDatasource(queryData, apiMethodId, searchFilterElements, isAdvancedSearch ? LogicalSearchOperatorEnums.And : LogicalSearchOperatorEnums.Or, executeQuery, searchFilterGroups);
			gridApi?.setServerSideDatasource(datasource);
		};
		t10 = [
			gridApi,
			apiMethodId,
			searchFilterElements,
			queryData,
			isAdvancedSearch,
			executeQuery,
			searchFilterGroups
		];
		$[31] = apiMethodId;
		$[32] = executeQuery;
		$[33] = gridApi;
		$[34] = isAdvancedSearch;
		$[35] = queryData;
		$[36] = searchFilterElements;
		$[37] = searchFilterGroups;
		$[38] = t10;
		$[39] = t9;
	} else {
		t10 = $[38];
		t9 = $[39];
	}
	useEffect(t9, t10);
	let t11;
	if ($[40] !== agGridOptions || $[41] !== classes || $[42] !== columnDefs || $[43] !== exportGrid || $[44] !== gridApi || $[45] !== gridFormat || $[46] !== handleRowDoubleClicked || $[47] !== handleSelectionChanged || $[48] !== onGridReady || $[49] !== topResultsOnly) {
		t11 = !topResultsOnly && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(PrimaryActionHeader, {
			hidePAB: true,
			hideActionRow: true,
			expandAllItems: () => gridFormat(gridApi, "expand"),
			collapse: () => gridFormat(gridApi, "collapse"),
			resizeGrid: () => gridFormat(gridApi, "resize"),
			exportGrid: () => exportGrid(gridApi, "csv"),
			grid: true
		}), /* @__PURE__ */ React.createElement(AgTable, {
			className: classes.root,
			modules: [ServerSideRowModelModule],
			rowModelType: "serverSide",
			serverSideStoreType: "partial",
			pagination: true,
			paginationPageSize: 20,
			cacheOverflowSize: 2,
			maxConcurrentDatasourceRequests: 1,
			cacheBlockSize: 20,
			onGridReady,
			columnDefs,
			defaultColDef,
			onRowDoubleClicked: handleRowDoubleClicked,
			rowSelection: "multiple",
			rowMultiSelectWithClick: true,
			onSelectionChanged: handleSelectionChanged,
			api: gridApi,
			dynamicSizing: true,
			...agGridOptions
		}));
		$[40] = agGridOptions;
		$[41] = classes;
		$[42] = columnDefs;
		$[43] = exportGrid;
		$[44] = gridApi;
		$[45] = gridFormat;
		$[46] = handleRowDoubleClicked;
		$[47] = handleSelectionChanged;
		$[48] = onGridReady;
		$[49] = topResultsOnly;
		$[50] = t11;
	} else t11 = $[50];
	let t12;
	if ($[51] !== agGridOptions || $[52] !== classes || $[53] !== columnDefs || $[54] !== gridApi || $[55] !== handleRowDoubleClicked || $[56] !== handleSelectionChanged || $[57] !== onGridReady || $[58] !== rowData || $[59] !== topResultsOnly) {
		t12 = topResultsOnly && /* @__PURE__ */ React.createElement(AgTable, {
			className: classes.root,
			rowData: rowData || [],
			onGridReady,
			columnDefs,
			defaultColDef,
			onRowDoubleClicked: handleRowDoubleClicked,
			rowSelection: "multiple",
			rowMultiSelectWithClick: true,
			onSelectionChanged: handleSelectionChanged,
			animateRows: true,
			api: gridApi,
			dynamicSizing: true,
			...agGridOptions
		});
		$[51] = agGridOptions;
		$[52] = classes;
		$[53] = columnDefs;
		$[54] = gridApi;
		$[55] = handleRowDoubleClicked;
		$[56] = handleSelectionChanged;
		$[57] = onGridReady;
		$[58] = rowData;
		$[59] = topResultsOnly;
		$[60] = t12;
	} else t12 = $[60];
	let t13;
	if ($[61] !== t11 || $[62] !== t12) {
		t13 = /* @__PURE__ */ React.createElement("div", null, t11, t12);
		$[61] = t11;
		$[62] = t12;
		$[63] = t13;
	} else t13 = $[63];
	return t13;
};
function _temp(config) {
	return !!config.results.trim();
}
function _temp2(config_0) {
	return !!config_0.results;
}
function _temp3(col_0) {
	return col_0.index;
}
//#endregion
export { InquirySearchTable };

//# sourceMappingURL=InquirySearchTable.mjs.map