import { ConfigService } from "../../../../configService/index.mjs";
import { apiMutate } from "../../../../utilities/useAxiosMutate.mjs";
import { SideSheet } from "../../../../UI/surfaces/SideSheet.mjs";
import { LinearProgress } from "../../../../UI/loading/LinearProgress.mjs";
import UdpTransactionTypeForm_default from "../forms/UdpTransactionTypeForm.mjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpTransactionTypesPage/sidesheets/UdpTransactionTypeSidesheet.jsx
const UdpTransactionTypeSidesheet = (props) => {
	const { initialTransactionType, isSideSheetOpen, handleSideSheet, setSelectedDomainName, selectedDomainName, handleCreateTransactionType, handleUpdateTransactionType, catalogObjectNames, catalogObjectProperties, catalogObjectDateProperties, isPageConfigurationSidesheetOpen, setIsPageConfigurationSidesheetOpen, isCalendarViewConfigurationSidesheetOpen, setIsCalendarViewConfigurationSidesheetOpen } = props;
	const isEditing = !!initialTransactionType;
	const formRef = useRef();
	const { t } = useTranslation();
	const [transactions, setTransactions] = useState(null);
	const onSubmitForm = useCallback(async (values, initialData) => {
		if (initialData) await handleUpdateTransactionType(values, initialData);
		else await handleCreateTransactionType(values);
	}, [handleCreateTransactionType, handleUpdateTransactionType]);
	const handleCloseSidesheet = useCallback((isOpen, domainName) => {
		handleSideSheet(isOpen);
		setSelectedDomainName(domainName);
	}, [handleSideSheet, setSelectedDomainName]);
	if (!isSideSheetOpen && transactions) setTransactions(null);
	const shouldDisableSubTypes = useMemo(() => {
		if (!transactions || transactions.length === 0 || !initialTransactionType) return false;
		const targetValue = initialTransactionType.domainFieldValue;
		if (targetValue === void 0 || targetValue === null) return false;
		return transactions.some((t_0) => {
			return t_0.entityBusinessObject?.[initialTransactionType.domainFieldType] === targetValue;
		});
	}, [transactions, initialTransactionType]);
	useEffect(() => {
		if (initialTransactionType) setSelectedDomainName(initialTransactionType?.domainName);
	}, [initialTransactionType, setSelectedDomainName]);
	useEffect(() => {
		let isMounted = true;
		const fetchTransactions = async () => {
			if (isEditing && isSideSheetOpen && initialTransactionType?.transactionTypeId) try {
				const response = await apiMutate(ConfigService.productV1ApiUrl, "UdpTransaction/search", { method: "post" }, { data: {
					filterElements: [{
						searchField: "transactionTypeId",
						searchOperator: "=",
						searchValue: initialTransactionType.transactionTypeId
					}, {
						searchField: "isDeleted",
						searchOperator: "=",
						searchValue: 0
					}],
					getAll: true
				} });
				if (isMounted && response?.data?.pageList) setTransactions(response.data.pageList);
			} catch (error) {
				console.error("Failed to check transaction existence", error);
				if (isMounted) setTransactions([]);
			}
		};
		fetchTransactions();
		return () => {
			isMounted = false;
		};
	}, [
		initialTransactionType,
		isEditing,
		isSideSheetOpen
	]);
	return /* @__PURE__ */ React.createElement(SideSheet, {
		open: isSideSheetOpen,
		width: 800,
		title: isEditing ? t("Edit Transaction Type") : t("Create Transaction Type"),
		onClose: () => handleCloseSidesheet(false),
		buttonLabel: isEditing ? t("Save") : t("Create"),
		onSubmit: () => {
			formRef?.current?.submit();
		}
	}, isEditing && !transactions ? /* @__PURE__ */ React.createElement(LinearProgress, null) : /* @__PURE__ */ React.createElement(UdpTransactionTypeForm_default, {
		ref: formRef,
		onSubmit: onSubmitForm,
		initialData: initialTransactionType,
		disableSubTypeFields: shouldDisableSubTypes,
		domainNameOptions: catalogObjectNames,
		onSelectDomainName: setSelectedDomainName,
		domainFieldOptions: catalogObjectProperties,
		dateDomainFieldOptions: catalogObjectDateProperties,
		selectedDomainName,
		isPageConfigOpen: isPageConfigurationSidesheetOpen,
		setIsPageConfigOpen: setIsPageConfigurationSidesheetOpen,
		isCalendarViewConfigOpen: isCalendarViewConfigurationSidesheetOpen,
		setIsCalendarViewConfigOpen: setIsCalendarViewConfigurationSidesheetOpen
	}));
};
//#endregion
export { UdpTransactionTypeSidesheet as default };

//# sourceMappingURL=UdpTransactionTypeSidesheet.mjs.map