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 UdpTransactionTypeConfigurationCreateAndEditSidesheet from './UdpTransactionTypeConfigurationCreateAndEditSidesheet';
import UdpTransactionTypeConfigurationTransactionActionSidesheet from './UdpTransactionTypeConfigurationTransactionActionSidesheet';
import { ConfigService } from '../../../../configService';
const UdpTransactionTypeConfigurationSidesheet = (props, ref) => {
    const { transactionType, transactionTypeConfiguration, isSideSheetOpen, isCreateOrEditTransactionTypeConfigurationSidesheetOpen, handleTransactionTypeConfigurationSidesheet, handleCreateOrEditTransactionTypeConfigurationSidesheet, handleCreateTransactionTypeConfiguration, handleUpdateTransactionTypeConfiguration, handleDeleteTransactionTypeConfiguration, transactionRuleTypes, catalogObjectProperties, ttstatuses, roles, transactionTypeConfigurationTransactionAction, isTransactionTypeConfigurationTransactionActionSidesheetOpen, handleTransactionTypeConfigurationTransactionActionSidesheet, handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet, isCreateOrEditTransactionTypeConfigurationTransactionActionSidesheetOpen, tttactions, handleCreateTransactionTypeConfigurationTransactionAction, handleUpdateTransactionTypeConfigurationTransactionAction, handleDeleteTransactionTypeConfigurationTransactionAction, ttctaList, ttctaGridApiRef } = props;
    const transactionTypeConfigurationGridApiRef = useRef(null);
    const { t } = useTranslation();
    useImperativeHandle(ref, () => ({
        refreshServerSide: () => {
            var _a;
            // eslint-disable-next-line no-unused-expressions
            (_a = transactionTypeConfigurationGridApiRef === null || transactionTypeConfigurationGridApiRef === void 0 ? void 0 : transactionTypeConfigurationGridApiRef.current) === null || _a === void 0 ? void 0 : _a.refreshServerSide();
        }
    }));
    const getTransactionRuleTypeName = useCallback((transactionRuleTypeId) => {
        if (!transactionRuleTypes)
            return '';
        const matchingTransactionRuleType = transactionRuleTypes === null || transactionRuleTypes === void 0 ? void 0 : transactionRuleTypes.find((transactionRuleType) => (transactionRuleType === null || transactionRuleType === void 0 ? void 0 : transactionRuleType.transactionRuleTypeId) === transactionRuleTypeId);
        return (matchingTransactionRuleType === null || matchingTransactionRuleType === void 0 ? void 0 : matchingTransactionRuleType.name) || '';
    }, [transactionRuleTypes]);
    const columnDefs = useMemo(() => {
        return [
            {
                headerName: t('TransactionTypeConfigurationId'),
                field: 'transactionTypeConfigurationId',
                resizable: true,
                sortable: false,
                hide: true
            },
            {
                headerName: t('Name'),
                field: 'name',
                resizable: true,
                sortable: false
            },
            {
                headerName: t('Order'),
                field: 'order',
                resizable: false,
                sortable: false
            },
            {
                headerName: t('Enabled'),
                field: 'enabled',
                resizable: false,
                sortable: false,
                cellDataType: 'boolean'
            },
            {
                headerName: t('Rule Type'),
                resizable: false,
                sortable: false,
                valueGetter: (params) => {
                    var _a;
                    return getTransactionRuleTypeName((_a = params === null || params === void 0 ? void 0 : params.data) === null || _a === void 0 ? void 0 : _a.transactionRuleTypeId);
                }
            },
            {
                headerName: t('Actions'),
                field: 'actions',
                cellRenderer: 'actionsRenderer',
                cellRendererParams: {
                    actionList: [
                        {
                            id: 0,
                            title: t('Edit'),
                            iconName: 'edit',
                            handleOnClick: (e, transactionTypeConfiguration) => {
                                handleCreateOrEditTransactionTypeConfigurationSidesheet(true, transactionTypeConfiguration === null || transactionTypeConfiguration === void 0 ? void 0 : transactionTypeConfiguration.data);
                            }
                        },
                        {
                            id: 1,
                            title: t('Delete'),
                            iconName: 'trashCan',
                            handleOnClick: (e, transactionTypeConfiguration) => {
                                handleDeleteTransactionTypeConfiguration(transactionTypeConfiguration === null || transactionTypeConfiguration === void 0 ? void 0 : transactionTypeConfiguration.data);
                            }
                        },
                        {
                            id: 2,
                            title: t('Link'),
                            iconName: 'link',
                            handleOnClick: (e, transactionTypeConfiguration) => {
                                handleTransactionTypeConfigurationTransactionActionSidesheet(true, transactionTypeConfiguration === null || transactionTypeConfiguration === void 0 ? void 0 : transactionTypeConfiguration.data);
                            }
                        }
                    ]
                },
                suppressMenu: true,
                sortable: false,
                filter: false,
                valueGetter: (params) => params.data
            }
        ];
    }, [
        t,
        handleCreateOrEditTransactionTypeConfigurationSidesheet,
        handleDeleteTransactionTypeConfiguration,
        getTransactionRuleTypeName,
        handleTransactionTypeConfigurationTransactionActionSidesheet
    ]);
    const headerConfig = {
        title: t('Rules'),
        primaryActionButton: {
            label: t('Add'),
            iconName: 'add',
            onClick: () => handleCreateOrEditTransactionTypeConfigurationSidesheet(true)
        }
    };
    const gridFunctions = [
        { name: 'agGridSizeColumnsToFit' },
        { name: 'agGridAutoSizeColumns' },
        { name: 'agGridResetColumns' },
        { name: 'agGridHideShowColumns' }
    ];
    const gridOptions = useMemo(() => {
        return {
            pagination: true
        };
    }, []);
    const handleGridApiCallback = (api) => {
        transactionTypeConfigurationGridApiRef.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}/UdpTransactionTypeConfiguration/search`,
                customSearchObject: {
                    filterElements: filterElements
                }
            };
        }
        return {};
    }, [transactionType]);
    return (React.createElement(SideSheet, { open: isSideSheetOpen, width: 900, title: t('Manage Rules'), onClose: () => handleTransactionTypeConfigurationSidesheet(false) }, React.createElement(SearchMethodUdpGrid, { searchMethodConfig: searchMethodConfig, columnDefs: columnDefs, gridHeaderConfig: headerConfig, gridOptions: gridOptions, gridApiCallback: handleGridApiCallback, gridFunctions: gridFunctions }), React.createElement(UdpTransactionTypeConfigurationCreateAndEditSidesheet, { initialTransactionTypeConfiguration: transactionTypeConfiguration, isSideSheetOpen: isCreateOrEditTransactionTypeConfigurationSidesheetOpen, handleCreateTransactionTypeConfiguration: handleCreateTransactionTypeConfiguration, handleUpdateTransactionTypeConfiguration: handleUpdateTransactionTypeConfiguration, handleSideSheet: handleCreateOrEditTransactionTypeConfigurationSidesheet, transactionRuleTypes: transactionRuleTypes, catalogObjectProperties: catalogObjectProperties, ttstatuses: ttstatuses, roles: roles }), React.createElement(UdpTransactionTypeConfigurationTransactionActionSidesheet, { ref: ttctaGridApiRef, transactionTypeConfiguration: transactionTypeConfiguration, transactionTypeConfigurationTransactionAction: transactionTypeConfigurationTransactionAction, isSideSheetOpen: isTransactionTypeConfigurationTransactionActionSidesheetOpen, handleTransactionTypeConfigurationTransactionActionSidesheet: handleTransactionTypeConfigurationTransactionActionSidesheet, handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet: handleCreateOrEditTransactionTypeConfigurationTransactionActionSidesheet, isCreateOrEditSidesheetOpen: isCreateOrEditTransactionTypeConfigurationTransactionActionSidesheetOpen, tttactions: tttactions, handleCreateTransactionTypeConfigurationTransactionAction: handleCreateTransactionTypeConfigurationTransactionAction, handleUpdateTransactionTypeConfigurationTransactionAction: handleUpdateTransactionTypeConfigurationTransactionAction, handleDeleteTransactionTypeConfigurationTransactionAction: handleDeleteTransactionTypeConfigurationTransactionAction, ttctaList: ttctaList })));
};
export default forwardRef(UdpTransactionTypeConfigurationSidesheet);
//# sourceMappingURL=UdpTransactionTypeConfigurationSidesheet.js.map