import { ConfigService } from "../../../configService/index.mjs";
import { useAxiosMutate } from "../../../utilities/useAxiosMutate.mjs";
import { getSanitizedName } from "../../../utilities/entities/entityUtilities.mjs";
import { useAxiosGet } from "../../../utilities/useAxiosGet.mjs";
import { PageLoading } from "../../../UI/feedback/PageLoading.mjs";
import { useAuthedUser } from "../../../utilities/auth/useAuthedUser.mjs";
import { ClientSideUdpGrid } from "../../../UI/templates/newGrid/ClientSideUdpGrid.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { useHistory } from "react-router-dom";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpMaintenanceConfigPage/UdpMaintenanceConfigPage.jsx
const UdpMaintenanceConfigPage = (props) => {
	const $ = c(51);
	const [apiCatalogId, setApiCatalogId] = useState(null);
	const user = useAuthedUser();
	const history = useHistory();
	const { t } = useTranslation();
	let t0;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t0 = {};
		$[0] = t0;
	} else t0 = $[0];
	const [t1] = useAxiosGet(ConfigService.integrationV1ApiUrl, `tenantProductApiCatalogBase/tenant/${user?.currentTenantId}/product/${ConfigService.config.UNITY_PRODUCT_ID}`, t0, !!!user, false);
	const { data: tpacb, loading: loadingTpacb, error: tpacbError } = t1;
	let t2;
	if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = {};
		$[1] = t2;
	} else t2 = $[1];
	const [t3] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${apiCatalogId}/promotedConfigEntity/product/${ConfigService.config.UNITY_PRODUCT_ID}`, t2, !!!apiCatalogId, false);
	const { data: promotedGetConfigApi, loading: loadingPromotedGetConfigApi, error: promotedGetConfigApiError } = t3;
	let t4;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t4 = { method: "post" };
		$[2] = t4;
	} else t4 = $[2];
	const [t5, fetchTableConfigList] = useAxiosMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${promotedGetConfigApi?.apiMethodId}`, t4);
	const { data: tableConfigList, loading: tableConfigListLoading, error: tableConfigListError } = t5;
	let t6;
	if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
		t6 = {};
		$[3] = t6;
	} else t6 = $[3];
	const [t7] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${apiCatalogId}/promotedMethodEntities?productId=${ConfigService.config.UNITY_PRODUCT_ID}`, t6, !!!apiCatalogId, false);
	const { data: promotedMethods } = t7;
	let t8;
	bb0: {
		if (!promotedMethods || !tableConfigList) {
			let t9;
			if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
				t9 = [];
				$[4] = t9;
			} else t9 = $[4];
			t8 = t9;
			break bb0;
		}
		let t9;
		if ($[5] !== promotedMethods || $[6] !== tableConfigList) {
			let tmp = [];
			if (Array.isArray(tableConfigList)) tmp = tableConfigList;
			else {
				let t10;
				if ($[8] !== tableConfigList) {
					t10 = Object.entries(tableConfigList);
					$[8] = tableConfigList;
					$[9] = t10;
				} else t10 = $[9];
				for (const [key, value] of t10) tmp.push(`${key}`);
			}
			let t10;
			if ($[10] !== promotedMethods) {
				t10 = (mergedData, tableConfig) => {
					const matchingEntityRow = promotedMethods.find((promotedEntity) => promotedEntity.entityName === getSanitizedName(tableConfig));
					if (matchingEntityRow) mergedData.push(matchingEntityRow);
					else {
						const placeholderData = {
							entityName: tableConfig,
							productId: ConfigService.config.UNITY_PRODUCT_ID,
							unityBaseGetAllMethod: null,
							unityBaseGetMethod: null,
							unityBaseCreateMethod: null,
							unityBaseUpdateMethod: null,
							unityBaseDeleteMethod: null
						};
						mergedData.push(placeholderData);
					}
					return mergedData;
				};
				$[10] = promotedMethods;
				$[11] = t10;
			} else t10 = $[11];
			t9 = tmp.reduce(t10, []);
			$[5] = promotedMethods;
			$[6] = tableConfigList;
			$[7] = t9;
		} else t9 = $[7];
		t8 = t9;
	}
	const rowData = t8;
	let t9;
	if ($[12] !== apiCatalogId || $[13] !== history) {
		t9 = (selectedEntity) => {
			history.push("/crudbrowser", {
				entityName: getSanitizedName(selectedEntity?.entityName),
				apiCatalogId,
				from: "tablebrowser"
			});
		};
		$[12] = apiCatalogId;
		$[13] = history;
		$[14] = t9;
	} else t9 = $[14];
	const handleOpenEntityMaintenancePage = t9;
	let t10;
	if ($[15] !== t) {
		t10 = t("View table data");
		$[15] = t;
		$[16] = t10;
	} else t10 = $[16];
	let t11;
	if ($[17] !== handleOpenEntityMaintenancePage) {
		t11 = (e, params) => handleOpenEntityMaintenancePage(params?.data);
		$[17] = handleOpenEntityMaintenancePage;
		$[18] = t11;
	} else t11 = $[18];
	let t12;
	if ($[19] !== t10 || $[20] !== t11) {
		t12 = [{
			id: 0,
			title: t10,
			iconName: "arrowRight16",
			handleOnClick: t11,
			disabled: false
		}];
		$[19] = t10;
		$[20] = t11;
		$[21] = t12;
	} else t12 = $[21];
	const actionList = t12;
	let t13;
	if ($[22] === Symbol.for("react.memo_cache_sentinel")) {
		t13 = {
			headerName: "Entity Name",
			field: "entityName",
			valueGetter: _temp
		};
		$[22] = t13;
	} else t13 = $[22];
	let t14;
	if ($[23] !== actionList) {
		t14 = [t13, {
			headerName: "Actions",
			field: "actions",
			cellRenderer: "actionsRenderer",
			cellRendererParams: { actionList }
		}];
		$[23] = actionList;
		$[24] = t14;
	} else t14 = $[24];
	const columnDefs = t14;
	let t15;
	let t16;
	if ($[25] !== tpacb) {
		t15 = () => {
			if (tpacb) setApiCatalogId(tpacb.apiCatalogId);
		};
		t16 = [tpacb];
		$[25] = tpacb;
		$[26] = t15;
		$[27] = t16;
	} else {
		t15 = $[26];
		t16 = $[27];
	}
	useEffect(t15, t16);
	let t17;
	if ($[28] !== fetchTableConfigList || $[29] !== promotedGetConfigApi?.apiMethodId) {
		t17 = () => {
			if (promotedGetConfigApi?.apiMethodId) fetchTableConfigList({ data: {} });
		};
		$[28] = fetchTableConfigList;
		$[29] = promotedGetConfigApi?.apiMethodId;
		$[30] = t17;
	} else t17 = $[30];
	let t18;
	if ($[31] !== fetchTableConfigList || $[32] !== promotedGetConfigApi) {
		t18 = [fetchTableConfigList, promotedGetConfigApi];
		$[31] = fetchTableConfigList;
		$[32] = promotedGetConfigApi;
		$[33] = t18;
	} else t18 = $[33];
	useEffect(t17, t18);
	if (loadingTpacb || loadingPromotedGetConfigApi || tableConfigListLoading) {
		let t19;
		if ($[34] === Symbol.for("react.memo_cache_sentinel")) {
			t19 = /* @__PURE__ */ React.createElement(PageLoading, null);
			$[34] = t19;
		} else t19 = $[34];
		return t19;
	}
	if (tpacbError) {
		let t19;
		if ($[35] !== t) {
			t19 = t("There is no Api Catalog associated with your tenant and product");
			$[35] = t;
			$[36] = t19;
		} else t19 = $[36];
		let t20;
		if ($[37] !== t19) {
			t20 = /* @__PURE__ */ React.createElement("div", null, t19);
			$[37] = t19;
			$[38] = t20;
		} else t20 = $[38];
		return t20;
	}
	if (promotedGetConfigApiError) {
		let t19;
		if ($[39] !== t) {
			t19 = t("Please promote your get configuration tables method before proceeding");
			$[39] = t;
			$[40] = t19;
		} else t19 = $[40];
		let t20;
		if ($[41] !== t19) {
			t20 = /* @__PURE__ */ React.createElement("div", null, t19);
			$[41] = t19;
			$[42] = t20;
		} else t20 = $[42];
		return t20;
	}
	if (tableConfigListError) {
		let t19;
		if ($[43] !== t) {
			t19 = t("No configuration tables could be found");
			$[43] = t;
			$[44] = t19;
		} else t19 = $[44];
		let t20;
		if ($[45] !== t19) {
			t20 = /* @__PURE__ */ React.createElement("div", null, t19);
			$[45] = t19;
			$[46] = t20;
		} else t20 = $[46];
		return t20;
	}
	let t19;
	if ($[47] === Symbol.for("react.memo_cache_sentinel")) {
		t19 = { title: "Table Browser" };
		$[47] = t19;
	} else t19 = $[47];
	let t20;
	if ($[48] !== columnDefs || $[49] !== rowData) {
		t20 = /* @__PURE__ */ React.createElement(ClientSideUdpGrid, {
			rowData,
			columnDefs,
			gridHeaderConfig: t19
		});
		$[48] = columnDefs;
		$[49] = rowData;
		$[50] = t20;
	} else t20 = $[50];
	return t20;
};
function _temp(params_0) {
	return params_0.data?.properties?.displayName || params_0.data?.sanitizedName || getSanitizedName(params_0.data?.entityName);
}
//#endregion
export { UdpMaintenanceConfigPage as default };

//# sourceMappingURL=UdpMaintenanceConfigPage.mjs.map