import { ConfigService } from "../../../configService/index.mjs";
import { useCatalogStore } from "../../../stores/catalogStore.mjs";
import { Typography as Typography$1 } from "../../../UI/dataDisplay/typography/Typography.mjs";
import { useAxiosGet } from "../../../utilities/useAxiosGet.mjs";
import { useTransactionEngine } from "../../../utilities/transactions/useTransactionEngine.mjs";
import UdpTransactionGrid from "./UdpTransactionGrid.mjs";
import UdpTransactionCalendar from "./UdpTransactionCalendar.mjs";
import UdpTransactionSidesheet from "./UdpTransactionSidesheet.mjs";
import { useTransactionType } from "../../../hooks/useTransactionType.mjs";
import { useUdpGridFilterModel } from "../../../hooks/useUdpGridFilterModel.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { CircularProgress } from "@material-ui/core";
import { useLocation } from "react-router-dom";
import "./UdpTransactionsPage.css";
//#region src/udp/pages/UdpTransactionsPage/UdpTransactionsPage.jsx
const UdpTransactionsPage = (props) => {
	const $ = c(66);
	const { transactionTypeId: t0, domainName: t1, domainFieldType, domainFieldValue, searchMethodObject, domainColumnFormatters, gridOptions, onBeforeRefresh, onAfterRefresh, gridTitle, hideCreateTransaction: t2, sideSheetProps, columnDefProperties, paginationAutoPageSize, customActions, gridApiCallback, loading: t3 } = props;
	const transactionTypeId = t0 === void 0 ? "" : t0;
	const initialDomainName = t1 === void 0 ? "" : t1;
	const hideCreateTransaction = t2 === void 0 ? false : t2;
	const loading = t3 === void 0 ? false : t3;
	let t4;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t4 = {
			workflowAction: false,
			addTransaction: false,
			editTransaction: false,
			viewTransaction: false,
			attachmentTransaction: false,
			createRevision: false
		};
		$[0] = t4;
	} else t4 = $[0];
	const [sidesheetState, setSidesheetState] = useState(t4);
	const [selectedTransaction, setSelectedTransaction] = useState(null);
	const [gridApi, setGridApi] = useState(null);
	const [calendarRefresh, setCalendarRefresh] = useState(null);
	const [showCalendarView, setShowCalendarView] = useState(false);
	const location = useLocation();
	const state = location.state;
	const searchParamStr = location.search;
	let t5;
	if ($[1] !== searchParamStr || $[2] !== state) {
		const searchParams = new URLSearchParams(searchParamStr);
		t5 = state?.params?.udpTransactionTypeId ? state.params.udpTransactionTypeId : searchParams.has("udpTransactionTypeId") === true ? searchParams.get("udpTransactionTypeId") : "";
		$[1] = searchParamStr;
		$[2] = state;
		$[3] = t5;
	} else t5 = $[3];
	const initialTransactionTypeId = t5;
	const t6 = transactionTypeId || initialTransactionTypeId;
	let t7;
	if ($[4] !== domainFieldType || $[5] !== domainFieldValue || $[6] !== initialDomainName || $[7] !== t6) {
		t7 = {
			transactionTypeId: t6,
			domainName: initialDomainName,
			domainFieldType,
			domainFieldValue
		};
		$[4] = domainFieldType;
		$[5] = domainFieldValue;
		$[6] = initialDomainName;
		$[7] = t6;
		$[8] = t7;
	} else t7 = $[8];
	const { data: transactionType, loading: transactionTypeLoading } = useTransactionType(t7);
	useTransactionEngine(transactionType?.transactionTypeId);
	const currentApiCatalogId = useCatalogStore(_temp);
	const filterModel = useUdpGridFilterModel();
	let t8;
	if ($[9] === Symbol.for("react.memo_cache_sentinel")) {
		t8 = (actionType, openStatus, transaction, t9) => {
			const width = t9 === void 0 ? 800 : t9;
			if (actionType == "add") setSidesheetState((prev) => ({
				...prev,
				addTransaction: openStatus,
				width
			}));
			else if (actionType == "edit") setSidesheetState((prev_0) => ({
				...prev_0,
				editTransaction: openStatus,
				width
			}));
			else if (actionType == "view") setSidesheetState((prev_1) => ({
				...prev_1,
				viewTransaction: openStatus,
				width
			}));
			else if (actionType == "attachment") setSidesheetState((prev_2) => ({
				...prev_2,
				attachmentTransaction: openStatus,
				width
			}));
			else if (actionType == "action") setSidesheetState((prev_3) => ({
				...prev_3,
				workflowAction: openStatus,
				width
			}));
			else if (actionType == "createRevision") setSidesheetState((prev_4) => ({
				...prev_4,
				createRevision: openStatus,
				width
			}));
			if (transaction) setSelectedTransaction(transaction);
		};
		$[9] = t8;
	} else t8 = $[9];
	const handleSidesheet = t8;
	const handleAttachmentTransaction = _temp2;
	let t9;
	if ($[10] !== gridApiCallback) {
		t9 = (api) => {
			setGridApi(api);
			gridApiCallback?.(api);
		};
		$[10] = gridApiCallback;
		$[11] = t9;
	} else t9 = $[11];
	const handleGridApiCallback = t9;
	let t10;
	if ($[12] === Symbol.for("react.memo_cache_sentinel")) {
		t10 = (refreshFn) => {
			setCalendarRefresh(() => refreshFn);
		};
		$[12] = t10;
	} else t10 = $[12];
	const handleCalendarApiCallback = t10;
	let t11;
	bb0: {
		if (transactionType) {
			let t12;
			if ($[13] !== transactionType.domainName) {
				t12 = transactionType.domainName.split(".").pop();
				$[13] = transactionType.domainName;
				$[14] = t12;
			} else t12 = $[14];
			t11 = t12;
			break bb0;
		}
		t11 = initialDomainName;
	}
	const domainName = t11;
	let t12;
	bb1: {
		const config = transactionType?.pageConfiguration?.calendarViewConfiguration;
		if (!config) {
			t12 = null;
			break bb1;
		}
		const t13 = !!config.isDefaultView;
		let t14;
		if ($[15] !== config.defaultCalendarViewType || $[16] !== config.domainEndDateProperty || $[17] !== config.domainPropertyToDisplay || $[18] !== config.domainStartDateProperty || $[19] !== config.enabled || $[20] !== t13) {
			t14 = {
				enabled: config.enabled,
				isDefaultView: t13,
				domainStartDateProperty: config.domainStartDateProperty,
				domainEndDateProperty: config.domainEndDateProperty,
				domainPropertyToDisplay: config.domainPropertyToDisplay,
				defaultCalendarViewType: config.defaultCalendarViewType
			};
			$[15] = config.defaultCalendarViewType;
			$[16] = config.domainEndDateProperty;
			$[17] = config.domainPropertyToDisplay;
			$[18] = config.domainStartDateProperty;
			$[19] = config.enabled;
			$[20] = t13;
			$[21] = t14;
		} else t14 = $[21];
		t12 = t14;
	}
	const calendarViewConfiguration = t12;
	const calendarViewEnabled = !!calendarViewConfiguration?.enabled;
	let t13;
	if ($[22] !== calendarViewConfiguration?.isDefaultView) {
		t13 = () => {
			if (calendarViewConfiguration?.isDefaultView) setShowCalendarView(true);
		};
		$[22] = calendarViewConfiguration?.isDefaultView;
		$[23] = t13;
	} else t13 = $[23];
	const t14 = calendarViewConfiguration?.isDefaultView;
	let t15;
	if ($[24] !== t14) {
		t15 = [t14];
		$[24] = t14;
		$[25] = t15;
	} else t15 = $[25];
	useEffect(t13, t15);
	let t16;
	if ($[26] === Symbol.for("react.memo_cache_sentinel")) {
		t16 = {};
		$[26] = t16;
	} else t16 = $[26];
	const [t17] = useAxiosGet(ConfigService.integrationV2ApiUrl, `apiCatalog/${currentApiCatalogId}/catalogObject/${transactionType?.domainName}`, t16, !!!currentApiCatalogId || !!!transactionType?.domainName, false);
	const { data: catalogObject, loading: catalogObjectLoading } = t17;
	let t18;
	if ($[27] !== calendarRefresh || $[28] !== gridApi || $[29] !== onAfterRefresh || $[30] !== onBeforeRefresh || $[31] !== showCalendarView) {
		t18 = async () => {
			if (onBeforeRefresh) await onBeforeRefresh(gridApi);
			if (showCalendarView) await calendarRefresh?.();
			else gridApi?.refreshServerSide();
			if (onAfterRefresh) await onAfterRefresh(gridApi);
		};
		$[27] = calendarRefresh;
		$[28] = gridApi;
		$[29] = onAfterRefresh;
		$[30] = onBeforeRefresh;
		$[31] = showCalendarView;
		$[32] = t18;
	} else t18 = $[32];
	const refreshGridData = t18;
	if (!transactionTypeId && !initialTransactionTypeId && !initialDomainName) {
		let t19;
		if ($[33] === Symbol.for("react.memo_cache_sentinel")) {
			t19 = /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Typography$1, { variant: "h6" }, "Error loading Transaction Type: No Transaction type provided."));
			$[33] = t19;
		} else t19 = $[33];
		return t19;
	}
	if (!transactionType || catalogObjectLoading || loading) {
		let t19;
		if ($[34] === Symbol.for("react.memo_cache_sentinel")) {
			t19 = /* @__PURE__ */ React.createElement("div", { className: "udp-transaction-page-loading" }, /* @__PURE__ */ React.createElement(CircularProgress, { size: 64 }));
			$[34] = t19;
		} else t19 = $[34];
		return t19;
	}
	let t19;
	if ($[35] !== calendarViewConfiguration || $[36] !== calendarViewEnabled || $[37] !== catalogObject || $[38] !== columnDefProperties || $[39] !== customActions || $[40] !== domainColumnFormatters || $[41] !== domainName || $[42] !== filterModel || $[43] !== gridApi || $[44] !== gridOptions || $[45] !== gridTitle || $[46] !== handleGridApiCallback || $[47] !== hideCreateTransaction || $[48] !== paginationAutoPageSize || $[49] !== refreshGridData || $[50] !== searchMethodObject || $[51] !== showCalendarView || $[52] !== transactionType || $[53] !== transactionTypeLoading) {
		t19 = calendarViewEnabled && showCalendarView ? /* @__PURE__ */ React.createElement(UdpTransactionCalendar, {
			transactionTypeId: transactionType?.transactionTypeId,
			domainName,
			searchMethodObject,
			calendarViewConfiguration,
			openWorkflowActionTransaction: (transaction_0) => {
				handleSidesheet("action", true, transaction_0);
			},
			handleCalendarApiCallback,
			openAddTransaction: () => {
				handleSidesheet("add", true);
			},
			gridTitle,
			hideCreateTransaction,
			onShowGridView: () => setShowCalendarView(false)
		}) : /* @__PURE__ */ React.createElement(UdpTransactionGrid, {
			transactionTypeId: transactionType?.transactionTypeId,
			transactionType,
			domainName,
			transactionTypeLoading,
			searchMethodObject,
			domainColumnFormatters,
			catalogObject,
			openAddTransaction: () => {
				handleSidesheet("add", true);
			},
			openViewTransaction: (transaction_1) => {
				handleSidesheet("view", true, transaction_1);
			},
			openEditTransaction: (transaction_2) => {
				handleSidesheet("edit", true, transaction_2);
			},
			openAttachmentTransaction: (transaction_3) => {
				handleSidesheet("attachment", true, transaction_3, 1e3);
			},
			openWorkflowActionTransaction: (transaction_4) => {
				handleSidesheet("action", true, transaction_4);
			},
			openCreateRevision: (transaction_5) => {
				handleSidesheet("createRevision", true, transaction_5);
			},
			handleGridApiCallback,
			refreshGridData,
			gridApi,
			initialFilterModel: filterModel,
			initialGridOptions: gridOptions,
			gridTitle,
			hideCreateTransaction,
			columnDefProperties,
			customActions,
			paginationAutoPageSize,
			calendarViewEnabled,
			onShowCalendarView: () => setShowCalendarView(true)
		});
		$[35] = calendarViewConfiguration;
		$[36] = calendarViewEnabled;
		$[37] = catalogObject;
		$[38] = columnDefProperties;
		$[39] = customActions;
		$[40] = domainColumnFormatters;
		$[41] = domainName;
		$[42] = filterModel;
		$[43] = gridApi;
		$[44] = gridOptions;
		$[45] = gridTitle;
		$[46] = handleGridApiCallback;
		$[47] = hideCreateTransaction;
		$[48] = paginationAutoPageSize;
		$[49] = refreshGridData;
		$[50] = searchMethodObject;
		$[51] = showCalendarView;
		$[52] = transactionType;
		$[53] = transactionTypeLoading;
		$[54] = t19;
	} else t19 = $[54];
	const t20 = transactionType?.transactionTypeId;
	let t21;
	if ($[55] !== catalogObject || $[56] !== domainName || $[57] !== refreshGridData || $[58] !== selectedTransaction || $[59] !== sideSheetProps || $[60] !== sidesheetState || $[61] !== t20) {
		t21 = /* @__PURE__ */ React.createElement(UdpTransactionSidesheet, {
			openStates: sidesheetState,
			handleSidesheet,
			handleAttachmentTransaction,
			transaction: selectedTransaction,
			transactionTypeId: t20,
			domainName,
			refreshGridData,
			catalogObject,
			...sideSheetProps
		});
		$[55] = catalogObject;
		$[56] = domainName;
		$[57] = refreshGridData;
		$[58] = selectedTransaction;
		$[59] = sideSheetProps;
		$[60] = sidesheetState;
		$[61] = t20;
		$[62] = t21;
	} else t21 = $[62];
	let t22;
	if ($[63] !== t19 || $[64] !== t21) {
		t22 = /* @__PURE__ */ React.createElement(React.Fragment, null, t19, t21);
		$[63] = t19;
		$[64] = t21;
		$[65] = t22;
	} else t22 = $[65];
	return t22;
};
function _temp(state_0) {
	return state_0.currentApiCatalogId;
}
function _temp2() {}
//#endregion
export { UdpTransactionsPage as default };

//# sourceMappingURL=UdpTransactionsPage.mjs.map