import { getPathParametersObj } from "../../utilities/maintenanceEngine/CrudBrowserUtilities.mjs";
import { ConfigService } from "../../configService/index.mjs";
import { NodeActionEnums, SplitScreenTypeEnums } from "../../enums/unitySystemEnums.mjs";
import { apiMutate } from "../../utilities/useAxiosMutate.mjs";
import { getSuccessAction } from "../../UI/SuccessAction.mjs";
import { useActionStore } from "../../stores/actionStore.mjs";
import { useInquiryStore } from "../../stores/inquiryStore.mjs";
import { c } from "react-compiler-runtime";
import { useSnackbar } from "notistack";
import { useHistory } from "react-router-dom";
import { useTranslation } from "react-i18next";
//#region src/actions/utils/actionUtils.js
function isValidURL(url) {
	return /^(ftp|http|https):\/\/[^ "]+$/.test(url);
}
const useHandleAction = () => {
	const $ = c(10);
	const history = useHistory();
	const { t } = useTranslation();
	const { enqueueSnackbar, closeSnackbar } = useSnackbar();
	let t0;
	if ($[0] !== closeSnackbar) {
		t0 = getSuccessAction(closeSnackbar);
		$[0] = closeSnackbar;
		$[1] = t0;
	} else t0 = $[1];
	const successAction = t0;
	const selectedNode = useActionStore(_temp);
	const setWorkflowMap = useInquiryStore(_temp2);
	const setWorkflowStarting = useInquiryStore(_temp3);
	const executeStartWorkflow = _temp4;
	const getApiMethod = _temp5;
	let t1;
	if ($[2] !== enqueueSnackbar || $[3] !== history || $[4] !== selectedNode || $[5] !== setWorkflowMap || $[6] !== setWorkflowStarting || $[7] !== successAction || $[8] !== t) {
		const handleAction = async (dispatch, action, state_2, executeAction, apiCatalogId, entityAttributes, actionData, currentContainerId) => {
			if (!action) return;
			const actionName = action.name;
			let actionValue = action.value;
			if (action?.parameters?.entityName) actionValue = actionValue + ("?entityName=" + action.parameters.entityName + "&apiCatalogId=" + apiCatalogId);
			bb24: switch (action.actionTypeId) {
				case NodeActionEnums.Open_External_Link:
					window.open(actionValue, "_blank");
					break bb24;
				case NodeActionEnums.Redirect:
					if (isValidURL(actionValue)) window.location.href = actionValue;
					else history.push(actionValue);
					break bb24;
				case NodeActionEnums.Create:
					if (dispatch) dispatch({
						type: "OPEN_SIDESHEET",
						sideSheet: {
							action,
							open: true
						}
					});
					break bb24;
				case NodeActionEnums.Update:
					if (dispatch) dispatch({
						type: "OPEN_SIDESHEET",
						sideSheet: {
							action,
							open: true
						}
					});
					break bb24;
				case NodeActionEnums.Delete:
					if (dispatch) dispatch({
						type: "OPEN_MODAL",
						modal: {
							action,
							open: true,
							title: "Delete "
						}
					});
					break bb24;
				case NodeActionEnums.Retrieve:
					if (dispatch) if (currentContainerId) break bb24;
					else dispatch({
						type: "OPEN_SPLIT_SCREEN",
						splitScreen: {
							action,
							open: true,
							type: SplitScreenTypeEnums.Entity
						}
					});
					break bb24;
				case NodeActionEnums.Open_Page_Split_Screen:
					if (dispatch) if (!!selectedNode) dispatch({
						type: "OPEN_PAGE_CONTAINER_SPLIT_SCREEN",
						pageContainerSplitScreen: {
							action,
							open: true,
							type: SplitScreenTypeEnums.Page
						}
					});
					else dispatch({
						type: "OPEN_PAGE_SPLIT_SCREEN",
						pageSplitScreen: {
							action,
							open: true,
							type: SplitScreenTypeEnums.Page
						}
					});
					break bb24;
				case NodeActionEnums.Open_Shell_Split_Screen:
					if (dispatch) if (currentContainerId) break bb24;
					else dispatch({
						type: "OPEN_SPLIT_SCREEN",
						splitScreen: {
							action,
							open: true,
							type: SplitScreenTypeEnums.Page
						}
					});
					break bb24;
				case NodeActionEnums.Open_Notes:
					if (dispatch) if (currentContainerId) break bb24;
					else dispatch({
						type: "OPEN_SPLIT_SCREEN",
						splitScreen: {
							action,
							open: true,
							type: SplitScreenTypeEnums.Notes
						}
					});
					break bb24;
				case NodeActionEnums.Open_Reminders:
					if (dispatch) if (currentContainerId) break bb24;
					else dispatch({
						type: "OPEN_SPLIT_SCREEN",
						splitScreen: {
							action,
							open: true,
							type: SplitScreenTypeEnums.Reminders
						}
					});
					break bb24;
				case NodeActionEnums.Execute_Api:
					if (executeAction && action?.value) {
						let data = {};
						if (entityAttributes.length > 0 && (selectedNode?.nodeObj || actionData)) {
							if (actionData) data = { ...actionData };
							else data = { ...selectedNode?.nodeObj };
							entityAttributes.filter(_temp6).forEach((att) => {
								if (att.name in data || "serializeFields" in data) delete data[att.name];
							});
						}
						const pathParamValues = getPathParametersObj((await getApiMethod(action?.value))?.data?.pathParametersDefinition, data);
						executeAction({ data: {
							requestBody: data,
							...pathParamValues
						} }, action.value).then((res) => {
							enqueueSnackbar(t(`${action?.name} api has been executed successfully.`), {
								anchorOrigin: {
									vertical: "top",
									horizontal: "center"
								},
								action: successAction
							});
						}).catch((res_0) => {
							enqueueSnackbar(res_0.message, {
								variant: "error",
								anchorOrigin: {
									vertical: "top",
									horizontal: "center"
								}
							});
						});
					} else enqueueSnackbar(t(`Unable to execute ${actionName} action.`), {
						variant: "error",
						anchorOrigin: {
							vertical: "top",
							horizontal: "center"
						}
					});
					break bb24;
				case NodeActionEnums.Start_Workflow: {
					setWorkflowStarting(true);
					const response = await executeStartWorkflow(actionValue, actionData || {}).catch((e) => {
						enqueueSnackbar(t("Unable to Start Workflow"), {
							variant: "error",
							anchorOrigin: {
								vertical: "top",
								horizontal: "center"
							}
						});
					});
					setWorkflowStarting(false);
					if (response.status === 200) {
						const playId = response.data;
						if (selectedNode) {
							const workflow = {
								inWorkflowMode: true,
								currentWorkflowPlayId: playId
							};
							setWorkflowMap({ [currentContainerId || 0]: workflow });
						} else history.push({
							pathname: `/workflowtask/${playId}?taskId=`,
							state: { label: t("Workflow") }
						});
					}
					break bb24;
				}
				case NodeActionEnums.Execute_Function:
					actionData?.execute && actionData.execute();
					break bb24;
				default:
			}
		};
		t1 = { handleAction };
		$[2] = enqueueSnackbar;
		$[3] = history;
		$[4] = selectedNode;
		$[5] = setWorkflowMap;
		$[6] = setWorkflowStarting;
		$[7] = successAction;
		$[8] = t;
		$[9] = t1;
	} else t1 = $[9];
	return t1;
};
function _temp(state) {
	return state.selectedNode;
}
function _temp2(state_0) {
	return state_0.setWorkflowMap;
}
function _temp3(state_1) {
	return state_1.setWorkflowStarting;
}
async function _temp4(workflowId, payload) {
	return apiMutate(ConfigService.builderV1ApiUrl, `workflow/${workflowId}/start`, { method: "post" }, { data: payload });
}
async function _temp5(apiMethodId) {
	return apiMutate(ConfigService.integrationV1ApiUrl, `apimethod/${apiMethodId}`, { method: "get" });
}
function _temp6(p) {
	return p?.extendedProperties?.childEntity;
}
//#endregion
export { useHandleAction };

//# sourceMappingURL=actionUtils.mjs.map