import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useRef } from 'react';
import { SideSheet } from '../../../../UI';
import { SearchMethodUdpGrid } from '../../../../UI/templates/newGrid';
import { useTranslation } from 'react-i18next';
import UdpTransactionTypeConfigurationTransactionActionCreateAndEditSidesheet from './UdpTransactionTypeConfigurationTransactionActionCreateAndEditSidesheet';
import { ConfigService } from '../../../../configService';
const UdpTransactionTypeConfigurationTransactionActionSidesheet = (props, ref) => {
    const { transactionTypeConfiguration, transactionTypeConfigurationTransactionAction, isSideSheetOpen, handleTransactionTypeConfigurationTransactionActionSidesheet, isCreateOrEditSidesheetOpen, handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet, handleCreateTransactionTypeConfigurationTransactionAction, handleUpdateTransactionTypeConfigurationTransactionAction, handleDeleteTransactionTypeConfigurationTransactionAction, tttactions, ttctaList } = props;
    const ttctaGridApiRef = useRef(null);
    const { t } = useTranslation();
    useImperativeHandle(ref, () => ({
        refreshServerSide: () => {
            var _a;
            // eslint-disable-next-line no-unused-expressions
            (_a = ttctaGridApiRef === null || ttctaGridApiRef === void 0 ? void 0 : ttctaGridApiRef.current) === null || _a === void 0 ? void 0 : _a.refreshServerSide();
        }
    }));
    const getTTTActionName = useCallback((transactionTypeTransactionActionId) => {
        if (!(tttactions === null || tttactions === void 0 ? void 0 : tttactions.length))
            return '';
        const matchingTTTAction = tttactions.find((ttta) => ttta.transactionTypeTransactionActionId ===
            transactionTypeTransactionActionId);
        return (matchingTTTAction === null || matchingTTTAction === void 0 ? void 0 : matchingTTTAction.name) || '';
    }, [tttactions]);
    const columnDefs = useMemo(() => {
        return [
            {
                headerName: t('TransactionTypeConfiguration'),
                field: 'transactionTypeConfigurationId',
                resizable: true,
                sortable: false,
                hide: true
            },
            {
                headerName: t('TransactionTypeTransactionAction'),
                field: 'transactionTypeTransactionActionId',
                resizable: false,
                sortable: false,
                hide: true
            },
            {
                headerName: t('Name'),
                field: 'name',
                resizable: true,
                sortable: false,
                valueGetter: (params) => {
                    var _a;
                    return getTTTActionName((_a = params === null || params === void 0 ? void 0 : params.data) === null || _a === void 0 ? void 0 : _a.transactionTypeTransactionActionId);
                }
            },
            {
                headerName: t('Actions'),
                field: 'actions',
                cellRenderer: 'actionsRenderer',
                cellRendererParams: {
                    actionList: [
                        // Commenting out the Edit action for now as the underlying DAO method does not allow updating a TTCTA's TTTAction
                        // {
                        //   id: 0,
                        //   title: t('Edit'),
                        //   iconName: 'edit',
                        //   handleOnClick: (e, transactionTypeConfigurationTransactionAction) => {
                        //     handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet(true, transactionTypeConfigurationTransactionAction?.data);
                        //   }
                        // },
                        {
                            id: 1,
                            title: t('Delete'),
                            iconName: 'trashCan',
                            handleOnClick: (e, transactionTypeConfigurationTransactionAction) => {
                                handleDeleteTransactionTypeConfigurationTransactionAction(transactionTypeConfigurationTransactionAction === null || transactionTypeConfigurationTransactionAction === void 0 ? void 0 : transactionTypeConfigurationTransactionAction.data);
                            }
                        }
                    ]
                },
                suppressMenu: true,
                sortable: false,
                filter: false,
                valueGetter: (params) => params.data
            }
        ];
    }, [
        t,
        //handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet,
        handleDeleteTransactionTypeConfigurationTransactionAction,
        getTTTActionName
    ]);
    const headerConfig = {
        title: t('Linked Actions'),
        primaryActionButton: {
            label: t('Add'),
            iconName: 'add',
            onClick: () => handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet(true)
        }
    };
    const gridFunctions = [
        { name: 'agGridSizeColumnsToFit' },
        { name: 'agGridAutoSizeColumns' },
        { name: 'agGridResetColumns' },
        { name: 'agGridHideShowColumns' }
    ];
    const gridOptions = useMemo(() => {
        return {
            pagination: true
        };
    }, []);
    const handleGridApiCallback = (api) => {
        ttctaGridApiRef.current = api;
    };
    const searchMethodConfig = useMemo(() => {
        let filterElements = [];
        if (transactionTypeConfiguration === null || transactionTypeConfiguration === void 0 ? void 0 : transactionTypeConfiguration.transactionTypeConfigurationId) {
            filterElements = [
                {
                    searchField: 'TransactionTypeConfigurationId',
                    searchOperator: '=',
                    searchValue: transactionTypeConfiguration === null || transactionTypeConfiguration === void 0 ? void 0 : transactionTypeConfiguration.transactionTypeConfigurationId,
                    groupId: 1
                }
            ];
            return {
                requestUrl: `${ConfigService.productV1ApiUrl}/UdpTransactionTypeConfigurationTransactionAction/search`,
                customSearchObject: {
                    filterElements: filterElements
                }
            };
        }
        return {};
    }, [transactionTypeConfiguration]);
    return (React.createElement(SideSheet, { open: isSideSheetOpen, width: 700, title: t('Manage Linked Actions'), onClose: () => handleTransactionTypeConfigurationTransactionActionSidesheet(false) }, React.createElement(SearchMethodUdpGrid, { searchMethodConfig: searchMethodConfig, columnDefs: columnDefs, gridHeaderConfig: headerConfig, gridOptions: gridOptions, gridApiCallback: handleGridApiCallback, gridFunctions: gridFunctions }), React.createElement(UdpTransactionTypeConfigurationTransactionActionCreateAndEditSidesheet, { initialTransactionTypeConfigurationTransactionAction: transactionTypeConfigurationTransactionAction, isSideSheetOpen: isCreateOrEditSidesheetOpen, handleCreateTransactionTypeConfigurationTransactionAction: handleCreateTransactionTypeConfigurationTransactionAction, handleUpdateTransactionTypeConfigurationTransactionAction: handleUpdateTransactionTypeConfigurationTransactionAction, handleSideSheet: handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet, transactionTypeTransactionActions: tttactions, existingTtctaList: ttctaList })));
};
export default forwardRef(UdpTransactionTypeConfigurationTransactionActionSidesheet);
//# sourceMappingURL=UdpTransactionTypeConfigurationTransactionActionSidesheet.js.map