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 UdpTransactionUiActionCreateAndEditSidesheet from './UdpTransactionUiActionCreateAndEditSidesheet';
import { ConfigService } from '../../../../configService';
const UdpTransactionUiActionSidesheet = (props, ref) => {
    const { transactionType, transactionUiAction, isSideSheetOpen, handleTransactionUiActionSidesheet, isCreateOrEditTransactionUiActionSidesheetOpen, handleCreateOrEditTransactionUiActionSidesheet, handleCreateTransactionUiAction, handleUpdateTransactionUiAction, handleDeleteTransactionUiAction, transactionUiActionTypes, actionApis, pages, tUiActionsList } = props;
    const transactionUiActionGridApiRef = useRef(null);
    const { t } = useTranslation();
    useImperativeHandle(ref, () => ({
        refreshServerSide: () => {
            var _a;
            // eslint-disable-next-line no-unused-expressions
            (_a = transactionUiActionGridApiRef === null || transactionUiActionGridApiRef === void 0 ? void 0 : transactionUiActionGridApiRef.current) === null || _a === void 0 ? void 0 : _a.refreshServerSide();
        }
    }));
    const getTransactionUiActionTypeName = useCallback((transactionUiActionTypeId) => {
        if (!transactionUiActionTypes)
            return '';
        const matchingTransactionUiActionType = transactionUiActionTypes.find((transactionUiActionType) => transactionUiActionType.transactionUiActionTypeId ===
            transactionUiActionTypeId);
        return (matchingTransactionUiActionType === null || matchingTransactionUiActionType === void 0 ? void 0 : matchingTransactionUiActionType.name) || '';
    }, [transactionUiActionTypes]);
    const columnDefs = useMemo(() => {
        return [
            {
                headerName: t('TransactionUiActionId'),
                field: 'transactionUiActionId',
                resizable: true,
                sortable: false,
                hide: true
            },
            {
                headerName: t('Type'),
                field: 'transactionUiActionTypeId',
                resizable: false,
                sortable: false,
                valueGetter: (params) => getTransactionUiActionTypeName(params.data.transactionUiActionTypeId)
            },
            {
                headerName: t('Actions'),
                field: 'actions',
                cellRenderer: 'actionsRenderer',
                cellRendererParams: {
                    actionList: [
                        {
                            id: 0,
                            title: t('Edit'),
                            iconName: 'edit',
                            handleOnClick: (e, transactionUiAction) => {
                                handleCreateOrEditTransactionUiActionSidesheet(true, transactionUiAction === null || transactionUiAction === void 0 ? void 0 : transactionUiAction.data);
                            }
                        },
                        {
                            id: 1,
                            title: t('Delete'),
                            iconName: 'trashCan',
                            handleOnClick: (e, transactionUiAction) => {
                                handleDeleteTransactionUiAction(transactionUiAction === null || transactionUiAction === void 0 ? void 0 : transactionUiAction.data);
                            }
                        }
                    ]
                },
                suppressMenu: true,
                sortable: false,
                filter: false,
                valueGetter: (params) => params.data
            }
        ];
    }, [
        t,
        handleCreateOrEditTransactionUiActionSidesheet,
        handleDeleteTransactionUiAction,
        getTransactionUiActionTypeName
    ]);
    const headerConfig = {
        title: t('Form / Page Overrides'),
        primaryActionButton: {
            label: t('Add'),
            iconName: 'add',
            onClick: () => handleCreateOrEditTransactionUiActionSidesheet(true)
        }
    };
    const gridFunctions = [
        { name: 'agGridExport' },
        { name: 'agGridSizeColumnsToFit' },
        { name: 'agGridAutoSizeColumns' },
        { name: 'agGridResetColumns' },
        { name: 'agGridHideShowColumns' }
    ];
    const gridOptions = useMemo(() => {
        return {
            pagination: true
        };
    }, []);
    const handleGridApiCallback = (api) => {
        transactionUiActionGridApiRef.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}/UdpTransactionUiAction/search`,
                customSearchObject: {
                    filterElements: filterElements
                }
            };
        }
        return {};
    }, [transactionType]);
    return (React.createElement(SideSheet, { open: isSideSheetOpen, width: 800, title: t('Manage Form / Page Overrides'), onClose: () => handleTransactionUiActionSidesheet(false) }, React.createElement(SearchMethodUdpGrid, { searchMethodConfig: searchMethodConfig, columnDefs: columnDefs, gridHeaderConfig: headerConfig, gridOptions: gridOptions, gridApiCallback: handleGridApiCallback, gridFunctions: gridFunctions }), React.createElement(UdpTransactionUiActionCreateAndEditSidesheet, { initialTransactionUiAction: transactionUiAction, isSideSheetOpen: isCreateOrEditTransactionUiActionSidesheetOpen, handleCreateTransactionUiAction: handleCreateTransactionUiAction, handleUpdateTransactionUiAction: handleUpdateTransactionUiAction, handleSideSheet: handleCreateOrEditTransactionUiActionSidesheet, transactionUiActionTypes: transactionUiActionTypes, actionApis: actionApis, pages: pages, tUiActionsList: tUiActionsList })));
};
export default forwardRef(UdpTransactionUiActionSidesheet);
//# sourceMappingURL=UdpTransactionUiActionSidesheet.js.map