import { getPathObject } from "../../utilities/maintenanceEngine/CrudBrowserUtilities.mjs";
import { ConfigService } from "../../configService/index.mjs";
import { UdpEnums } from "../../enums/unitySystemEnums.mjs";
import { useHandleAxiosSnackbar } from "../../utilities/apiHelpers.mjs";
import { useUser } from "../../utilities/auth/useUser.mjs";
import { useAxiosMutate } from "../../utilities/useAxiosMutate.mjs";
import { getSanitizedName } from "../../utilities/entities/entityUtilities.mjs";
import PrimaryActionButton from "../../UI/inputs/buttons/PrimaryActionButton.mjs";
import { SideSheet } from "../../UI/surfaces/SideSheet.mjs";
import { useAxiosGet } from "../../utilities/useAxiosGet.mjs";
import { useSetLocationState } from "../../hooks/useSetLocationState.mjs";
import { useBuildEnvironment } from "../../shell/ui/useBuildEnvironment.mjs";
import { executeQueryWithParameters } from "../../udp/utilities/methodUtils.mjs";
import { AmbientGridTemplate } from "../../UI/templates/deprecated/AmbientGridTemplate/AmbientGridTemplate.mjs";
import { ActionsRenderer } from "../../UI/templates/ui/renderers/ActionsRenderer.mjs";
import { VirtualTableColumnDefs } from "./VirtualTableColumnDefs.mjs";
import { VirtualTableSidePanel } from "./VirtualTableSidePanel.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { Typography } from "@material-ui/core";
import { useLocation } from "react-router-dom";
import { DateTime } from "luxon";
import { AddIcon, DeleteIcon, EditIcon } from "@fluentui/react-icons";
import { useTranslation } from "react-i18next";
import { cloneDeep } from "lodash-es";
//#region src/maintenanceEngine/virtualBrowser/VirtualBrowser.jsx
const useSearchParams = () => {
	const $ = c(2);
	const { search } = useLocation();
	let t0;
	if ($[0] !== search) {
		t0 = new URLSearchParams(search);
		$[0] = search;
		$[1] = t0;
	} else t0 = $[1];
	return t0;
};
const VirtualBrowser = (props) => {
	const $ = c(135);
	let other;
	if ($[0] !== props) {
		({...other} = props);
		$[0] = props;
		$[1] = other;
	} else other = $[1];
	const urlSearchParams = useSearchParams();
	const location = useLocation();
	let t0;
	if ($[2] !== location.state) {
		t0 = location.state || {};
		$[2] = location.state;
		$[3] = t0;
	} else t0 = $[3];
	const { value, promotedEntity } = t0;
	let t1;
	if ($[4] !== location.pathname) {
		t1 = location?.pathname?.split("/");
		$[4] = location.pathname;
		$[5] = t1;
	} else t1 = $[5];
	const parts = t1;
	const virtualId = value ? value?.id : parts ? parts[parts.length - 1] : null;
	const user = useUser();
	const buildEnv = useBuildEnvironment();
	const [isSheetOpen, setSheetOpen] = useState(false);
	const [sideSheetWidth, setSideSheetWidth] = useState(600);
	const [sideSheetButtonLabel, setSideSheetButtonLabel] = useState("");
	let t2;
	if ($[6] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = {};
		$[6] = t2;
	} else t2 = $[6];
	const [otherSideSheetProps, setOtherSideSheetProps] = useState(t2);
	const [sideSheetOnSubmit, setSideSheetOnSubmit] = useState(_temp2);
	const [selectedEntity, setSelectedEntity] = useState(promotedEntity);
	const { t } = useTranslation();
	const { handleErrorSnackbar, handleSuccessSnackbar } = useHandleAxiosSnackbar();
	const setLocationState = useSetLocationState();
	let t3;
	if ($[7] === Symbol.for("react.memo_cache_sentinel")) {
		t3 = () => {
			setSheetOpen(false);
		};
		$[7] = t3;
	} else t3 = $[7];
	const closeSheet = t3;
	let t4;
	if (selectedEntity) t4 = selectedEntity.entityName || "";
	else {
		let t5;
		if ($[8] !== urlSearchParams) {
			t5 = urlSearchParams.has("entityName") === true ? urlSearchParams.get("entityName") : "";
			$[8] = urlSearchParams;
			$[9] = t5;
		} else t5 = $[9];
		t4 = t5;
	}
	const entityName = t4;
	const createApiMethodPageId = selectedEntity?.unityBaseCreateMethod?.pageId;
	let t5;
	if ($[10] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = {};
		$[10] = t5;
	} else t5 = $[10];
	const [t6] = useAxiosGet(ConfigService.integrationV1ApiUrl, `tenantProductApiCatalogBase/tenant/${user?.currentTenantId}/product/${ConfigService.config.UNITY_PRODUCT_ID}`, t5, !!!user || !!promotedEntity, false);
	const { data: tpacb } = t6;
	let t7;
	if ($[11] === Symbol.for("react.memo_cache_sentinel")) {
		t7 = {};
		$[11] = t7;
	} else t7 = $[11];
	const [t8] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${tpacb?.apiCatalogId}/promotedMethodEntities/${entityName}/product/${ConfigService.config.UNITY_PRODUCT_ID}`, t7, !!!tpacb || !!!user || !!!entityName, false);
	const { data: promotedMethodEntities } = t8;
	let t9;
	if ($[12] === Symbol.for("react.memo_cache_sentinel")) {
		t9 = { method: "post" };
		$[12] = t9;
	} else t9 = $[12];
	const [t10, fetchEntityData] = useAxiosMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${selectedEntity?.unityBaseGetDataMethod?.apiMethodId}`, t9);
	const { data: entityData, loading: isLoading } = t10;
	let t11;
	if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
		t11 = { method: "post" };
		$[13] = t11;
	} else t11 = $[13];
	const [t12, fetchTableDescription] = useAxiosMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${selectedEntity?.unityBaseDescribeMethod?.apiMethodId}`, t11);
	const { data: tableDescription } = t12;
	let t13;
	if ($[14] !== entityName || $[15] !== tableDescription) {
		t13 = (values) => {
			if (!tableDescription) return [];
			const bodyArr = [];
			tableDescription.forEach((column) => {
				const valueExist = typeof values[column.name] == "boolean" || values[column.name];
				if (column.name || valueExist) bb104: switch (getSanitizedName(entityName?.toLowerCase())) {
					case UdpEnums.Udp_Lookup.toLowerCase():
						bodyArr.push({
							lookupColumnId: column.id,
							value: values[column.name]
						});
						break bb104;
					default:
				}
			});
			return bodyArr;
		};
		$[14] = entityName;
		$[15] = tableDescription;
		$[16] = t13;
	} else t13 = $[16];
	const constructVirtualValuesForRequest = t13;
	let t14;
	if ($[17] !== constructVirtualValuesForRequest || $[18] !== fetchEntityData || $[19] !== handleErrorSnackbar || $[20] !== handleSuccessSnackbar || $[21] !== selectedEntity || $[22] !== t || $[23] !== virtualId) {
		t14 = async (values_0) => {
			if (selectedEntity?.unityBaseVirtualCreateMethod) {
				if ((await executeQueryWithParameters({ data: {
					requestBody: constructVirtualValuesForRequest(values_0),
					id: virtualId
				} }, selectedEntity.unityBaseVirtualCreateMethod.apiMethodId).catch((res) => {
					handleErrorSnackbar(res.message);
				}))?.status === 200) {
					handleSuccessSnackbar(t("Row has been created"));
					fetchEntityData({ data: { id: virtualId } });
				}
			}
		};
		$[17] = constructVirtualValuesForRequest;
		$[18] = fetchEntityData;
		$[19] = handleErrorSnackbar;
		$[20] = handleSuccessSnackbar;
		$[21] = selectedEntity;
		$[22] = t;
		$[23] = virtualId;
		$[24] = t14;
	} else t14 = $[24];
	const handleOnCreate = t14;
	let t15;
	if ($[25] !== constructVirtualValuesForRequest || $[26] !== fetchEntityData || $[27] !== handleErrorSnackbar || $[28] !== handleSuccessSnackbar || $[29] !== selectedEntity || $[30] !== t || $[31] !== virtualId) {
		t15 = async (values_1) => {
			if (selectedEntity?.unityBaseVirtualUpdateMethod) {
				const primaryKeyValues = [virtualId, values_1.udpVirtualRowId];
				const pathObject = getPathObject(selectedEntity.unityBaseVirtualUpdateMethod.pathParameters, primaryKeyValues);
				const requestArr_0 = constructVirtualValuesForRequest(values_1);
				let data_0 = { requestBody: requestArr_0 };
				if (Array.isArray(pathObject)) pathObject.forEach((po) => {
					data_0[Object.keys(po)[0]] = po[Object.keys(po)[0]];
				});
				else data_0 = {
					...pathObject,
					requestBody: requestArr_0
				};
				if ((await executeQueryWithParameters({ data: data_0 }, selectedEntity.unityBaseVirtualUpdateMethod.apiMethodId).catch((res_0) => {
					handleErrorSnackbar(res_0.message);
				}))?.status === 200) {
					handleSuccessSnackbar(t("Row has been updated"));
					fetchEntityData({ data: { id: virtualId } });
				}
			}
		};
		$[25] = constructVirtualValuesForRequest;
		$[26] = fetchEntityData;
		$[27] = handleErrorSnackbar;
		$[28] = handleSuccessSnackbar;
		$[29] = selectedEntity;
		$[30] = t;
		$[31] = virtualId;
		$[32] = t15;
	} else t15 = $[32];
	const handleOnUpdate = t15;
	let t16;
	if ($[33] !== fetchEntityData || $[34] !== handleErrorSnackbar || $[35] !== handleSuccessSnackbar || $[36] !== selectedEntity || $[37] !== t || $[38] !== virtualId) {
		t16 = async (value_0) => {
			if (selectedEntity?.unityBaseVirtualDeleteMethod) {
				const primaryKeyValues_0 = [virtualId, value_0.udpVirtualRowId];
				const pathObject_0 = getPathObject(selectedEntity?.unityBaseVirtualDeleteMethod.pathParameters, primaryKeyValues_0);
				let data_1 = {};
				if (Array.isArray(pathObject_0)) pathObject_0.forEach((po_0) => {
					data_1[Object.keys(po_0)[0]] = po_0[Object.keys(po_0)[0]];
				});
				else data_1 = { ...pathObject_0 };
				if ((await executeQueryWithParameters({ data: data_1 }, selectedEntity.unityBaseVirtualDeleteMethod.apiMethodId).catch((res_1) => {
					handleErrorSnackbar(res_1.message);
				}))?.status === 200) {
					handleSuccessSnackbar(t("Row has been deleted"));
					fetchEntityData({ data: { id: virtualId } });
				}
			}
		};
		$[33] = fetchEntityData;
		$[34] = handleErrorSnackbar;
		$[35] = handleSuccessSnackbar;
		$[36] = selectedEntity;
		$[37] = t;
		$[38] = virtualId;
		$[39] = t16;
	} else t16 = $[39];
	const handleOnDelete = t16;
	let t17;
	bb0: {
		if (!selectedEntity) {
			t17 = void 0;
			break bb0;
		}
		let t18;
		if ($[40] !== selectedEntity?.unityBaseVirtualUpdateMethod || $[41] !== t) {
			t18 = selectedEntity?.unityBaseVirtualUpdateMethod ? t("Edit") : t("Edit Method Not Accessible");
			$[40] = selectedEntity?.unityBaseVirtualUpdateMethod;
			$[41] = t;
			$[42] = t18;
		} else t18 = $[42];
		const t19 = selectedEntity?.unityBaseVirtualUpdateMethod?.pageId;
		let t20;
		if ($[43] !== handleOnUpdate || $[44] !== t19 || $[45] !== tableDescription) {
			t20 = {
				component: VirtualTableSidePanel,
				properties: {
					tableDescription,
					onUpdate: handleOnUpdate,
					valuePropName: "value",
					closeSideSheet: null,
					closeSideSheetPropName: "closeSideSheet",
					pageId: t19,
					isNewEntry: false
				}
			};
			$[43] = handleOnUpdate;
			$[44] = t19;
			$[45] = tableDescription;
			$[46] = t20;
		} else t20 = $[46];
		const t21 = !!!selectedEntity?.unityBaseVirtualUpdateMethod;
		let t22;
		if ($[47] !== t18 || $[48] !== t20 || $[49] !== t21) {
			t22 = {
				id: 1,
				title: t18,
				icon: EditIcon,
				onClick: null,
				opens: "sideSheet",
				view: t20,
				disabled: t21
			};
			$[47] = t18;
			$[48] = t20;
			$[49] = t21;
			$[50] = t22;
		} else t22 = $[50];
		let t23;
		if ($[51] !== selectedEntity?.unityBaseVirtualDeleteMethod || $[52] !== t) {
			t23 = selectedEntity?.unityBaseVirtualDeleteMethod ? t("Delete") : t("Delete Method Not Accessible");
			$[51] = selectedEntity?.unityBaseVirtualDeleteMethod;
			$[52] = t;
			$[53] = t23;
		} else t23 = $[53];
		let t24;
		if ($[54] !== t) {
			t24 = t("Delete");
			$[54] = t;
			$[55] = t24;
		} else t24 = $[55];
		let t25;
		if ($[56] !== t) {
			t25 = t("Are you sure you want to delete this row?");
			$[56] = t;
			$[57] = t25;
		} else t25 = $[57];
		let t26;
		if ($[58] !== t) {
			t26 = t("Cancel");
			$[58] = t;
			$[59] = t26;
		} else t26 = $[59];
		let t27;
		if ($[60] !== t) {
			t27 = t("Confirm");
			$[60] = t;
			$[61] = t27;
		} else t27 = $[61];
		let t28;
		if ($[62] !== handleOnDelete || $[63] !== t24 || $[64] !== t25 || $[65] !== t26 || $[66] !== t27) {
			t28 = {
				component: null,
				properties: {
					title: t24,
					message: t25,
					labelOne: t26,
					actionTwo: handleOnDelete,
					labelTwo: t27
				}
			};
			$[62] = handleOnDelete;
			$[63] = t24;
			$[64] = t25;
			$[65] = t26;
			$[66] = t27;
			$[67] = t28;
		} else t28 = $[67];
		const t29 = !!!selectedEntity?.unityBaseVirtualDeleteMethod;
		let t30;
		if ($[68] !== t23 || $[69] !== t28 || $[70] !== t29) {
			t30 = {
				id: 2,
				title: t23,
				icon: DeleteIcon,
				onClick: null,
				opens: "dialog",
				view: t28,
				disabled: t29
			};
			$[68] = t23;
			$[69] = t28;
			$[70] = t29;
			$[71] = t30;
		} else t30 = $[71];
		let t31;
		if ($[72] !== t22 || $[73] !== t30) {
			t31 = [t22, t30];
			$[72] = t22;
			$[73] = t30;
			$[74] = t31;
		} else t31 = $[74];
		t17 = t31;
	}
	const actionList = t17;
	let t18;
	bb1: {
		if (tableDescription?.length) {
			let t19;
			if ($[75] !== actionList || $[76] !== tableDescription) {
				t19 = VirtualTableColumnDefs(tableDescription, actionList);
				$[75] = actionList;
				$[76] = tableDescription;
				$[77] = t19;
			} else t19 = $[77];
			t18 = t19;
			break bb1;
		}
		let t19;
		if ($[78] === Symbol.for("react.memo_cache_sentinel")) {
			t19 = [];
			$[78] = t19;
		} else t19 = $[78];
		t18 = t19;
	}
	const columnDefs = t18;
	let t19;
	if ($[79] !== columnDefs) {
		t19 = columnDefs?.map(_temp3);
		$[79] = columnDefs;
		$[80] = t19;
	} else t19 = $[80];
	const groupData = t19;
	let t20;
	let t21;
	if ($[81] !== promotedMethodEntities) {
		t20 = () => {
			if (promotedMethodEntities) setSelectedEntity(promotedMethodEntities);
		};
		t21 = [promotedMethodEntities];
		$[81] = promotedMethodEntities;
		$[82] = t20;
		$[83] = t21;
	} else {
		t20 = $[82];
		t21 = $[83];
	}
	useEffect(t20, t21);
	let t22;
	if ($[84] !== fetchEntityData || $[85] !== fetchTableDescription || $[86] !== selectedEntity?.unityBaseDescribeMethod?.apiMethodId || $[87] !== selectedEntity?.unityBaseGetDataMethod?.apiMethodId || $[88] !== virtualId) {
		t22 = () => {
			if (virtualId) {
				const queryData_2 = { id: virtualId };
				if (selectedEntity?.unityBaseGetDataMethod?.apiMethodId) fetchEntityData({ data: queryData_2 });
				if (selectedEntity?.unityBaseDescribeMethod?.apiMethodId) fetchTableDescription({ data: queryData_2 });
			}
		};
		$[84] = fetchEntityData;
		$[85] = fetchTableDescription;
		$[86] = selectedEntity?.unityBaseDescribeMethod?.apiMethodId;
		$[87] = selectedEntity?.unityBaseGetDataMethod?.apiMethodId;
		$[88] = virtualId;
		$[89] = t22;
	} else t22 = $[89];
	let t23;
	if ($[90] !== fetchEntityData || $[91] !== fetchTableDescription || $[92] !== selectedEntity || $[93] !== virtualId) {
		t23 = [
			selectedEntity,
			fetchEntityData,
			virtualId,
			fetchTableDescription
		];
		$[90] = fetchEntityData;
		$[91] = fetchTableDescription;
		$[92] = selectedEntity;
		$[93] = virtualId;
		$[94] = t23;
	} else t23 = $[94];
	useEffect(t22, t23);
	let t24;
	if ($[95] !== setLocationState || $[96] !== t || $[97] !== value?.name) {
		t24 = () => {
			setLocationState({ label: t(value?.name) || t("Virtual Lookup") });
		};
		$[95] = setLocationState;
		$[96] = t;
		$[97] = value?.name;
		$[98] = t24;
	} else t24 = $[98];
	let t25;
	if ($[99] !== setLocationState || $[100] !== t || $[101] !== value) {
		t25 = [
			setLocationState,
			t,
			value
		];
		$[99] = setLocationState;
		$[100] = t;
		$[101] = value;
		$[102] = t25;
	} else t25 = $[102];
	useEffect(t24, t25);
	let t26;
	if ($[103] === Symbol.for("react.memo_cache_sentinel")) {
		t26 = () => setSheetOpen && setSheetOpen(true);
		$[103] = t26;
	} else t26 = $[103];
	let t27;
	if ($[104] === Symbol.for("react.memo_cache_sentinel")) {
		t27 = /* @__PURE__ */ React.createElement(PrimaryActionButton, { onClick: t26 }, /* @__PURE__ */ React.createElement(Typography, { style: { marginRight: 5 } }, "Add"), /* @__PURE__ */ React.createElement(AddIcon, null));
		$[104] = t27;
	} else t27 = $[104];
	const pab = t27;
	const t28 = `Add new ${entityName}`;
	let t29;
	if ($[105] !== createApiMethodPageId || $[106] !== handleOnCreate || $[107] !== tableDescription) {
		t29 = /* @__PURE__ */ React.createElement(VirtualTableSidePanel, {
			onCreate: handleOnCreate,
			closeSideSheet: closeSheet,
			pageId: createApiMethodPageId,
			setSideSheetWidth,
			setSideSheetButtonLabel,
			setSideSheetOnSubmit,
			setSideSheetProps: setOtherSideSheetProps,
			tableDescription
		});
		$[105] = createApiMethodPageId;
		$[106] = handleOnCreate;
		$[107] = tableDescription;
		$[108] = t29;
	} else t29 = $[108];
	let t30;
	if ($[109] !== isSheetOpen || $[110] !== otherSideSheetProps || $[111] !== sideSheetButtonLabel || $[112] !== sideSheetOnSubmit || $[113] !== sideSheetWidth || $[114] !== t28 || $[115] !== t29) {
		t30 = /* @__PURE__ */ React.createElement(SideSheet, {
			open: isSheetOpen,
			onClose: closeSheet,
			title: t28,
			width: sideSheetWidth,
			buttonLabel: sideSheetButtonLabel,
			onSubmit: sideSheetOnSubmit,
			...otherSideSheetProps
		}, t29);
		$[109] = isSheetOpen;
		$[110] = otherSideSheetProps;
		$[111] = sideSheetButtonLabel;
		$[112] = sideSheetOnSubmit;
		$[113] = sideSheetWidth;
		$[114] = t28;
		$[115] = t29;
		$[116] = t30;
	} else t30 = $[116];
	const SideOptionList = t30;
	let t31;
	if ($[117] !== entityData?.data || $[118] !== tableDescription) {
		t31 = () => {
			if (entityData?.data && tableDescription) {
				const formattedEntityData = cloneDeep(entityData?.data);
				const datetimeFields = tableDescription?.reduce(_temp4, []);
				if (datetimeFields.length) formattedEntityData.forEach((e) => {
					Object.keys(e).forEach((key) => {
						if (datetimeFields.find((field) => field === key)) {
							if (e[key] === null) return;
							e[key] = DateTime.fromJSDate(new Date(e[key])).toFormat("yyyy/MM/dd HH:mm:ss");
						}
					});
				});
				return formattedEntityData;
			}
			return [];
		};
		$[117] = entityData?.data;
		$[118] = tableDescription;
		$[119] = t31;
	} else t31 = $[119];
	const formatData = t31;
	const t32 = value?.name || entityData?.name;
	let t33;
	if ($[120] !== entityData || $[121] !== formatData) {
		t33 = entityData ? formatData(entityData?.data) : [];
		$[120] = entityData;
		$[121] = formatData;
		$[122] = t33;
	} else t33 = $[122];
	let t34;
	if ($[123] === Symbol.for("react.memo_cache_sentinel")) {
		t34 = { actionsRenderer: ActionsRenderer };
		$[123] = t34;
	} else t34 = $[123];
	const t35 = `calc(100vh - ${buildEnv ? "139px" : "114px"})`;
	let t36;
	if ($[124] !== columnDefs || $[125] !== groupData || $[126] !== isLoading || $[127] !== other || $[128] !== t32 || $[129] !== t33 || $[130] !== t35) {
		t36 = /* @__PURE__ */ React.createElement(AmbientGridTemplate, {
			title: t32,
			data: t33,
			columnDefs,
			groupingData: groupData,
			primaryActionButton: pab,
			frameworkComponents: t34,
			height: t35,
			disableRowSelection: true,
			loading: isLoading,
			...other
		});
		$[124] = columnDefs;
		$[125] = groupData;
		$[126] = isLoading;
		$[127] = other;
		$[128] = t32;
		$[129] = t33;
		$[130] = t35;
		$[131] = t36;
	} else t36 = $[131];
	let t37;
	if ($[132] !== SideOptionList || $[133] !== t36) {
		t37 = /* @__PURE__ */ React.createElement(React.Fragment, null, t36, SideOptionList);
		$[132] = SideOptionList;
		$[133] = t36;
		$[134] = t37;
	} else t37 = $[134];
	return t37;
};
function _temp() {
	return console.warn("onSubmit not set");
}
function _temp2() {
	return _temp;
}
function _temp3(item) {
	return {
		groupItem: item.field,
		label: item.headerName,
		value: item.field
	};
}
function _temp4(fields, element) {
	if (element?.type === "datetime") fields.push(element?.name);
	return fields;
}
//#endregion
export { VirtualBrowser, VirtualBrowser as default };

//# sourceMappingURL=VirtualBrowser.mjs.map