import { SplitScreenTypeEnums, UdpEnums } from "../../../enums/unitySystemEnums.mjs";
import { useHandleAxiosSnackbar } from "../../../utilities/apiHelpers.mjs";
import { useUser } from "../../../utilities/auth/useUser.mjs";
import { useActionState, useActions } from "../../../hooks/useActions.mjs";
import { getSearchObject } from "../../../utilities/search/SearchUtilities.mjs";
import { SideSheet } from "../../../UI/surfaces/SideSheet.mjs";
import { executeQueryAdHoc, getBaseMethod } from "../../utilities/methodUtils.mjs";
import { FILE_TO_TYPES } from "../../../constants/reminderConstants.mjs";
import { RemindersScreen } from "../../../reminders/RemindersScreen.mjs";
import { ClientSideUdpGrid } from "../../../UI/templates/newGrid/ClientSideUdpGrid.mjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpRemindersPage/UdpRemindersPage.jsx
const GRID_ID = "aac12169-8f86-4a03-b68b-f6d87ac9a2df";
const UdpRemindersPage = (props) => {
	const { params } = props;
	const { nodeObj, manytoManyNodeObjKey } = params;
	const [reminders, setReminders] = useState([]);
	const [typeOptions, setTypeOptions] = useState([]);
	const [loading, setLoading] = useState(false);
	const [sheetOpen, setSheetOpen] = useState(false);
	const [reminderId, setReminderId] = useState("");
	const { openSplitScreen } = useActions();
	const { catalogPromotedEntities } = useActionState();
	const { handleErrorSnackbar } = useHandleAxiosSnackbar();
	const { t } = useTranslation();
	const formRef = useRef(null);
	const gridApiRef = useRef(null);
	const user = useUser();
	const reminderFiledForSearchApiMethod = useMemo(() => {
		return getBaseMethod(UdpEnums.Udp_Reminder_Filed_For, "unityBaseSearchMethod", catalogPromotedEntities);
	}, [catalogPromotedEntities]);
	const reminderTypeGetAllApiMethod = useMemo(() => {
		return getBaseMethod(UdpEnums.Udp_Reminder_Type, "unityBaseGetAllMethod", catalogPromotedEntities);
	}, [catalogPromotedEntities]);
	const getReminders = useCallback(async () => {
		if (!nodeObj) return [];
		let remindersList = [];
		setLoading(true);
		const data = { data: getSearchObject([{
			searchField: "entityBusinessKey",
			searchValue: manytoManyNodeObjKey ? nodeObj[manytoManyNodeObjKey]?.businessObjectKey : nodeObj?.businessObjectKey,
			searchOperator: "="
		}], 1, 200, true) };
		if (reminderFiledForSearchApiMethod) {
			const response = await executeQueryAdHoc(data, reminderFiledForSearchApiMethod.apiMethodId);
			if (response?.status === 200) {
				const pageList = response.data.pageList;
				if (pageList?.length) remindersList = pageList.map((item) => ({
					...item.reminder,
					filedToType: item.type
				}));
			} else handleErrorSnackbar(null, t("Could not retrieve reminders"));
		} else handleErrorSnackbar(null, t("Search ReminderFiledFor Method Not Promoted"));
		setLoading(false);
		setReminders(remindersList);
	}, [
		reminderFiledForSearchApiMethod,
		handleErrorSnackbar,
		nodeObj,
		manytoManyNodeObjKey,
		t,
		user
	]);
	const openSheet = useCallback((value) => {
		setReminderId(value?.id || "");
		setSheetOpen(true);
	}, []);
	const closeSheet = useCallback(() => {
		setReminderId("");
		setSheetOpen(false);
	}, []);
	const handleEditPage = useCallback((value) => {
		openSplitScreen({
			open: true,
			action: null,
			type: SplitScreenTypeEnums.Reminders,
			params: {
				id: value?.id || "",
				reloadReminders: getReminders
			}
		});
	}, [openSplitScreen, getReminders]);
	const actionList = useMemo(() => [{
		id: 1,
		title: t("Reminder"),
		iconName: "view",
		handleOnClick: (e, params) => openSheet(params?.data),
		disabled: false
	}, {
		id: 2,
		title: t("Edit"),
		iconName: "edit",
		handleOnClick: (e, params) => handleEditPage(params?.data),
		disabled: false
	}], [
		t,
		openSheet,
		handleEditPage
	]);
	const getFiledToType = (params) => {
		return FILE_TO_TYPES.find((type) => type.value === params.data.filedToType)?.label || "";
	};
	const columnDefs = [
		{
			headerName: "Date",
			field: "createdOn",
			resizable: true,
			sortable: true
		},
		{
			headerName: "Subject",
			field: "subject",
			resizable: true,
			sortable: true
		},
		{
			headerName: "Reminder",
			field: "content",
			resizable: true,
			sortable: true,
			valueGetter: (params) => params.data.content?.replace(/(<([^>]+)>)/gi, "") || ""
		},
		{
			headerName: "Reminder Date",
			field: "reminderDate",
			resizable: true,
			sortable: true
		},
		{
			headerName: "Type",
			field: "typeCode",
			resizable: true,
			sortable: true,
			valueGetter: useCallback((params) => {
				return typeOptions.find((t) => t.code === params.data.typeCode)?.name || "";
			}, [typeOptions])
		},
		{
			headerName: "Filed To Type",
			field: "filedToType",
			resizable: true,
			sortable: true,
			valueGetter: getFiledToType
		},
		{
			headerName: "Created By",
			field: "userDisplayName",
			resizable: true,
			sortable: true
		},
		{
			headerName: "",
			cellRenderer: "actionsRenderer",
			cellRendererParams: { actionList },
			pinned: "right",
			suppressMenu: true,
			sortable: false
		}
	];
	const gridFunctions = [
		{ name: "quickFilter" },
		{ name: "agGridExport" },
		{ name: "agGridSizeColumnsToFit" },
		{ name: "agGridAutoSizeColumns" },
		{ name: "agGridResetColumns" },
		{ name: "agGridHideShowColumns" },
		{ name: "openSavedViews" }
	];
	const gridOptions = useMemo(() => {
		return { pagination: true };
	}, []);
	const handleGridApiCallback = (api) => {
		gridApiRef.current = api;
	};
	useEffect(() => {
		getReminders();
	}, []);
	useEffect(() => {
		const getTypeOptions = async () => {
			let options = [];
			if (reminderTypeGetAllApiMethod) {
				const typesResponse = await executeQueryAdHoc({ data: {} }, reminderTypeGetAllApiMethod.apiMethodId);
				if (typesResponse?.status === 200) options = typesResponse.data;
			}
			setTypeOptions(options);
		};
		getTypeOptions();
	}, [reminderTypeGetAllApiMethod]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(SideSheet, {
		open: sheetOpen,
		onClose: closeSheet,
		title: t("Reminder"),
		width: 600
	}, /* @__PURE__ */ React.createElement(RemindersScreen, {
		viewOnly: true,
		id: reminderId,
		shortcutContentFinderEnabled: false,
		formRef
	})), /* @__PURE__ */ React.createElement(ClientSideUdpGrid, {
		gridId: GRID_ID,
		rowData: reminders,
		loading,
		columnDefs,
		gridHeaderConfig: { title: t("Reminders") },
		gridFunctions,
		gridOptions,
		gridApiCallback: handleGridApiCallback
	}));
};
//#endregion
export { UdpRemindersPage as default };

//# sourceMappingURL=UdpRemindersPage.mjs.map