import { LookupTypeEnums } from "../../enums/unitySystemEnums.mjs";
import { FluentTextFieldAutoComplete } from "../../UI/inputs/textField/FluentTextFieldAutoComplete.mjs";
import { PropertyTypeEnum } from "../../enums/propertyTypeEnums.mjs";
import { SearchOperators } from "../../models/SearchOperator.mjs";
import { PropertyField } from "../../UI/property/PropertyFields.mjs";
import { csvStrToArrayOfObjects } from "../../utilities/input/SelectUtilities.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect } from "react";
import { Divider, Typography, makeStyles } from "@material-ui/core";
import { useForm } from "react-final-form";
import clsx from "clsx";
import { v4 } from "uuid";
//#region src/inquiry/search/InquiryAdvancedSearchForm.jsx
const useStyles = makeStyles((theme) => ({
	root: {
		marginRight: theme.spacing(1),
		marginLeft: theme.spacing(1),
		overflowX: "auto"
	},
	fieldRow: {
		display: "flex",
		alignItems: "center"
	},
	dividerContainer: {
		display: "flex",
		justifyContent: "space-between",
		alignItems: "center",
		width: "100%"
	},
	divider: {
		width: "100%",
		marginTop: theme.spacing(1)
	},
	autocompleteRoot: {
		width: "38%",
		marginRight: theme.spacing(1)
	}
}));
const InquiryAdvancedSearchForm = (props) => {
	const $ = c(40);
	const { advancedSearchFields, selectedSearchOperators, setSelectedSearchOperators, catalogProperties, entityType, lookupPropertyNames, classes: classProps, setForm } = props;
	let t0;
	if ($[0] !== classProps) {
		t0 = classProps || {};
		$[0] = classProps;
		$[1] = t0;
	} else t0 = $[1];
	const { root } = t0;
	const classes = useStyles();
	const form = useForm();
	let t1;
	if ($[2] !== selectedSearchOperators || $[3] !== setSelectedSearchOperators) {
		t1 = (value, fieldName) => {
			if (fieldName) {
				let updatedSelectedSearchOps = {};
				if (selectedSearchOperators) updatedSelectedSearchOps = { ...selectedSearchOperators };
				updatedSelectedSearchOps[fieldName] = value;
				setSelectedSearchOperators(updatedSelectedSearchOps);
			}
		};
		$[2] = selectedSearchOperators;
		$[3] = setSelectedSearchOperators;
		$[4] = t1;
	} else t1 = $[4];
	const handleSearchOperatorChange = t1;
	const getPropertyWidgetType = _temp;
	const getPropertyTypeSearchOperators = _temp3;
	let t2;
	bb0: {
		if (!advancedSearchFields || !catalogProperties) {
			let t3;
			if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
				t3 = [];
				$[5] = t3;
			} else t3 = $[5];
			t2 = t3;
			break bb0;
		}
		let t3;
		if ($[6] !== advancedSearchFields || $[7] !== catalogProperties || $[8] !== lookupPropertyNames) {
			t3 = Object.keys(advancedSearchFields).reduce((list, key) => {
				const groupedItems = advancedSearchFields[key];
				let groupHeader = "";
				const foundGroupItem = groupedItems.find(_temp4);
				if (foundGroupItem) groupHeader = foundGroupItem.groupHeader;
				list.push({
					groupHeader,
					isDivider: true,
					id: v4()
				});
				groupedItems.forEach(async (item_0) => {
					const foundProperty = catalogProperties.find((property) => property.name === item_0.name);
					const propertyWidgetType_0 = getPropertyWidgetType(foundProperty?.type, foundProperty?.extendedProperties?.useLookup);
					if (!item_0.searchable && !foundProperty?.extendedProperties?.searchable) return;
					if (lookupPropertyNames.includes(foundProperty?.name) && foundProperty?.extendedProperties?.useLookup === LookupTypeEnums.Api) {}
					list.push({
						...item_0,
						isDivider: false,
						type: foundProperty?.type,
						propertyWidgetType: propertyWidgetType_0,
						useLookup: foundProperty?.extendedProperties?.useLookup,
						lookup: foundProperty?.extendedProperties?.lookup,
						apiLookupOptions: null
					});
				});
				return list;
			}, []);
			$[6] = advancedSearchFields;
			$[7] = catalogProperties;
			$[8] = lookupPropertyNames;
			$[9] = t3;
		} else t3 = $[9];
		t2 = t3;
	}
	const fieldRows = t2;
	let t3;
	if ($[10] !== entityType || $[11] !== selectedSearchOperators) {
		t3 = (config) => {
			const searchOperator = selectedSearchOperators ? selectedSearchOperators[config?.name]?.value : null;
			let options = [];
			if (config?.useLookup === LookupTypeEnums.Csv) options = csvStrToArrayOfObjects(config?.lookup);
			if (searchOperator === "BETWEEN") return /* @__PURE__ */ React.createElement("div", { style: {
				display: "block",
				width: "100%"
			} }, /* @__PURE__ */ React.createElement(PropertyField, {
				propertyType: entityType,
				property: {
					key: config?.name,
					label: `${config?.columnName} (From)`,
					type: config?.propertyWidgetType,
					options: options || [],
					valueName: "value"
				}
			}), /* @__PURE__ */ React.createElement(PropertyField, {
				propertyType: entityType,
				property: {
					key: `${config?.name}:between`,
					label: `${config?.columnName} (To)`,
					type: config?.propertyWidgetType,
					options: options || [],
					valueName: "value"
				}
			}));
			return /* @__PURE__ */ React.createElement(PropertyField, {
				propertyType: entityType,
				property: {
					key: config?.name,
					label: config?.columnName,
					type: config?.propertyWidgetType,
					options: options || [],
					valueName: "value"
				}
			});
		};
		$[10] = entityType;
		$[11] = selectedSearchOperators;
		$[12] = t3;
	} else t3 = $[12];
	const getPropertyFields = t3;
	let t4;
	bb1: {
		if (!fieldRows || !fieldRows.length) {
			t4 = null;
			break bb1;
		}
		let t5;
		if ($[13] !== classes.autocompleteRoot || $[14] !== classes.divider || $[15] !== classes.dividerContainer || $[16] !== classes.fieldRow || $[17] !== fieldRows || $[18] !== getPropertyFields || $[19] !== handleSearchOperatorChange || $[20] !== selectedSearchOperators) {
			let t6;
			if ($[22] !== classes.autocompleteRoot || $[23] !== classes.divider || $[24] !== classes.dividerContainer || $[25] !== classes.fieldRow || $[26] !== getPropertyFields || $[27] !== handleSearchOperatorChange || $[28] !== selectedSearchOperators) {
				t6 = (config_0) => /* @__PURE__ */ React.createElement("div", { key: config_0?.id }, !config_0?.isDivider && /* @__PURE__ */ React.createElement("div", { className: classes.fieldRow }, /* @__PURE__ */ React.createElement(FluentTextFieldAutoComplete, {
					id: "udpRecord-InquiryAdvancedSearchForm-Text",
					udprecordid: "udpRecord-InquiryAdvancedSearchForm-Text",
					label: "Operator",
					name: "searchOperator",
					variant: "outlined",
					margin: "dense",
					size: "small",
					options: getPropertyTypeSearchOperators(config_0?.type) || [],
					optionKey: "label",
					value: selectedSearchOperators && selectedSearchOperators[config_0?.name] || "",
					onChange: (value_0) => handleSearchOperatorChange(value_0, config_0?.name),
					classes: { autocompleteRoot: classes.autocompleteRoot }
				}), getPropertyFields(config_0)), config_0?.isDivider && /* @__PURE__ */ React.createElement("div", { className: classes.dividerContainer }, /* @__PURE__ */ React.createElement("div", { className: classes.divider }, /* @__PURE__ */ React.createElement(Divider, { orientation: "horizontal" })), config_0?.groupHeader && /* @__PURE__ */ React.createElement("div", { style: {
					marginRight: 8,
					marginLeft: 8,
					textAlign: "center",
					whiteSpace: "nowrap"
				} }, /* @__PURE__ */ React.createElement(Typography, { variant: "caption" }, config_0?.groupHeader)), /* @__PURE__ */ React.createElement("div", { className: classes.divider }, /* @__PURE__ */ React.createElement(Divider, { orientation: "horizontal" }))));
				$[22] = classes.autocompleteRoot;
				$[23] = classes.divider;
				$[24] = classes.dividerContainer;
				$[25] = classes.fieldRow;
				$[26] = getPropertyFields;
				$[27] = handleSearchOperatorChange;
				$[28] = selectedSearchOperators;
				$[29] = t6;
			} else t6 = $[29];
			t5 = fieldRows.map(t6);
			$[13] = classes.autocompleteRoot;
			$[14] = classes.divider;
			$[15] = classes.dividerContainer;
			$[16] = classes.fieldRow;
			$[17] = fieldRows;
			$[18] = getPropertyFields;
			$[19] = handleSearchOperatorChange;
			$[20] = selectedSearchOperators;
			$[21] = t5;
		} else t5 = $[21];
		t4 = t5;
	}
	const fieldComponents = t4;
	let t5;
	let t6;
	if ($[30] !== form || $[31] !== setForm) {
		t5 = () => {
			if (form) setForm(form);
		};
		t6 = [form, setForm];
		$[30] = form;
		$[31] = setForm;
		$[32] = t5;
		$[33] = t6;
	} else {
		t5 = $[32];
		t6 = $[33];
	}
	useEffect(t5, t6);
	let t7;
	if ($[34] !== classes.root || $[35] !== root) {
		t7 = clsx(classes.root, root);
		$[34] = classes.root;
		$[35] = root;
		$[36] = t7;
	} else t7 = $[36];
	let t8;
	if ($[37] !== fieldComponents || $[38] !== t7) {
		t8 = /* @__PURE__ */ React.createElement("div", { className: t7 }, fieldComponents);
		$[37] = fieldComponents;
		$[38] = t7;
		$[39] = t8;
	} else t8 = $[39];
	return t8;
};
function _temp(type, useLookup) {
	if (!type) return null;
	let propertyWidgetType;
	bb14: switch (type) {
		case PropertyTypeEnum.Int:
		case PropertyTypeEnum.Long:
			propertyWidgetType = "IntegerWidget";
			break bb14;
		case PropertyTypeEnum.DateTime:
			propertyWidgetType = "DateTimeWidget";
			break bb14;
		case PropertyTypeEnum.Boolean:
			propertyWidgetType = "BooleanWidget";
			break bb14;
		default: propertyWidgetType = "TextWidget";
	}
	if (useLookup === LookupTypeEnums.Api || useLookup === LookupTypeEnums.Csv) propertyWidgetType = "AutoCompleteWidget";
	return propertyWidgetType;
}
function _temp2(operator) {
	return operator.value === "=";
}
function _temp3(type_0) {
	if (!type_0) return [];
	switch (type_0) {
		case PropertyTypeEnum.Boolean: return SearchOperators.filter(_temp2);
		default: return SearchOperators;
	}
}
function _temp4(item) {
	return !!item.groupHeader;
}
//#endregion
export { InquiryAdvancedSearchForm };

//# sourceMappingURL=InquiryAdvancedSearchForm.mjs.map