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 { NotesScreen } from "../../../notes/NotesScreen.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/UdpNotesPage/UdpNotesPage.jsx
const GRID_ID = "52904970-8669-418e-b762-febb9ab38bad";
const UdpNotesPage = (props) => {
	const { params } = props;
	const { nodeObj, manytoManyNodeObjKey } = params;
	const [notes, setNotes] = useState([]);
	const [typeOptions, setTypeOptions] = useState([]);
	const [loading, setLoading] = useState(false);
	const [sheetOpen, setSheetOpen] = useState(false);
	const [noteId, setNoteId] = 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 noteFiledForSearchApiMethod = useMemo(() => {
		return getBaseMethod(UdpEnums.Udp_Note_Filed_For, "unityBaseSearchMethod", catalogPromotedEntities);
	}, [catalogPromotedEntities]);
	const noteTypeGetAllApiMethod = useMemo(() => {
		return getBaseMethod(UdpEnums.Udp_Note_Type, "unityBaseGetAllMethod", catalogPromotedEntities);
	}, [catalogPromotedEntities]);
	const getNotes = useCallback(async () => {
		if (!nodeObj) return [];
		let notesList = [];
		setLoading(true);
		const data = { data: getSearchObject([{
			searchField: "entityBusinessKey",
			searchValue: manytoManyNodeObjKey ? nodeObj[manytoManyNodeObjKey]?.businessObjectKey : nodeObj?.businessObjectKey,
			searchOperator: "="
		}], 1, 200, true) };
		if (noteFiledForSearchApiMethod) {
			const response = await executeQueryAdHoc(data, noteFiledForSearchApiMethod.apiMethodId);
			if (response?.status === 200) {
				const pageList = response.data.pageList;
				if (pageList?.length) notesList = pageList.map((item) => item.note);
			} else handleErrorSnackbar(null, t("Could not retrieve notes"));
		} else handleErrorSnackbar(null, t("Search NoteFiledFor Method Not Promoted"));
		setLoading(false);
		setNotes(notesList);
	}, [
		noteFiledForSearchApiMethod,
		handleErrorSnackbar,
		nodeObj,
		manytoManyNodeObjKey,
		t,
		user
	]);
	const openSheet = useCallback((value) => {
		setNoteId(value?.id || "");
		setSheetOpen(true);
	}, []);
	const closeSheet = useCallback(() => {
		setNoteId("");
		setSheetOpen(false);
	}, []);
	const handleEditPage = useCallback((value) => {
		openSplitScreen({
			open: true,
			action: null,
			type: SplitScreenTypeEnums.Notes,
			params: {
				id: value?.id || "",
				reloadNotes: getNotes
			}
		});
	}, [openSplitScreen, getNotes]);
	const getType = useCallback((params) => {
		return typeOptions.find((t) => t.code === params.data.typeCode)?.name || "";
	}, [typeOptions]);
	const actionList = useMemo(() => [{
		id: 1,
		title: t("Note"),
		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 columnDefs = [
		{
			headerName: "Date",
			field: "createdOn",
			resizable: true,
			sortable: true
		},
		{
			headerName: "Subject",
			field: "subject",
			resizable: true,
			sortable: true
		},
		{
			headerName: "Note",
			field: "content",
			resizable: true,
			sortable: true,
			valueGetter: (params) => params.data.content?.replace(/(<([^>]+)>)/gi, "") || ""
		},
		{
			headerName: "Type",
			field: "typeCode",
			resizable: true,
			sortable: true,
			valueGetter: getType
		},
		{
			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(() => {
		getNotes();
	}, []);
	useEffect(() => {
		const getTypeOptions = async () => {
			let options = [];
			if (noteTypeGetAllApiMethod) {
				const typesResponse = await executeQueryAdHoc({ data: {} }, noteTypeGetAllApiMethod.apiMethodId);
				if (typesResponse?.status === 200) options = typesResponse.data;
			}
			setTypeOptions(options);
		};
		getTypeOptions();
	}, [noteTypeGetAllApiMethod]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(SideSheet, {
		open: sheetOpen,
		onClose: closeSheet,
		title: t("Note"),
		width: 600
	}, /* @__PURE__ */ React.createElement(NotesScreen, {
		viewOnly: true,
		id: noteId,
		shortcutContentFinderEnabled: false,
		formRef
	})), /* @__PURE__ */ React.createElement(ClientSideUdpGrid, {
		gridId: GRID_ID,
		rowData: notes,
		loading,
		columnDefs,
		gridHeaderConfig: { title: t("Notes") },
		gridFunctions,
		gridOptions,
		gridApiCallback: handleGridApiCallback
	}));
};
//#endregion
export { UdpNotesPage as default };

//# sourceMappingURL=UdpNotesPage.mjs.map