var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useMemo, useState, useRef, forwardRef, useImperativeHandle } from 'react';
import { ApiMethodInstanceGrid } from 'udp-react-stencil-component-library';
import { useUdpExport } from '../../../udp/export/useUdpExport';
import { ExportSidesheet } from '../../../udp/export/ExportSidesheet';
/*
 * This grid is intended to be a temporary drop in replacement for ambient-template-grid.
 * Do not use it unless you were using the old grid, this will not be further supported and is worse on performance than the other new grids.
 */
export const BackwardsCompatibleGrid = forwardRef((props, ref) => {
    const { gridBarTitle, queryId, gridId, primaryActionOnClick, primaryActionLabel, primaryActionIcon, defaultSearchObject, renderConfigs, disableGridPersistance, enableCheckboxOnColumns, gridHeight } = props;
    const [exportSidesheetOpen, setExportSidesheetOpen] = useState(false);
    const gridApiRef = useRef(null);
    //Convert the filter Elements passed in to a search object
    const fullDefaultSearchObject = useMemo(() => {
        return {
            filterElements: defaultSearchObject,
            logicalSearchOperator: 1
        };
    }, [defaultSearchObject]);
    //Convert renderConfigs to columnOverrides
    const columnOverrides = useMemo(() => {
        const initialColumns = renderConfigs;
        return initialColumns.map((col) => {
            // Use destructuring to separate 'rendererName' from the rest of the properties.
            const { rendererName } = col, rest = __rest(col, ["rendererName"]);
            // Start building the new object, renaming rendererName if it exists.
            const newCol = Object.assign(Object.assign({}, rest), (rendererName && { cellRenderer: rendererName }));
            return newCol;
        });
    }, [renderConfigs]);
    const sizedIcon = useMemo(() => {
        if (primaryActionIcon === 'add32') {
            return 'add';
        }
    }, [primaryActionIcon]);
    //export functions
    const { updateExportParameters, createExport, entity, searchObject, fileFormat, numRowsToExport, validSearch, errorList, activeExportJob, exportLoading, handleFileDownload, clearParameters } = useUdpExport();
    const exportFromGrid = (searchObject, entityName) => {
        if (searchObject && entityName) {
            updateExportParameters(entityName, searchObject);
            setExportSidesheetOpen(true);
        }
        else {
            console.error('Failed to retrieve searchObject or entityName from grid');
        }
    };
    const handleCloseExportSidesheet = () => {
        setExportSidesheetOpen(false);
        clearParameters();
    };
    useImperativeHandle(ref, () => ({
        //hooks for the parent to call these functions
        refreshOnNoResults: () => {
            var _a;
            return (_a = gridApiRef.current) === null || _a === void 0 ? void 0 : _a.refreshServerSide();
        },
        getSelectedRows: () => {
            var _a;
            return (_a = gridApiRef.current) === null || _a === void 0 ? void 0 : _a.getSelectedRows();
        }
    }));
    const handleGridApiCallback = (api) => {
        gridApiRef.current = api;
    };
    return (React.createElement("div", null, React.createElement(ExportSidesheet, { entity: entity, searchObject: searchObject, fileFormat: fileFormat, numRowsToExport: numRowsToExport, errorList: errorList, activeExportJob: activeExportJob, handleFileDownload: handleFileDownload, exportSidesheetOpen: exportSidesheetOpen, handleCloseExportSidesheet: handleCloseExportSidesheet, createExport: createExport, exportLoading: exportLoading, validSearch: validSearch }), React.createElement(ApiMethodInstanceGrid, { apiMethodInstanceId: queryId, gridId: gridId, defaultSearchObject: fullDefaultSearchObject, columnDefOverrides: columnOverrides, gridHeight: gridHeight, gridOptions: {
            pagination: true,
            rowSelection: enableCheckboxOnColumns && {
                mode: 'multiRow',
                enableClickSelection: true,
                headerCheckbox: false
            }
        }, gridHeaderConfig: {
            title: gridBarTitle,
            primaryActionButton: {
                label: primaryActionLabel,
                iconName: sizedIcon,
                onClick: primaryActionOnClick
            }
        }, gridFunctions: [
            { name: 'agGridExport' },
            { name: 'openSavedViews' },
            { name: 'agGridSizeColumnsToFit' },
            { name: 'agGridAutoSizeColumns' },
            !disableGridPersistance && { name: 'restoreGridState' },
            { name: 'agGridHideShowColumns' },
            { name: 'agGridResetColumns' },
            {
                name: 'udpExport',
                params: {
                    callback: (searchObject, entityName) => exportFromGrid(searchObject, entityName)
                }
            }
        ], gridApiCallback: handleGridApiCallback })));
});
BackwardsCompatibleGrid.displayName = 'BackwardsCompatibleGrid';
//# sourceMappingURL=BackwardsCompatibleGrid.js.map