import React, { forwardRef, useImperativeHandle, useMemo, useRef } from 'react';
import { SideSheet } from '../../../../UI';
import { useTranslation } from 'react-i18next';
import { SearchMethodUdpGrid } from '../../../../UI/templates/newGrid';
import UdpTransactionTypeStatusCreateAndEditSidesheet from './UdpTransactionTypeStatusCreateAndEditSidesheet';
import { TRANSACTION_TYPE_STATUS_COLORS } from '../../../utilities/transactionConstants';
import { ConfigService } from '../../../../configService';
const UdpTransactionTypeStatusSidesheet = (props, ref) => {
    const { transactionType, transactionTypeStatus, isSideSheetOpen, isTransactionTypeStatusCreateOrEditSidesheetOpen, handleTransactionTypeStatusSideSheet, handleCreateOrEditTransactionTypeStatusSideSheet, handleCreateTransactionTypeStatus, handleUpdateTransactionTypeStatus, handleDeleteTransactionTypeStatus } = props;
    const transactionTypeStatusGridApiRef = useRef(null);
    const { t } = useTranslation();
    useImperativeHandle(ref, () => ({
        refreshServerSide: () => {
            var _a;
            // eslint-disable-next-line no-unused-expressions
            (_a = transactionTypeStatusGridApiRef === null || transactionTypeStatusGridApiRef === void 0 ? void 0 : transactionTypeStatusGridApiRef.current) === null || _a === void 0 ? void 0 : _a.refreshServerSide();
        }
    }));
    const getStatusLabel = (node) => {
        var _a;
        let label = '';
        label = (_a = TRANSACTION_TYPE_STATUS_COLORS.find((color) => color.value === node.data.color)) === null || _a === void 0 ? void 0 : _a.name;
        return label || '';
    };
    const getStatusColor = useMemo(() => {
        return {
            Default: 'gray',
            Warning: 'warning',
            Info: 'info',
            Success: 'success',
            Error: 'error'
        };
    }, []);
    const columnDefs = useMemo(() => {
        return [
            {
                headerName: t('Name'),
                field: 'name',
                resizable: true,
                sortable: false
            },
            {
                headerName: t('Code'),
                field: 'code',
                resizable: true,
                sortable: false
            },
            {
                headerName: t('Color'),
                field: 'color',
                resizable: true,
                sortable: false,
                cellRenderer: 'statusRenderer',
                cellRendererParams: {
                    statusMappingConfig: getStatusColor,
                    getStatusValue: getStatusLabel
                }
            },
            {
                headerName: t('Default'),
                field: 'isDefault',
                resizable: true,
                sortable: false,
                cellDataType: 'boolean'
            },
            {
                headerName: t('Closed Status'),
                field: 'isClosedStatus',
                resizable: true,
                sortable: false,
                cellDataType: 'boolean'
            },
            {
                headerName: t('Cancelled Status'),
                field: 'isCancelledStatus',
                resizable: true,
                sortable: false,
                cellDataType: 'boolean'
            },
            {
                headerName: t('Actions'),
                field: 'actions',
                cellRenderer: 'actionsRenderer',
                cellRendererParams: {
                    actionList: [
                        {
                            id: 0,
                            title: t('Edit'),
                            iconName: 'edit',
                            handleOnClick: (e, transactionTypeStatus) => {
                                handleCreateOrEditTransactionTypeStatusSideSheet(true, transactionTypeStatus === null || transactionTypeStatus === void 0 ? void 0 : transactionTypeStatus.data);
                            }
                        },
                        {
                            id: 1,
                            title: t('Delete'),
                            iconName: 'trashCan',
                            handleOnClick: (e, transactionTypeStatus) => {
                                handleDeleteTransactionTypeStatus(transactionTypeStatus === null || transactionTypeStatus === void 0 ? void 0 : transactionTypeStatus.data);
                            }
                        }
                    ]
                },
                suppressMenu: true,
                sortable: false,
                filter: false,
                valueGetter: (params) => params.data
            }
        ];
    }, [
        t,
        handleCreateOrEditTransactionTypeStatusSideSheet,
        handleDeleteTransactionTypeStatus
    ]);
    const headerConfig = {
        title: t('Statuses'),
        primaryActionButton: {
            label: t('Add'),
            iconName: 'add',
            onClick: () => handleCreateOrEditTransactionTypeStatusSideSheet(true)
        }
    };
    const gridFunctions = [
        { name: 'agGridExport' },
        { name: 'agGridSizeColumnsToFit' },
        { name: 'agGridAutoSizeColumns' },
        { name: 'agGridResetColumns' },
        { name: 'agGridHideShowColumns' }
    ];
    const gridOptions = useMemo(() => {
        return {
            pagination: true
        };
    }, []);
    const handleGridApiCallback = (api) => {
        transactionTypeStatusGridApiRef.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}/UdpTransactionTypeStatus/search`,
                customSearchObject: {
                    filterElements: filterElements
                }
            };
        }
        return {};
    }, [transactionType]);
    return (React.createElement(SideSheet, { open: isSideSheetOpen, width: 900, title: t('Manage Statuses'), onClose: () => handleTransactionTypeStatusSideSheet(false) }, React.createElement(SearchMethodUdpGrid, { searchMethodConfig: searchMethodConfig, columnDefs: columnDefs, gridHeaderConfig: headerConfig, gridOptions: gridOptions, gridApiCallback: handleGridApiCallback, gridFunctions: gridFunctions }), React.createElement(UdpTransactionTypeStatusCreateAndEditSidesheet, { initialTransactionTypeStatus: transactionTypeStatus, isSideSheetOpen: isTransactionTypeStatusCreateOrEditSidesheetOpen, handleCreateTransactionTypeStatus: handleCreateTransactionTypeStatus, handleUpdateTransactionTypeStatus: handleUpdateTransactionTypeStatus, handleSideSheet: handleCreateOrEditTransactionTypeStatusSideSheet })));
};
export default forwardRef(UdpTransactionTypeStatusSidesheet);
//# sourceMappingURL=UdpTransactionTypeStatusSidesheet.js.map