import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { LinearProgress, SideSheet } from '../../../../UI/index.ts';
import { useTranslation } from 'react-i18next';
import UdpTransactionTypeForm from '../forms/UdpTransactionTypeForm';
import { apiMutate } from '../../../../utilities';
import { ConfigService } from '../../../../configService';
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 === undefined || targetValue === null) {
            return false;
        }
        return transactions.some((t) => {
            const entityBusinessObject = t.entityBusinessObject;
            return ((entityBusinessObject === null || entityBusinessObject === void 0 ? void 0 : entityBusinessObject[initialTransactionType.domainFieldType]) ===
                targetValue);
        });
    }, [transactions, initialTransactionType]);
    useEffect(() => {
        if (initialTransactionType) {
            setSelectedDomainName(initialTransactionType === null || initialTransactionType === void 0 ? void 0 : initialTransactionType.domainName);
        }
    }, [initialTransactionType, setSelectedDomainName]);
    useEffect(() => {
        let isMounted = true;
        const fetchTransactions = async () => {
            var _a;
            if (isEditing &&
                isSideSheetOpen &&
                (initialTransactionType === null || initialTransactionType === void 0 ? void 0 : 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 && ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.pageList)) {
                        setTransactions(response.data.pageList);
                    }
                }
                catch (error) {
                    console.error('Failed to check transaction existence', error);
                    if (isMounted)
                        setTransactions([]);
                }
            }
        };
        // eslint-disable-next-line react-you-might-not-need-an-effect/no-adjust-state-on-prop-change
        fetchTransactions();
        return () => {
            isMounted = false;
        };
    }, [initialTransactionType, isEditing, isSideSheetOpen]);
    return (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: () => {
            var _a;
            // eslint-disable-next-line no-unused-expressions
            (_a = formRef === null || formRef === void 0 ? void 0 : formRef.current) === null || _a === void 0 ? void 0 : _a.submit();
        } }, isEditing && !transactions ? (React.createElement(LinearProgress, null)) : (React.createElement(UdpTransactionTypeForm, { ref: formRef, onSubmit: onSubmitForm, initialData: initialTransactionType, disableSubTypeFields: shouldDisableSubTypes, domainNameOptions: catalogObjectNames, onSelectDomainName: setSelectedDomainName, domainFieldOptions: catalogObjectProperties, dateDomainFieldOptions: catalogObjectDateProperties, selectedDomainName: selectedDomainName, isPageConfigOpen: isPageConfigurationSidesheetOpen, setIsPageConfigOpen: setIsPageConfigurationSidesheetOpen, isCalendarViewConfigOpen: isCalendarViewConfigurationSidesheetOpen, setIsCalendarViewConfigOpen: setIsCalendarViewConfigurationSidesheetOpen }))));
};
export default UdpTransactionTypeSidesheet;
//# sourceMappingURL=UdpTransactionTypeSidesheet.js.map