import { AllModules } from '@ag-grid-enterprise/all-modules';
import '@ag-grid-enterprise/all-modules/dist/styles/ag-grid.css';
import '@ag-grid-enterprise/all-modules/dist/styles/ag-theme-material.css';
import './ag-grid-styles.css';
import { AgGridReact } from '@ag-grid-community/react';
import { ExcelExportModule } from '@ag-grid-enterprise/excel-export';
import { Box, Button, Grid, makeStyles, MenuItem } from '@material-ui/core';
import clsx from 'clsx';
import invariant from 'invariant';
import React, { useCallback, useMemo, useReducer } from 'react';
import { SplitButton } from './SplitButton';
import { LicenseManager } from '@ag-grid-enterprise/core';
import { LicenseKey } from '../../AGLicense';
LicenseManager.setLicenseKey(LicenseKey);
const useStyles = makeStyles((theme) => ({
    root: {
        flex: 1,
        height: (props) => (props === null || props === void 0 ? void 0 : props.height) ? props.height : 'calc(100vh - 280px)'
    },
    AgGrid: (props) => (Object.assign({ '--ag-control-panel-background-color': theme.palette.primary.light, '--ag-checkbox-checked-color': theme.palette.primary.main }, (props.hideHeader ? {} : {
        '--ag-header-background-color': theme.palette.primary.main,
        '--ag-header-foreground-color': theme.palette.primary.light,
    })))
}), { name: 'AgTable' });
const globalModules = [...AllModules, ExcelExportModule];
/**
 * Utility hook for getting the ag-grid APIs into state
 */
export function useAgGridApi() {
    const [{ gridApi, gridColumnApi }, onGridReady] = useReducer((state, params) => {
        return { gridApi: params.api, gridColumnApi: params.columnApi };
    }, {});
    return { gridApi, gridColumnApi, onGridReady };
}
/**
 * Size/Export buttons for AgGrid tables.
 *
 * @deprecated Tightly coupled to the legacy `AgTable` component. The new UDP
 * grids expose equivalent functionality via `gridFunctions` — see the
 * `udp-grid-conversion` skill.
 */
export const AgTableButtons = (props) => {
    const { gridApi, gridColumnApi } = props;
    const exportToCSV = useCallback(() => {
        invariant(gridApi, `gridApi not ready`);
        gridApi === null || gridApi === void 0 ? void 0 : gridApi.exportDataAsCsv({
            allColumns: true,
            fileName: 'export'
        });
    }, [gridApi]);
    const exportToExcel = useCallback(() => {
        invariant(gridApi, `gridApi not ready`);
        gridApi === null || gridApi === void 0 ? void 0 : gridApi.exportDataAsExcel({
            allColumns: true,
            fileName: 'export'
        });
    }, [gridApi]);
    const sizeToFit = useCallback((params) => {
        invariant(gridApi, `gridApi not ready`);
        gridApi === null || gridApi === void 0 ? void 0 : gridApi.sizeColumnsToFit();
    }, [gridApi]);
    const autoSizeAll = useCallback((skipHeader) => {
        var _a;
        invariant(gridColumnApi, `gridColumnApi not ready`);
        const allColumnIds = [];
        (_a = gridColumnApi === null || gridColumnApi === void 0 ? void 0 : gridColumnApi.getAllColumns()) === null || _a === void 0 ? void 0 : _a.forEach((column) => {
            allColumnIds.push(column.getColId());
        });
        gridColumnApi === null || gridColumnApi === void 0 ? void 0 : gridColumnApi.autoSizeColumns(allColumnIds, skipHeader);
    }, [gridColumnApi]);
    const autoSizeAllWithHeader = useCallback(() => autoSizeAll(false), [autoSizeAll]);
    const autoSizeAllWithoutHeader = useCallback(() => autoSizeAll(false), [autoSizeAll]);
    return (
    // @ts-ignore
    React.createElement(Box, { mb: 1 }, React.createElement(Grid, { container: true, direction: "row", spacing: 1, justify: "flex-end" }, React.createElement(Grid, { item: true }, React.createElement(Button, { variant: "outlined", size: "small", onClick: sizeToFit }, "Size to Fi")), React.createElement(Grid, { item: true }, React.createElement(SplitButton, { popupName: "auto-size", variant: "outlined", size: "small", label: "Auto-Size" }, React.createElement(MenuItem, { onClick: autoSizeAllWithHeader }, "All"), React.createElement(MenuItem, { onClick: autoSizeAllWithoutHeader }, "Skip Header"))), React.createElement(Grid, { item: true }, React.createElement(SplitButton, { popupName: "export", variant: "outlined", size: "small", label: "Export" }, React.createElement(MenuItem, { onClick: exportToExcel }, "Excel"), React.createElement(MenuItem, { onClick: exportToCSV }, "CSV"))))));
};
/**
 * AgGrid with tweaks.
 *
 * @deprecated Use the new UDP grid components instead — `ClientSideUdpGrid`,
 * `SearchMethodUdpGrid`, `ServerSideUdpGrid`, or `ApiMethodInstanceUdpGrid`.
 * See the `udp-grid-conversion` skill for migration guidance.
 */
export const AgTable = (props) => {
    const classes = useStyles(props);
    const { modules: modulesProp, className, getRowHeight, rowModelType, isUsingAgRowHeight = false, hideHeader = false // Add the hideHeader prop to the destructuring
     } = props;
    const modules = useMemo(() => (modulesProp ? globalModules.concat(modulesProp) : globalModules), [modulesProp]);
    const calculateRowHeight = (params) => {
        if (params.node.level === 0) {
            return 48;
        }
        if (params.node.level === 1) {
            return 32;
        }
        return 16;
    };
    const rowHeightFunction = useCallback((params) => {
        if (rowModelType === 'serverSide') {
            if (isUsingAgRowHeight) {
                return undefined;
            }
            else {
                if (getRowHeight) {
                    return getRowHeight(params);
                }
                else {
                    return calculateRowHeight(params);
                }
            }
        }
        else {
            if (getRowHeight) {
                return getRowHeight(params);
            }
        }
        return undefined;
    }, [getRowHeight, isUsingAgRowHeight]);
    const onGridSizeChanged = () => {
        var _a;
        if (props.dynamicSizing) {
            (_a = props.api) === null || _a === void 0 ? void 0 : _a.sizeColumnsToFit();
        }
    };
    return (React.createElement("div", { className: clsx('ag-theme-material', classes.root, className) }, React.createElement(AgGridReact, Object.assign({}, props, { modules: modules, onGridSizeChanged: onGridSizeChanged, getRowHeight: rowHeightFunction, className: classes.AgGrid }))));
};
//# sourceMappingURL=AgTable.js.map