import { ConfigService } from "../../../../configService/index.mjs";
import { TransactionActionTypeEnums, TransactionEventTypeEnums } from "../../../../enums/unitySystemEnums.mjs";
import { useHandleAxiosSnackbar } from "../../../../utilities/apiHelpers.mjs";
import { useAxiosMutate } from "../../../../utilities/useAxiosMutate.mjs";
import { getIconFromName } from "../../../../utilities/tree/TreeUtilities.mjs";
import { SideSheet } from "../../../../UI/surfaces/SideSheet.mjs";
import { FluentDialog } from "../../../../UI/feedback/FluentDialog.mjs";
import { UdpActionCard } from "../../../../UI/inputs/buttons/UdpActionCard/UdpActionCard.mjs";
import { useTransactionEngine } from "../../../../utilities/transactions/useTransactionEngine.mjs";
import UdpTransactionCustomPage from "../components/UdpTransactionCustomPage.mjs";
import { c } from "react-compiler-runtime";
import React, { useCallback, useMemo, useRef, useState } from "react";
import { DocumentIcon } from "@fluentui/react-icons";
import { useTranslation } from "react-i18next";
import "./WorkflowActions.css";
//#region src/udp/pages/UdpTransactionsPage/sidesheets/WorkflowActions.jsx
const TransactionActionButton = (t0) => {
	const $ = c(10);
	const { icon, primaryText, secondaryText, onClick, action } = t0;
	let t1;
	if ($[0] !== icon) {
		t1 = getIconFromName(icon);
		$[0] = icon;
		$[1] = t1;
	} else t1 = $[1];
	const actionCardIcon = t1;
	const t2 = actionCardIcon ? actionCardIcon : DocumentIcon;
	let t3;
	if ($[2] !== action || $[3] !== onClick) {
		t3 = () => {
			onClick(action);
		};
		$[2] = action;
		$[3] = onClick;
		$[4] = t3;
	} else t3 = $[4];
	let t4;
	if ($[5] !== primaryText || $[6] !== secondaryText || $[7] !== t2 || $[8] !== t3) {
		t4 = /* @__PURE__ */ React.createElement(UdpActionCard, {
			icon: t2,
			primaryText,
			secondaryText,
			onClick: t3
		});
		$[5] = primaryText;
		$[6] = secondaryText;
		$[7] = t2;
		$[8] = t3;
		$[9] = t4;
	} else t4 = $[9];
	return t4;
};
const WorkflowActions = ({ transaction, closeSideSheet, refreshGridData, handleSidesheet }) => {
	const [dialogOpen, setDialogOpen] = useState(true);
	const [dialogUserStrings, setDialogUserStrings] = useState(null);
	const [selectedAction, setSelectedAction] = useState(null);
	const [sideSheetOpen, setSideSheetOpen] = useState(false);
	const [loading, setLoading] = useState(false);
	const [sidesheetPrimaryButtonDisabled, setSidesheetPrimaryButtonDisabled] = useState(false);
	const formRef = useRef(null);
	const { getTransactionTypeTransactionActions } = useTransactionEngine(transaction?.transactionTypeId);
	const { handleErrorSnackbar, handleSuccessSnackbar } = useHandleAxiosSnackbar();
	const handleOnCloseSideSheet = useCallback(() => {
		setSideSheetOpen(false);
	}, []);
	const handleSubmitWorkflowAction = useCallback(async () => {
		setLoading(true);
		await formRef.current?.form?.submit();
		setLoading(false);
	}, [formRef]);
	const handleSubmitSuccessfulWorkflowAction = useCallback((response) => {
		handleOnCloseSideSheet();
		closeSideSheet();
		refreshGridData();
	}, [
		handleOnCloseSideSheet,
		refreshGridData,
		closeSideSheet
	]);
	const transactionTypeTransactionActions = useMemo(() => {
		return getTransactionTypeTransactionActions(transaction, TransactionEventTypeEnums.Sidesheet) || [];
	}, [getTransactionTypeTransactionActions, transaction]);
	const [{ data: transactionData }, postTransactionData] = useAxiosMutate(`${ConfigService.productV1ApiUrl}`, "udptransactiontypeTransactionAction", { method: "post" }, {});
	const i18n = useTranslation().t;
	const handleDialogOpen = useCallback((title, message) => {
		setDialogUserStrings({
			title,
			message
		});
		setDialogOpen(true);
	}, []);
	const handleDialogClose = useCallback(() => {
		setDialogOpen(false);
	}, []);
	const handleConfirmActionDialog = useCallback(async () => {
		setDialogOpen(true);
		handleDialogClose();
		setLoading(true);
		try {
			const { status, data } = await postTransactionData({
				url: `${ConfigService.productV1ApiUrl}/udptransactiontypeTransactionAction/${selectedAction.transactionTypeTransactionActionId}/transaction/${transaction.transactionId}/submit`,
				data: {}
			}) || {};
			const apiResult = data?.actionApiResult;
			if (status === 200) {
				if (!apiResult || apiResult.success) {
					handleSuccessSnackbar(i18n("Successfully submitted!"));
					closeSideSheet();
					refreshGridData();
				} else if (apiResult?.errorMessage) handleErrorSnackbar(new Error(apiResult.errorMessage));
			}
		} catch (event) {
			handleSuccessSnackbar(i18n(event.message));
		}
		setLoading(false);
	}, [
		postTransactionData,
		handleDialogClose,
		transaction,
		selectedAction,
		i18n,
		closeSideSheet,
		refreshGridData,
		handleSuccessSnackbar,
		handleErrorSnackbar
	]);
	const handleOnActionClick = useCallback((action) => {
		const pageId = action.parameters.pageId;
		setSelectedAction(action);
		if (pageId) setSideSheetOpen(true);
		else if (action.transactionActionId == TransactionActionTypeEnums.Open_Edit_Transaction_Sidesheet) handleSidesheet("edit", true, transaction);
		else handleDialogOpen(action.name, action.description);
	}, [
		handleDialogOpen,
		transaction,
		handleSidesheet
	]);
	const TransactionActionButtons = useMemo(() => {
		if (!transactionTypeTransactionActions.length) return null;
		return transactionTypeTransactionActions?.map((action_0) => /* @__PURE__ */ React.createElement(TransactionActionButton, {
			key: action_0.transactionTypeTransactionActionId,
			primaryText: action_0.name,
			secondaryText: action_0.description,
			icon: action_0.icon,
			onClick: handleOnActionClick,
			action: action_0
		}));
	}, [transactionTypeTransactionActions, handleOnActionClick]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "transaction-action-buttons-container" }, TransactionActionButtons), dialogUserStrings && /* @__PURE__ */ React.createElement(FluentDialog, {
		title: i18n(`Are you sure you want to proceed with \"${dialogUserStrings.title}\"?`),
		message: dialogUserStrings.message,
		labelOne: i18n("Confirm"),
		actionOne: handleConfirmActionDialog,
		labelTwo: i18n("Cancel"),
		actionTwo: handleDialogClose,
		open: dialogOpen,
		handleClose: handleDialogClose
	}), /* @__PURE__ */ React.createElement(SideSheet, {
		open: sideSheetOpen,
		title: selectedAction?.name,
		onClose: handleOnCloseSideSheet,
		width: 600,
		buttonLabel: i18n("Submit"),
		onSubmit: handleSubmitWorkflowAction,
		progress: loading,
		variant: "indeterminate",
		color: "primary",
		buttonOneDisabled: loading || sidesheetPrimaryButtonDisabled
	}, selectedAction && selectedAction.parameters?.pageId && /* @__PURE__ */ React.createElement(UdpTransactionCustomPage, {
		pageId: selectedAction.parameters.pageId,
		transaction,
		formRef,
		transactionTypeTransactionActionId: selectedAction.transactionTypeTransactionActionId,
		onSubmitSuccess: handleSubmitSuccessfulWorkflowAction,
		refreshGridData,
		additionalPageParms: { sidesheetParams: { setSidesheetPrimaryButtonDisabled } }
	})));
};
//#endregion
export { WorkflowActions as default };

//# sourceMappingURL=WorkflowActions.mjs.map