import { LicenseKey } from "../../AGLicense.mjs";
import { SplitButton } from "./SplitButton.mjs";
import { c } from "react-compiler-runtime";
import React, { useCallback, useMemo, useReducer } from "react";
import { Box, Button, Grid, MenuItem, makeStyles } from "@material-ui/core";
import clsx from "clsx";
import invariant from "invariant";
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 { LicenseManager } from "@ag-grid-enterprise/core";
//#region src/utilities/aggrid/AgTable.tsx
LicenseManager.setLicenseKey(LicenseKey);
const useStyles = makeStyles((theme) => ({
	root: {
		flex: 1,
		height: (props) => props?.height ? props.height : "calc(100vh - 280px)"
	},
	AgGrid: (props) => ({
		"--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
*/
function useAgGridApi() {
	const $ = c(4);
	let t0;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t0 = {};
		$[0] = t0;
	} else t0 = $[0];
	const [t1, onGridReady] = useReducer(_temp, t0);
	const { gridApi, gridColumnApi } = t1;
	let t2;
	if ($[1] !== gridApi || $[2] !== gridColumnApi) {
		t2 = {
			gridApi,
			gridColumnApi,
			onGridReady
		};
		$[1] = gridApi;
		$[2] = gridColumnApi;
		$[3] = t2;
	} else t2 = $[3];
	return t2;
}
function _temp(state, params) {
	return {
		gridApi: params.api,
		gridColumnApi: params.columnApi
	};
}
/**
* 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.
*/
const AgTableButtons = (props) => {
	const $ = c(32);
	const { gridApi, gridColumnApi } = props;
	let t0;
	if ($[0] !== gridApi) {
		t0 = () => {
			invariant(gridApi, "gridApi not ready");
			gridApi?.exportDataAsCsv({
				allColumns: true,
				fileName: "export"
			});
		};
		$[0] = gridApi;
		$[1] = t0;
	} else t0 = $[1];
	const exportToCSV = t0;
	let t1;
	if ($[2] !== gridApi) {
		t1 = () => {
			invariant(gridApi, "gridApi not ready");
			gridApi?.exportDataAsExcel({
				allColumns: true,
				fileName: "export"
			});
		};
		$[2] = gridApi;
		$[3] = t1;
	} else t1 = $[3];
	const exportToExcel = t1;
	let t2;
	if ($[4] !== gridApi) {
		t2 = (params) => {
			invariant(gridApi, "gridApi not ready");
			gridApi?.sizeColumnsToFit();
		};
		$[4] = gridApi;
		$[5] = t2;
	} else t2 = $[5];
	const sizeToFit = t2;
	let t3;
	if ($[6] !== gridColumnApi) {
		t3 = (skipHeader) => {
			invariant(gridColumnApi, "gridColumnApi not ready");
			const allColumnIds = [];
			gridColumnApi?.getAllColumns()?.forEach((column) => {
				allColumnIds.push(column.getColId());
			});
			gridColumnApi?.autoSizeColumns(allColumnIds, skipHeader);
		};
		$[6] = gridColumnApi;
		$[7] = t3;
	} else t3 = $[7];
	const autoSizeAll = t3;
	let t4;
	if ($[8] !== autoSizeAll) {
		t4 = () => autoSizeAll(false);
		$[8] = autoSizeAll;
		$[9] = t4;
	} else t4 = $[9];
	const autoSizeAllWithHeader = t4;
	let t5;
	if ($[10] !== autoSizeAll) {
		t5 = () => autoSizeAll(false);
		$[10] = autoSizeAll;
		$[11] = t5;
	} else t5 = $[11];
	const autoSizeAllWithoutHeader = t5;
	let t6;
	if ($[12] !== sizeToFit) {
		t6 = /* @__PURE__ */ React.createElement(Grid, { item: true }, /* @__PURE__ */ React.createElement(Button, {
			variant: "outlined",
			size: "small",
			onClick: sizeToFit
		}, "Size to Fit"));
		$[12] = sizeToFit;
		$[13] = t6;
	} else t6 = $[13];
	let t7;
	if ($[14] !== autoSizeAllWithHeader) {
		t7 = /* @__PURE__ */ React.createElement(MenuItem, { onClick: autoSizeAllWithHeader }, "All");
		$[14] = autoSizeAllWithHeader;
		$[15] = t7;
	} else t7 = $[15];
	let t8;
	if ($[16] !== autoSizeAllWithoutHeader) {
		t8 = /* @__PURE__ */ React.createElement(MenuItem, { onClick: autoSizeAllWithoutHeader }, "Skip Header");
		$[16] = autoSizeAllWithoutHeader;
		$[17] = t8;
	} else t8 = $[17];
	let t9;
	if ($[18] !== t7 || $[19] !== t8) {
		t9 = /* @__PURE__ */ React.createElement(Grid, { item: true }, /* @__PURE__ */ React.createElement(SplitButton, {
			popupName: "auto-size",
			variant: "outlined",
			size: "small",
			label: "Auto-Size"
		}, t7, t8));
		$[18] = t7;
		$[19] = t8;
		$[20] = t9;
	} else t9 = $[20];
	let t10;
	if ($[21] !== exportToExcel) {
		t10 = /* @__PURE__ */ React.createElement(MenuItem, { onClick: exportToExcel }, "Excel");
		$[21] = exportToExcel;
		$[22] = t10;
	} else t10 = $[22];
	let t11;
	if ($[23] !== exportToCSV) {
		t11 = /* @__PURE__ */ React.createElement(MenuItem, { onClick: exportToCSV }, "CSV");
		$[23] = exportToCSV;
		$[24] = t11;
	} else t11 = $[24];
	let t12;
	if ($[25] !== t10 || $[26] !== t11) {
		t12 = /* @__PURE__ */ React.createElement(Grid, { item: true }, /* @__PURE__ */ React.createElement(SplitButton, {
			popupName: "export",
			variant: "outlined",
			size: "small",
			label: "Export"
		}, t10, t11));
		$[25] = t10;
		$[26] = t11;
		$[27] = t12;
	} else t12 = $[27];
	let t13;
	if ($[28] !== t12 || $[29] !== t6 || $[30] !== t9) {
		t13 = /* @__PURE__ */ React.createElement(Box, { mb: 1 }, /* @__PURE__ */ React.createElement(Grid, {
			container: true,
			direction: "row",
			spacing: 1,
			justify: "flex-end"
		}, t6, t9, t12));
		$[28] = t12;
		$[29] = t6;
		$[30] = t9;
		$[31] = t13;
	} else t13 = $[31];
	return t13;
};
/**
* 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.
*/
const AgTable = (props) => {
	const classes = useStyles(props);
	const { modules: modulesProp, className, getRowHeight, rowModelType, isUsingAgRowHeight = false, hideHeader = false } = 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_0) => {
		if (rowModelType === "serverSide") if (isUsingAgRowHeight) return;
		else if (getRowHeight) return getRowHeight(params_0);
		else return calculateRowHeight(params_0);
		else if (getRowHeight) return getRowHeight(params_0);
	}, [getRowHeight, isUsingAgRowHeight]);
	const onGridSizeChanged = () => {
		if (props.dynamicSizing) props.api?.sizeColumnsToFit();
	};
	return /* @__PURE__ */ React.createElement("div", { className: clsx("ag-theme-material", classes.root, className) }, /* @__PURE__ */ React.createElement(AgGridReact, {
		...props,
		modules,
		onGridSizeChanged,
		getRowHeight: rowHeightFunction,
		className: classes.AgGrid
	}));
};
//#endregion
export { AgTable, AgTableButtons, useAgGridApi };

//# sourceMappingURL=AgTable.mjs.map