import { ConfigService } from "../../configService/index.mjs";
import { useHandleAxiosSnackbar } from "../../utilities/apiHelpers.mjs";
import { useUser } from "../../utilities/auth/useUser.mjs";
import { getSanitizedName } from "../../utilities/entities/entityUtilities.mjs";
import { useAxiosGet } from "../../utilities/useAxiosGet.mjs";
import { executeQueryWithParameters } from "../utilities/methodUtils.mjs";
import { FluentDialog } from "../../UI/feedback/FluentDialog.mjs";
import { ActionsRenderer } from "../../UI/templates/ui/renderers/ActionsRenderer.mjs";
import { TableContainer } from "../../maintenanceEngine/tableBrowser/TableContainer.mjs";
import { AmbientAutoComplete } from "../../UI/inputs/textField/AmbientAutoComplete.mjs";
import { AmbientAlert } from "../../UI/feedback/AmbientAlert.mjs";
import { EmptyStateDisplay } from "../../UI/dataDisplay/EmptyStateDisplay.mjs";
import { StatusCellRenderer } from "../../UI/templates/ui/renderers/StatusCellRenderer.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { DownloadIcon, ErrorIcon, StatusCircleCheckmarkIcon } from "@fluentui/react-icons";
//#region src/udp/export/MyExportsPage.jsx
const MyExportsPage = (props) => {
	const $ = c(56);
	const { productId } = props;
	const user = useUser();
	const [product, setProduct] = useState();
	const [downloadLinkLoading, setDownloadLinkLoading] = useState(false);
	const [fileResultData, setFileResultData] = useState();
	const [downloadOpen, setDownloadOpen] = useState(false);
	const [noDownloadMethod, setNoDownloadMethod] = useState(false);
	const { handleErrorSnackbar, handleSuccessSnackbar } = useHandleAxiosSnackbar();
	let t0;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t0 = {};
		$[0] = t0;
	} else t0 = $[0];
	const [t1] = useAxiosGet(ConfigService.tenantV2ApiUrl, `tenant/products?tenantId=${user?.currentTenantId}&isSubscribable=${!user?.can?.CAN_MANAGE_CATALOGS_FOR_UNSUBSCRIBABLE_PRODUCTS}`, t0, !user);
	const { data: products, loading: productsLoading } = t1;
	let t2;
	if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = {};
		$[1] = t2;
	} else t2 = $[1];
	const [t3] = useAxiosGet(ConfigService.integrationV1ApiUrl, `tenantProductApiCatalogBase/tenant/${user?.currentTenantId}/product/${productId ? productId : product?.productId}`, t2, !(product || productId), false);
	const { data: apiCatalog, loading: catalogLoading, error: catalogError } = t3;
	let t4;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t4 = {};
		$[2] = t4;
	} else t4 = $[2];
	const [t5] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${apiCatalog?.apiCatalogId}/promotedMethodEntities/udpJob/product/${apiCatalog?.productId}?compareSanitizedName=true`, t4, !apiCatalog?.productId || !apiCatalog?.apiCatalogId, false);
	const { data: udpJobEntity, loading: jobEntityLoading } = t5;
	let t6;
	if ($[3] !== udpJobEntity?.unityBaseGetFileResultMethod?.apiMethodId) {
		t6 = async (selectedRecord) => {
			if (udpJobEntity?.unityBaseGetFileResultMethod?.apiMethodId) {
				setDownloadLinkLoading(true);
				await executeQueryWithParameters({ data: { JobId: selectedRecord?.id } }, udpJobEntity?.unityBaseGetFileResultMethod?.apiMethodId).then((res) => {
					setFileResultData(res.data);
				}).catch(_temp);
				setDownloadLinkLoading(false);
			} else setNoDownloadMethod(true);
		};
		$[3] = udpJobEntity?.unityBaseGetFileResultMethod?.apiMethodId;
		$[4] = t6;
	} else t6 = $[4];
	udpJobEntity?.unityBaseGetFileResultMethod?.apiMethodId;
	const refetchfileResultData = t6;
	let t7;
	if ($[5] !== refetchfileResultData) {
		t7 = (data) => {
			refetchfileResultData(data);
			setDownloadOpen(true);
		};
		$[5] = refetchfileResultData;
		$[6] = t7;
	} else t7 = $[6];
	const handleSelectRecord = t7;
	const getOptionLabel = _temp2;
	let t8;
	if ($[7] === Symbol.for("react.memo_cache_sentinel")) {
		t8 = (val_0) => {
			setProduct(val_0);
		};
		$[7] = t8;
	} else t8 = $[7];
	const handleProductChange = t8;
	let t9;
	if ($[8] !== fileResultData?.link || $[9] !== handleSuccessSnackbar) {
		t9 = () => {
			if (fileResultData?.link) {
				const link = document.createElement("a");
				link.href = fileResultData?.link;
				link.download = "";
				document.body.appendChild(link);
				link.click();
				document.body.removeChild(link);
				setDownloadOpen(false);
				handleSuccessSnackbar("File successfully downloaded");
			}
		};
		$[8] = fileResultData?.link;
		$[9] = handleSuccessSnackbar;
		$[10] = t9;
	} else t9 = $[10];
	fileResultData?.link;
	const handleFileDownload = t9;
	const getJobStatus = _temp3;
	const getJobLabel = _temp4;
	let t10;
	let t11;
	if ($[11] !== catalogError || $[12] !== handleErrorSnackbar) {
		t10 = () => {
			if (catalogError) handleErrorSnackbar("", "Error fetching catalog for the selected product");
		};
		t11 = [catalogError, handleErrorSnackbar];
		$[11] = catalogError;
		$[12] = handleErrorSnackbar;
		$[13] = t10;
		$[14] = t11;
	} else {
		t10 = $[13];
		t11 = $[14];
	}
	useEffect(t10, t11);
	let t12;
	if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
		t12 = [{
			searchField: "jobTypeId",
			searchOperator: "=",
			searchValue: 2
		}];
		$[15] = t12;
	} else t12 = $[15];
	const filterElements = t12;
	let t13;
	if ($[16] !== handleSelectRecord) {
		t13 = [{
			id: 1,
			title: "Download",
			icon: DownloadIcon,
			onClick: handleSelectRecord,
			disabled: false,
			checkDisabled: _temp5
		}];
		$[16] = handleSelectRecord;
		$[17] = t13;
	} else t13 = $[17];
	const actionList = t13;
	let t14;
	let t15;
	let t16;
	if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
		t14 = {
			headerName: "Id",
			sortable: true,
			field: "id",
			suppressMenu: true
		};
		t15 = {
			headerName: "Name",
			sortable: false,
			field: "name",
			minWidth: 140,
			suppressMenu: true,
			valueFormatter: _temp6
		};
		t16 = {
			headerName: "Date",
			sortable: false,
			valueFormatter: _temp7,
			minWidth: 130,
			suppressMenu: true
		};
		$[18] = t14;
		$[19] = t15;
		$[20] = t16;
	} else {
		t14 = $[18];
		t15 = $[19];
		t16 = $[20];
	}
	let t17;
	if ($[21] === Symbol.for("react.memo_cache_sentinel")) {
		t17 = {
			headerName: "Status",
			field: "statusId",
			wrapText: true,
			autoHeight: true,
			sortable: false,
			cellRenderer: "statusCellRenderer",
			cellRendererParams: {
				getStatus: getJobStatus,
				getLabel: getJobLabel
			},
			minWidth: 130,
			maxWidth: 130,
			suppressMenu: true
		};
		$[21] = t17;
	} else t17 = $[21];
	let t18;
	if ($[22] !== actionList) {
		t18 = {
			actionList,
			value: _temp8
		};
		$[22] = actionList;
		$[23] = t18;
	} else t18 = $[23];
	let t19;
	if ($[24] === Symbol.for("react.memo_cache_sentinel")) {
		t19 = {
			display: "flex",
			alignItems: "center"
		};
		$[24] = t19;
	} else t19 = $[24];
	let t20;
	if ($[25] !== t18) {
		t20 = [
			t14,
			t15,
			t16,
			t17,
			{
				headerName: "Actions",
				filter: false,
				pinned: "right",
				cellRenderer: "actionsRenderer",
				width: 120,
				maxWidth: 120,
				cellRendererParams: t18,
				suppressMenu: true,
				sortable: false,
				cellStyle: t19
			}
		];
		$[25] = t18;
		$[26] = t20;
	} else t20 = $[26];
	const columnDefs = t20;
	let t21;
	if ($[27] !== udpJobEntity) {
		t21 = udpJobEntity && !udpJobEntity?.unityBaseSearchMethod?.apiMethodId && /* @__PURE__ */ React.createElement(AmbientAlert, {
			alertMessage: "UdpJob's search method must be promoted",
			showAlert: true,
			alertSeverity: "error"
		});
		$[27] = udpJobEntity;
		$[28] = t21;
	} else t21 = $[28];
	let t22;
	if ($[29] !== product || $[30] !== productId || $[31] !== products) {
		t22 = !productId && /* @__PURE__ */ React.createElement("div", { style: { margin: "0px 5px 5px 5px" } }, /* @__PURE__ */ React.createElement(AmbientAutoComplete, {
			isMultiple: false,
			id: "product",
			name: "product",
			label: "Product",
			onChange: handleProductChange,
			options: products,
			size: "small",
			getOptionLabelFunction: getOptionLabel,
			value: product,
			required: true
		}));
		$[29] = product;
		$[30] = productId;
		$[31] = products;
		$[32] = t22;
	} else t22 = $[32];
	const t23 = productsLoading || catalogLoading || jobEntityLoading;
	let t24;
	if ($[33] === Symbol.for("react.memo_cache_sentinel")) {
		t24 = {
			statusCellRenderer: StatusCellRenderer,
			actionsRenderer: ActionsRenderer
		};
		$[33] = t24;
	} else t24 = $[33];
	let t25;
	if ($[34] === Symbol.for("react.memo_cache_sentinel")) {
		t25 = [
			"saveView",
			"views",
			"insights",
			"importExport",
			"group",
			"filter",
			"columns",
			"clearFilters",
			"fitGrid",
			"expandGrid"
		];
		$[34] = t25;
	} else t25 = $[34];
	let t26;
	if ($[35] !== actionList || $[36] !== columnDefs || $[37] !== t23 || $[38] !== udpJobEntity) {
		t26 = /* @__PURE__ */ React.createElement(TableContainer, {
			loading: t23,
			title: "Exports",
			isServerSide: true,
			eagerLoad: false,
			logicalSearchOperator: 2,
			promotedEntity: udpJobEntity,
			filterElements,
			actionList,
			customColumnDefs: columnDefs,
			frameworkComponents: t24,
			hideSearchBar: true,
			hiddenActionButtons: t25,
			height: "calc(100vh - 167px)",
			dynamicSizing: true
		});
		$[35] = actionList;
		$[36] = columnDefs;
		$[37] = t23;
		$[38] = udpJobEntity;
		$[39] = t26;
	} else t26 = $[39];
	let t27;
	if ($[40] === Symbol.for("react.memo_cache_sentinel")) {
		t27 = () => setDownloadOpen(false);
		$[40] = t27;
	} else t27 = $[40];
	let t28;
	if ($[41] === Symbol.for("react.memo_cache_sentinel")) {
		t28 = () => setDownloadOpen(false);
		$[41] = t28;
	} else t28 = $[41];
	const t29 = noDownloadMethod || downloadLinkLoading;
	let t30;
	if ($[42] !== downloadLinkLoading || $[43] !== noDownloadMethod) {
		t30 = noDownloadMethod ? /* @__PURE__ */ React.createElement(AmbientAlert, {
			alertMessage: "UdpJob's get file result method must be promoted",
			showAlert: true,
			alertSeverity: "error"
		}) : downloadLinkLoading ? /* @__PURE__ */ React.createElement(EmptyStateDisplay, { primaryText: "Fetching download link..." }) : /* @__PURE__ */ React.createElement(EmptyStateDisplay, {
			icon: noDownloadMethod ? ErrorIcon : StatusCircleCheckmarkIcon,
			primaryText: !noDownloadMethod && "File is ready to download!"
		});
		$[42] = downloadLinkLoading;
		$[43] = noDownloadMethod;
		$[44] = t30;
	} else t30 = $[44];
	let t31;
	if ($[45] !== downloadLinkLoading || $[46] !== downloadOpen || $[47] !== handleFileDownload || $[48] !== t29 || $[49] !== t30) {
		t31 = /* @__PURE__ */ React.createElement(FluentDialog, {
			actionOne: handleFileDownload,
			actionTwo: t27,
			labelOne: "Download",
			labelTwo: "Cancel",
			open: downloadOpen,
			handleClose: t28,
			disableOne: t29,
			progress: downloadLinkLoading,
			fullWidth: true
		}, t30);
		$[45] = downloadLinkLoading;
		$[46] = downloadOpen;
		$[47] = handleFileDownload;
		$[48] = t29;
		$[49] = t30;
		$[50] = t31;
	} else t31 = $[50];
	let t32;
	if ($[51] !== t21 || $[52] !== t22 || $[53] !== t26 || $[54] !== t31) {
		t32 = /* @__PURE__ */ React.createElement("div", null, t21, t22, t26, t31);
		$[51] = t21;
		$[52] = t22;
		$[53] = t26;
		$[54] = t31;
		$[55] = t32;
	} else t32 = $[55];
	return t32;
};
function _temp(err) {}
function _temp2(val) {
	return val?.name;
}
function _temp3(value) {
	if (value === 1) return null;
	else if (value === 2) return "info";
	else if (value === 3) return "success";
	else if (value === 4) return "error";
}
function _temp4(value_0) {
	if (value_0 === 1) return "Pending";
	else if (value_0 === 2) return "Running";
	else if (value_0 === 3) return "Completed";
	else if (value_0 === 4) return "Failed";
}
function _temp5(value_1) {
	return value_1?.statusId !== 3;
}
function _temp6(data_0) {
	return getSanitizedName(data_0.data.name);
}
function _temp7(data_1) {
	return new Date(data_1.data.startTime).toLocaleString();
}
function _temp8(data_2) {
	return data_2;
}
//#endregion
export { MyExportsPage, MyExportsPage as default };

//# sourceMappingURL=MyExportsPage.mjs.map