import { UdpEnums } from "../../../enums/unitySystemEnums.mjs";
import { useHandleAxiosSnackbar } from "../../../utilities/apiHelpers.mjs";
import { useCatalogStore } from "../../../stores/catalogStore.mjs";
import { executeQueryAdHoc, getBaseMethod } from "../../utilities/methodUtils.mjs";
import { useAgGridApi } from "../../../utilities/aggrid/AgTable.mjs";
import { LinearProgress as LinearProgress$1 } from "../../../UI/loading/LinearProgress.mjs";
import { SearchMethodUdpGrid } from "../../../UI/templates/newGrid/SearchMethodUdpGrid.mjs";
import UdpAuditEventSidesheet from "./UdpAuditEventSidesheet.mjs";
import { useDateStringColumnDef } from "../../utilities/useDateStringColumnDef.mjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { makeStyles } from "@material-ui/core";
import "@fluentui/react-icons";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpAuditEvents/UdpAuditEventsPage.jsx
const baseSearchPayload = {
	pageNumber: 1,
	pageSize: 15,
	filterElements: [],
	filterGroups: [],
	orderElements: [],
	groupingType: "",
	groupProperty: [],
	groupOperationList: [],
	eagerLoad: false,
	logicalSearchOperator: 1
};
const useStyles = makeStyles((theme) => ({ loadingBar: {
	bottom: theme.spacing(-.5),
	marginLeft: theme.spacing(.5),
	marginRight: theme.spacing(.5)
} }));
const UdpAuditEventsPage = (props) => {
	const { params } = props;
	const { gridApi, gridColumnApi, onGridReady } = useAgGridApi();
	const { t } = useTranslation();
	const classes = useStyles();
	const dateStringColumnDef = useDateStringColumnDef(true);
	const { handleErrorSnackbar } = useHandleAxiosSnackbar();
	const gridApiRef = useRef(null);
	const [searchQuery, setSearchQuery] = useState("");
	const [displayProgressBar, setDisplayProgressBar] = useState(false);
	const [selectedAuditEvent, setSelectedAuditEvent] = useState(null);
	const [viewSideSheetOpen, setViewSideSheetOpen] = useState(false);
	const [auditEventTypes, setAuditEventTypes] = useState([]);
	const [searchObject, setSearchObject] = useState(baseSearchPayload);
	const currentApiCatalogId = useCatalogStore((state) => state.currentApiCatalogId);
	const promotedMethodEntitiesMap = useCatalogStore((state_0) => state_0.promotedMethodEntitiesMap);
	const promotedEntities = useMemo(() => {
		if (!currentApiCatalogId || !promotedMethodEntitiesMap) return null;
		return promotedMethodEntitiesMap[currentApiCatalogId] || [];
	}, [promotedMethodEntitiesMap, currentApiCatalogId]);
	const auditEventTypeGetAllApiMethod = useMemo(() => {
		return getBaseMethod(UdpEnums.Udp_Audit_Event_Type, "unityBaseGetAllMethod", promotedEntities);
	}, [promotedEntities]);
	const auditEventSearchApiMethod = useMemo(() => {
		return getBaseMethod(UdpEnums.Udp_Audit_Event, "unityBaseSearchMethod", promotedEntities);
	}, [promotedEntities]);
	const getAuditEventTypeName = useCallback((auditEventTypeCode) => {
		if (!auditEventTypes) return "";
		return auditEventTypes.find((auditEventType) => auditEventType.code === auditEventTypeCode)?.name || "";
	}, [auditEventTypes]);
	const handleSideSheet = useCallback((isOpen, auditEvent) => {
		setViewSideSheetOpen(isOpen);
		setSelectedAuditEvent(auditEvent);
	}, []);
	const columnDefs = useMemo(() => {
		return [
			{
				headerName: t("Event Type"),
				field: "UdpAuditEventUdpAuditEventType.Name",
				valueGetter: (params_0) => getAuditEventTypeName(params_0.data.typeCode),
				resizable: true,
				sortable: false
			},
			{
				headerName: t("User"),
				field: "userDisplayName",
				resizable: true,
				sortable: false
			},
			{
				headerName: t("Description"),
				field: "domain",
				resizable: true,
				sortable: false
			},
			{
				headerName: t("Created On"),
				field: "createdOn",
				resizable: true,
				sortable: true,
				...dateStringColumnDef
			},
			{
				field: "actions",
				headerName: t("Details"),
				cellRenderer: "actionsRenderer",
				cellRendererParams: { actionList: [{
					id: 0,
					title: t("View"),
					iconName: "view",
					handleOnClick: (e, auditEvent_0) => handleSideSheet(true, {
						...auditEvent_0?.data,
						auditEventTypeName: getAuditEventTypeName(auditEvent_0?.data?.typeCode)
					}),
					disabled: false
				}] },
				suppressMenu: true,
				sortable: false,
				filter: false
			}
		];
	}, [getAuditEventTypeName]);
	const gridFunctions = [
		{ name: "chipFilter" },
		{ name: "agGridExport" },
		{ name: "agGridSizeColumnsToFit" },
		{ name: "agGridAutoSizeColumns" },
		{ name: "agGridResetColumns" },
		{ name: "agGridHideShowColumns" }
	];
	const gridOptions = useMemo(() => {
		return { pagination: true };
	}, []);
	useEffect(() => {
		const getAuditEventTypes = async () => {
			let types = [];
			if (auditEventTypeGetAllApiMethod) {
				const response = await executeQueryAdHoc({ data: {} }, auditEventTypeGetAllApiMethod.apiMethodId);
				if (response?.status === 200) types = response.data;
			}
			setAuditEventTypes(types);
		};
		getAuditEventTypes();
	}, [auditEventTypeGetAllApiMethod]);
	const handleGridApiCallback = (api) => {
		gridApiRef.current = api;
	};
	const searchMethodConfig = useMemo(() => {
		return {
			apiMethodId: auditEventSearchApiMethod?.apiMethodId,
			customSearchObject: searchObject
		};
	}, [auditEventSearchApiMethod]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, displayProgressBar && /* @__PURE__ */ React.createElement(LinearProgress$1, {
		className: classes.loadingBar,
		color: "secondary"
	}), /* @__PURE__ */ React.createElement(SearchMethodUdpGrid, {
		searchMethodConfig,
		columnDefs,
		gridHeaderConfig: { title: t("Audit Events") },
		gridOptions,
		gridApiCallback: handleGridApiCallback,
		gridFunctions
	}), /* @__PURE__ */ React.createElement(UdpAuditEventSidesheet, {
		auditEvent: selectedAuditEvent,
		isSideSheetOpen: viewSideSheetOpen,
		handleSideSheet,
		promotedEntities
	}));
};
//#endregion
export { UdpAuditEventsPage as default };

//# sourceMappingURL=UdpAuditEventsPage.mjs.map