import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
import { SideSheet } from '../../../../UI';
import { useTranslation } from 'react-i18next';
import { SearchMethodUdpGrid } from '../../../../UI/templates/newGrid';
import UdpTransactionTypeTransactionActionCreateAndEditSidesheet from './UdpTransactionTypeTransactionActionCreateAndEditSidesheet';
import { UdpTransactionActionEnums } from '../../../../enums';
import { ConfigService } from '../../../../configService';
const UdpTransactionTypeTransactionActionSidesheet = (props, ref) => {
    const { transactionType, transactionTypeTransactionAction, isSideSheetOpen, isTransactionTypeTransactionActionCreateOrEditSidesheetOpen, handleTransactionTypeTransactionActionSidesheet, handleCreateOrEditTransactionTypeTransactionActionSidesheet, handleCreateTransactionTypeTransactionAction, handleUpdateTransactionTypeTransactionAction, handleDeleteTransactionTypeTransactionAction, transactionActions, catalogObjectProperties, actionApis, pages, ttstatuses } = props;
    const transactionTypeTransactionActionGridApiRef = useRef(null);
    const { t } = useTranslation();
    const filteredTransactionActions = useMemo(() => {
        if (!transactionActions)
            return [];
        return transactionActions.filter((ta) => (ta === null || ta === void 0 ? void 0 : ta.name) !== UdpTransactionActionEnums.Send_Email &&
            (ta === null || ta === void 0 ? void 0 : ta.name) !== UdpTransactionActionEnums.Custom_Client_Action);
    }, [transactionActions]);
    useImperativeHandle(ref, () => ({
        refreshServerSide: () => {
            var _a;
            // eslint-disable-next-line no-unused-expressions
            (_a = transactionTypeTransactionActionGridApiRef.current) === null || _a === void 0 ? void 0 : _a.refreshServerSide();
        }
    }));
    const getTransactionActionName = useCallback((transactionActionId) => {
        if (!transactionActions)
            return '';
        const matchingTransactionAction = transactionActions.find((transactionAction) => transactionAction.transactionActionId === transactionActionId);
        return (matchingTransactionAction === null || matchingTransactionAction === void 0 ? void 0 : matchingTransactionAction.name) || '';
    }, [transactionActions]);
    const columnDefs = useMemo(() => {
        return [
            {
                headerName: t('Name'),
                field: 'name',
                resizable: true,
                sortable: false
            },
            {
                headerName: t('Description'),
                field: 'description',
                resizable: true,
                sortable: false
            },
            {
                headerName: t('Icon'),
                field: 'icon',
                resizable: true,
                sortable: false
            },
            {
                headerName: t('Transaction Action'),
                resizable: true,
                sortable: false,
                valueGetter: (params) => getTransactionActionName(params.data.transactionActionId)
            },
            {
                headerName: t('Actions'),
                field: 'actions',
                cellRenderer: 'actionsRenderer',
                cellRendererParams: {
                    actionList: [
                        {
                            id: 0,
                            title: t('Edit'),
                            iconName: 'edit',
                            handleOnClick: (e, transactionTypeTransactionAction) => {
                                handleCreateOrEditTransactionTypeTransactionActionSidesheet(true, transactionTypeTransactionAction === null || transactionTypeTransactionAction === void 0 ? void 0 : transactionTypeTransactionAction.data);
                            }
                        },
                        {
                            id: 1,
                            title: t('Delete'),
                            iconName: 'trashCan',
                            handleOnClick: (e, transactionTypeTransactionAction) => {
                                handleDeleteTransactionTypeTransactionAction(transactionTypeTransactionAction === null || transactionTypeTransactionAction === void 0 ? void 0 : transactionTypeTransactionAction.data);
                            }
                        }
                    ]
                },
                suppressMenu: true,
                sortable: false,
                filter: false,
                valueGetter: (params) => params.data
            }
        ];
    }, [
        t,
        handleCreateOrEditTransactionTypeTransactionActionSidesheet,
        handleDeleteTransactionTypeTransactionAction,
        getTransactionActionName
    ]);
    const headerConfig = {
        title: t('Actions'),
        primaryActionButton: {
            label: t('Add'),
            iconName: 'add',
            onClick: () => handleCreateOrEditTransactionTypeTransactionActionSidesheet(true)
        }
    };
    const gridFunctions = [
        { name: 'agGridExport' },
        { name: 'agGridSizeColumnsToFit' },
        { name: 'agGridAutoSizeColumns' },
        { name: 'agGridResetColumns' },
        { name: 'agGridHideShowColumns' }
    ];
    const gridOptions = useMemo(() => {
        return {
            pagination: true
        };
    }, []);
    const handleGridApiCallback = (api) => {
        transactionTypeTransactionActionGridApiRef.current = api;
    };
    const searchMethodConfig = useMemo(() => {
        let filterElements = [];
        if (transactionType === null || transactionType === void 0 ? void 0 : transactionType.transactionTypeId) {
            filterElements = [
                {
                    searchField: 'TransactionTypeId',
                    searchOperator: '=',
                    searchValue: transactionType === null || transactionType === void 0 ? void 0 : transactionType.transactionTypeId,
                    groupId: 1
                }
            ];
            return {
                requestUrl: `${ConfigService.productV1ApiUrl}/UdpTransactionTypeTransactionAction/search`,
                customSearchObject: {
                    filterElements: filterElements
                }
            };
        }
        return {};
    }, [transactionType]);
    return (React.createElement(SideSheet, { open: isSideSheetOpen, width: 800, title: t('Manage Actions'), onClose: () => handleTransactionTypeTransactionActionSidesheet(false) }, React.createElement(SearchMethodUdpGrid, { searchMethodConfig: searchMethodConfig, columnDefs: columnDefs, gridHeaderConfig: headerConfig, gridOptions: gridOptions, gridApiCallback: handleGridApiCallback, gridFunctions: gridFunctions }), React.createElement(UdpTransactionTypeTransactionActionCreateAndEditSidesheet, { initialTransactionTypeTransactionAction: transactionTypeTransactionAction, isSideSheetOpen: isTransactionTypeTransactionActionCreateOrEditSidesheetOpen, handleCreateTransactionTypeTransactionAction: handleCreateTransactionTypeTransactionAction, handleUpdateTransactionTypeTransactionAction: handleUpdateTransactionTypeTransactionAction, handleSideSheet: handleCreateOrEditTransactionTypeTransactionActionSidesheet, transactionActions: filteredTransactionActions, catalogObjectProperties: catalogObjectProperties, actionApis: actionApis, pages: pages, transactionTypeStatusOptions: ttstatuses })));
};
export default forwardRef(UdpTransactionTypeTransactionActionSidesheet);
//# sourceMappingURL=UdpTransactionTypeTransactionActionSidesheet.js.map