import { Form } from "../../utilities/form/Form.mjs";
import { FluentButton } from "../../UI/inputs/buttons/FluentButton.mjs";
import { SubmitButton } from "../../utilities/form/SubmitButton.mjs";
import { FormButtons } from "../../utilities/form/FormButtons.mjs";
import { LookupTypeEnums } from "../../enums/unitySystemEnums.mjs";
import { FluentTextFieldAutoComplete } from "../../UI/inputs/textField/FluentTextFieldAutoComplete.mjs";
import { FluentIconButton } from "../../UI/inputs/buttons/FluentIconButton.mjs";
import { getSanitizedName } from "../../utilities/entities/entityUtilities.mjs";
import { SearchOperators } from "../../models/SearchOperator.mjs";
import { InquiryAdvancedSearchForm } from "./InquiryAdvancedSearchForm.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { Divider, Typography, makeStyles } from "@material-ui/core";
import { ClearIcon } from "@fluentui/react-icons";
import { orderBy } from "lodash-es";
//#region src/inquiry/search/InquiryAdvancedSearch.jsx
const useStyles = makeStyles((theme) => ({
	root: {},
	entitySelect: {
		marginRight: theme.spacing(1),
		marginLeft: theme.spacing(1)
	},
	fieldsRoot: { height: (props) => "calc(100vh - 288px)" },
	title: {
		display: "flex",
		justifyContent: "space-between",
		margin: theme.spacing(1)
	},
	buttonWrapper: { textAlign: "center" },
	button: {
		textTransform: "none",
		padding: 0,
		"&:hover": { backgroundColor: "rgba(0, 0, 0, 0)" },
		color: theme.palette.secondary
	}
}));
const InquiryAdvancedSearch = (props) => {
	const $ = c(86);
	const { entityList, onSubmit, currentNodeCatalogReturnObject, catalogObjects, entityValue, setEntityValue, changeSideBarView, tree, executeQueryWithParameters } = props;
	const classes = useStyles(props);
	const [initialSelectedSearchOperators, setInitialSelectedSearchOperators] = useState(null);
	const [selectedSearchOperators, setSelectedSearchOperators] = useState(null);
	const [form, setForm] = useState(null);
	let t0;
	bb0: {
		if (!entityValue || !tree) {
			t0 = null;
			break bb0;
		}
		let t1;
		if ($[0] !== entityValue || $[1] !== tree.treeStructure) {
			let t2;
			if ($[3] !== entityValue) {
				t2 = (node) => node.properties.catalogReturnObject === entityValue.value;
				$[3] = entityValue;
				$[4] = t2;
			} else t2 = $[4];
			t1 = tree.treeStructure.find(t2);
			$[0] = entityValue;
			$[1] = tree.treeStructure;
			$[2] = t1;
		} else t1 = $[2];
		const matchingEntityTree = t1;
		let nodeTree = tree;
		if (matchingEntityTree?.properties?.associatedInquiryTree) nodeTree = matchingEntityTree?.properties?.associatedInquiryTree;
		t0 = nodeTree;
	}
	const entityTree = t0;
	let t1;
	bb1: {
		if (!entityValue || !catalogObjects) {
			let t2;
			if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
				t2 = [];
				$[5] = t2;
			} else t2 = $[5];
			t1 = t2;
			break bb1;
		}
		let t2;
		if ($[6] !== catalogObjects || $[7] !== entityValue) {
			let t3;
			if ($[9] !== entityValue) {
				t3 = (catalog) => catalog.catalogObjectName === entityValue.value;
				$[9] = entityValue;
				$[10] = t3;
			} else t3 = $[10];
			t2 = catalogObjects.find(t3);
			$[6] = catalogObjects;
			$[7] = entityValue;
			$[8] = t2;
		} else t2 = $[8];
		const foundCatalogObject = t2;
		let t3;
		if ($[11] !== foundCatalogObject?.properties) {
			t3 = foundCatalogObject?.properties || [];
			$[11] = foundCatalogObject?.properties;
			$[12] = t3;
		} else t3 = $[12];
		t1 = t3;
	}
	const catalogProperties = t1;
	let t2;
	bb2: {
		if (!entityTree) {
			t2 = null;
			break bb2;
		}
		let groupedAndOrderdFields;
		if ($[13] !== entityTree.treeStructure) {
			const advancedSearchConfiguration = entityTree.treeStructure.find(_temp)?.properties?.searchConfiguration?.filter(_temp2);
			advancedSearchConfiguration?.forEach(_temp3);
			const orderedSearchConfiguration = orderBy(advancedSearchConfiguration, ["advancedSearch"], ["asc"]);
			groupedAndOrderdFields = {};
			orderedSearchConfiguration.forEach((config_1) => {
				const wholeInt = Math.floor(config_1.advancedSearch);
				if (!groupedAndOrderdFields[wholeInt]) groupedAndOrderdFields[wholeInt] = [config_1];
				else groupedAndOrderdFields[wholeInt].push(config_1);
			});
			$[13] = entityTree.treeStructure;
			$[14] = groupedAndOrderdFields;
		} else groupedAndOrderdFields = $[14];
		t2 = groupedAndOrderdFields;
	}
	const advancedSearchFields = t2;
	let t3;
	bb3: {
		if (!entityTree) {
			let t4;
			if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
				t4 = [];
				$[15] = t4;
			} else t4 = $[15];
			t3 = t4;
			break bb3;
		}
		let lookupPropertyNames;
		if ($[16] !== catalogProperties || $[17] !== entityTree.treeStructure) {
			const advancedSearchPropertyName = entityTree.treeStructure.find(_temp4)?.properties?.searchConfiguration?.filter(_temp5).map(_temp6);
			const filteredProperties = catalogProperties.filter((property) => advancedSearchPropertyName?.includes(property.name));
			lookupPropertyNames = [];
			filteredProperties.forEach((property_0) => {
				if (property_0?.extendedProperties?.useLookup !== LookupTypeEnums.No_Lookup || !property_0?.extendedProperties?.useLookup) lookupPropertyNames.push(property_0.name);
			});
			$[16] = catalogProperties;
			$[17] = entityTree.treeStructure;
			$[18] = lookupPropertyNames;
		} else lookupPropertyNames = $[18];
		t3 = lookupPropertyNames;
	}
	const asCatalogLookupPropertyNames = t3;
	let t4;
	if ($[19] !== setEntityValue) {
		t4 = (value) => {
			setEntityValue(value);
		};
		$[19] = setEntityValue;
		$[20] = t4;
	} else t4 = $[20];
	const handleEntityChange = t4;
	let t5;
	if ($[21] !== asCatalogLookupPropertyNames || $[22] !== entityValue?.value || $[23] !== onSubmit || $[24] !== selectedSearchOperators) {
		t5 = (values) => {
			if (onSubmit) onSubmit(values, selectedSearchOperators, entityValue?.value, asCatalogLookupPropertyNames);
		};
		$[21] = asCatalogLookupPropertyNames;
		$[22] = entityValue?.value;
		$[23] = onSubmit;
		$[24] = selectedSearchOperators;
		$[25] = t5;
	} else t5 = $[25];
	const handleOnSubmit = t5;
	let t6;
	if ($[26] !== form || $[27] !== initialSelectedSearchOperators) {
		t6 = () => {
			if (form) form.reset();
			setSelectedSearchOperators(initialSelectedSearchOperators);
		};
		$[26] = form;
		$[27] = initialSelectedSearchOperators;
		$[28] = t6;
	} else t6 = $[28];
	const handleClearFilter = t6;
	let t7;
	let t8;
	if ($[29] !== currentNodeCatalogReturnObject || $[30] !== entityList || $[31] !== setEntityValue) {
		t7 = () => {
			if (entityList?.length) {
				const foundEntity = entityList.find((entity) => entity.value === currentNodeCatalogReturnObject);
				if (foundEntity) setEntityValue(foundEntity);
			}
		};
		t8 = [
			entityList,
			currentNodeCatalogReturnObject,
			setEntityValue
		];
		$[29] = currentNodeCatalogReturnObject;
		$[30] = entityList;
		$[31] = setEntityValue;
		$[32] = t7;
		$[33] = t8;
	} else {
		t7 = $[32];
		t8 = $[33];
	}
	useEffect(t7, t8);
	let t10;
	let t9;
	if ($[34] !== advancedSearchFields) {
		t9 = () => {
			if (advancedSearchFields) {
				const initialSearchOperators = [].concat.apply([], Object.values(advancedSearchFields)).reduce(_temp7, {});
				setInitialSelectedSearchOperators(initialSearchOperators);
				setSelectedSearchOperators(initialSearchOperators);
			}
		};
		t10 = [advancedSearchFields];
		$[34] = advancedSearchFields;
		$[35] = t10;
		$[36] = t9;
	} else {
		t10 = $[35];
		t9 = $[36];
	}
	useEffect(t9, t10);
	const t11 = classes.root;
	let t12;
	if ($[37] === Symbol.for("react.memo_cache_sentinel")) {
		t12 = /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Divider, null));
		$[37] = t12;
	} else t12 = $[37];
	let t13;
	if ($[38] === Symbol.for("react.memo_cache_sentinel")) {
		t13 = /* @__PURE__ */ React.createElement(Typography, { variant: "subtitle1" }, "Advanced Search");
		$[38] = t13;
	} else t13 = $[38];
	let t14;
	if ($[39] !== changeSideBarView) {
		t14 = /* @__PURE__ */ React.createElement(FluentIconButton, {
			id: "udpRecord-InquiryAdvancedSearch-ClearIcon",
			udprecordid: "udpRecord-InquiryAdvancedSearch-ClearIcon",
			icon: ClearIcon,
			onClick: () => changeSideBarView(0)
		});
		$[39] = changeSideBarView;
		$[40] = t14;
	} else t14 = $[40];
	let t15;
	if ($[41] !== classes.title || $[42] !== t14) {
		t15 = /* @__PURE__ */ React.createElement("div", { className: classes.title }, t13, t14);
		$[41] = classes.title;
		$[42] = t14;
		$[43] = t15;
	} else t15 = $[43];
	let t16;
	if ($[44] !== entityList || $[45] !== entityValue || $[46] !== handleEntityChange) {
		t16 = /* @__PURE__ */ React.createElement(FluentTextFieldAutoComplete, {
			id: "udpRecord-InquiryAdvancedSearch-TextField",
			udprecordid: "udpRecord-InquiryAdvancedSearch-TextField",
			label: "Entity",
			variant: "outlined",
			margin: "normal",
			size: "small",
			fullWidth: true,
			options: entityList,
			optionKey: "label",
			value: entityValue,
			onChange: handleEntityChange
		});
		$[44] = entityList;
		$[45] = entityValue;
		$[46] = handleEntityChange;
		$[47] = t16;
	} else t16 = $[47];
	let t17;
	if ($[48] !== classes.entitySelect || $[49] !== t16) {
		t17 = /* @__PURE__ */ React.createElement("div", { className: classes.entitySelect }, t16);
		$[48] = classes.entitySelect;
		$[49] = t16;
		$[50] = t17;
	} else t17 = $[50];
	const t18 = entityValue?.value;
	let t19;
	if ($[51] !== t18) {
		t19 = getSanitizedName(t18);
		$[51] = t18;
		$[52] = t19;
	} else t19 = $[52];
	let t20;
	if ($[53] !== classes.fieldsRoot) {
		t20 = { root: classes.fieldsRoot };
		$[53] = classes.fieldsRoot;
		$[54] = t20;
	} else t20 = $[54];
	let t21;
	if ($[55] !== advancedSearchFields || $[56] !== asCatalogLookupPropertyNames || $[57] !== catalogProperties || $[58] !== executeQueryWithParameters || $[59] !== selectedSearchOperators || $[60] !== t19 || $[61] !== t20) {
		t21 = /* @__PURE__ */ React.createElement(InquiryAdvancedSearchForm, {
			advancedSearchFields,
			selectedSearchOperators,
			setSelectedSearchOperators,
			catalogProperties,
			entityType: t19,
			lookupPropertyNames: asCatalogLookupPropertyNames,
			classes: t20,
			executeQueryWithParameters,
			setForm
		});
		$[55] = advancedSearchFields;
		$[56] = asCatalogLookupPropertyNames;
		$[57] = catalogProperties;
		$[58] = executeQueryWithParameters;
		$[59] = selectedSearchOperators;
		$[60] = t19;
		$[61] = t20;
		$[62] = t21;
	} else t21 = $[62];
	let t22;
	if ($[63] === Symbol.for("react.memo_cache_sentinel")) {
		t22 = /* @__PURE__ */ React.createElement(Typography, { variant: "subtitle1" }, "Clear Filter");
		$[63] = t22;
	} else t22 = $[63];
	let t23;
	if ($[64] !== classes.button || $[65] !== handleClearFilter) {
		t23 = /* @__PURE__ */ React.createElement(FluentButton, {
			id: "udpRecord-InquiryAdvancedSearch-ClearFilter",
			udprecordid: "udpRecord-InquiryAdvancedSearch-ClearFilter",
			onClick: handleClearFilter,
			className: classes.button
		}, t22);
		$[64] = classes.button;
		$[65] = handleClearFilter;
		$[66] = t23;
	} else t23 = $[66];
	const t24 = !entityValue;
	let t25;
	if ($[67] === Symbol.for("react.memo_cache_sentinel")) {
		t25 = {
			marginRight: 8,
			marginLeft: 8
		};
		$[67] = t25;
	} else t25 = $[67];
	let t26;
	if ($[68] !== t24) {
		t26 = /* @__PURE__ */ React.createElement(FormButtons, null, /* @__PURE__ */ React.createElement(SubmitButton, {
			id: "udpRecord-InquiryAdvancedSearch-Search",
			udprecordid: "udpRecord-InquiryAdvancedSearch-Search",
			variant: "contained",
			color: "primary",
			disablePristine: false,
			disabled: t24,
			fullWidth: true,
			style: t25
		}, "Search"));
		$[68] = t24;
		$[69] = t26;
	} else t26 = $[69];
	let t27;
	if ($[70] !== classes.buttonWrapper || $[71] !== t23 || $[72] !== t26) {
		t27 = /* @__PURE__ */ React.createElement("div", { className: classes.buttonWrapper }, t23, t26);
		$[70] = classes.buttonWrapper;
		$[71] = t23;
		$[72] = t26;
		$[73] = t27;
	} else t27 = $[73];
	let t28;
	if ($[74] !== classes.submitWrapper || $[75] !== t27) {
		t28 = /* @__PURE__ */ React.createElement("div", { className: classes.submitWrapper }, t27);
		$[74] = classes.submitWrapper;
		$[75] = t27;
		$[76] = t28;
	} else t28 = $[76];
	let t29;
	if ($[77] !== handleOnSubmit || $[78] !== t21 || $[79] !== t28) {
		t29 = /* @__PURE__ */ React.createElement(Form, { onSubmit: handleOnSubmit }, t21, t28);
		$[77] = handleOnSubmit;
		$[78] = t21;
		$[79] = t28;
		$[80] = t29;
	} else t29 = $[80];
	let t30;
	if ($[81] !== classes.root || $[82] !== t15 || $[83] !== t17 || $[84] !== t29) {
		t30 = /* @__PURE__ */ React.createElement("div", { className: t11 }, t12, t15, t17, t29);
		$[81] = classes.root;
		$[82] = t15;
		$[83] = t17;
		$[84] = t29;
		$[85] = t30;
	} else t30 = $[85];
	return t30;
};
function _temp(node_0) {
	return !node_0.parentId;
}
function _temp2(config) {
	return config.advancedSearch;
}
function _temp3(config_0) {
	return config_0.advancedSearch = Number.parseFloat(config_0.advancedSearch);
}
function _temp4(node_1) {
	return !node_1.parentId;
}
function _temp5(config_2) {
	return config_2.advancedSearch;
}
function _temp6(asConfig) {
	return asConfig.name;
}
function _temp7(operators, field) {
	if (field.advancedSearchOperator) {
		const foundOperator = SearchOperators.find((op) => op.value === field.advancedSearchOperator);
		operators[field.name] = foundOperator ?? SearchOperators[0];
	} else operators[field.name] = SearchOperators[0];
	return operators;
}
//#endregion
export { InquiryAdvancedSearch };

//# sourceMappingURL=InquiryAdvancedSearch.mjs.map