import { ConfigService } from "../../../../../configService/index.mjs";
import { apiMutate, useAxiosMutate } from "../../../../../utilities/useAxiosMutate.mjs";
import { SideSheet } from "../../../../../UI/surfaces/SideSheet.mjs";
import { LinearProgress as LinearProgress$1 } from "../../../../../UI/loading/LinearProgress.mjs";
import { EmptyStateDisplay } from "../../../../../UI/dataDisplay/EmptyStateDisplay.mjs";
import { TransactionNote } from "./TransactionNote.mjs";
import { TransactionNoteForm } from "./TransactionNoteForm.mjs";
import { c } from "react-compiler-runtime";
import React, { useEffect, useState } from "react";
import { makeStyles } from "@material-ui/core";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpTransactionsPage/components/transactionNote/TransactionNotes.jsx
const useStyles = makeStyles((theme) => ({ notes: {
	display: "flex",
	flexDirection: "column",
	gap: theme.spacing(1)
} }));
const TransactionNotes = (props) => {
	const $ = c(42);
	const { businessObjectKey, refreshNotes, setRefreshNotes, noteTypes, fetchNoteTypesLoading } = props;
	const [openSideSheet, setOpenSideSheet] = useState(false);
	const [selectedNote, setSelectedNote] = useState(null);
	let t0;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t0 = [];
		$[0] = t0;
	} else t0 = $[0];
	const [noteList, setNoteList] = useState(t0);
	const classes = useStyles();
	const { t } = useTranslation();
	const [isLoading, setIsLoading] = useState(false);
	let t1;
	if ($[1] !== businessObjectKey) {
		t1 = {
			eagerLoad: true,
			pageNumber: 0,
			pageSize: 100,
			filterElements: [{
				searchField: "entityBusinessKey",
				searchValue: businessObjectKey,
				searchOperator: "="
			}],
			getAll: true
		};
		$[1] = businessObjectKey;
		$[2] = t1;
	} else t1 = $[2];
	const noteFileForSearchObj = t1;
	let t2;
	if ($[3] !== noteFileForSearchObj) {
		t2 = {
			method: "post",
			data: noteFileForSearchObj
		};
		$[3] = noteFileForSearchObj;
		$[4] = t2;
	} else t2 = $[4];
	const [t3, fetchNoteFiledFor] = useAxiosMutate(ConfigService.productV1ApiUrl, "UdpNoteFiledFor/search", t2);
	const { data: noteFiledFor, loading: fetchNoteFiledForLoading } = t3;
	let t4;
	if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
		t4 = (note) => {
			setSelectedNote(note);
			setOpenSideSheet(true);
		};
		$[5] = t4;
	} else t4 = $[5];
	const handleViewNote = t4;
	let t5;
	if ($[6] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = () => {
			setOpenSideSheet(false);
			setSelectedNote(null);
		};
		$[6] = t5;
	} else t5 = $[6];
	const handleCloseSideSheet = t5;
	let t6;
	let t7;
	if ($[7] !== fetchNoteFiledFor) {
		t6 = () => {
			fetchNoteFiledFor();
		};
		t7 = [fetchNoteFiledFor];
		$[7] = fetchNoteFiledFor;
		$[8] = t6;
		$[9] = t7;
	} else {
		t6 = $[8];
		t7 = $[9];
	}
	useEffect(t6, t7);
	let t8;
	let t9;
	if ($[10] !== fetchNoteFiledFor || $[11] !== refreshNotes || $[12] !== setRefreshNotes) {
		t8 = () => {
			if (refreshNotes) {
				fetchNoteFiledFor();
				setRefreshNotes(false);
			}
		};
		t9 = [
			fetchNoteFiledFor,
			refreshNotes,
			setRefreshNotes
		];
		$[10] = fetchNoteFiledFor;
		$[11] = refreshNotes;
		$[12] = setRefreshNotes;
		$[13] = t8;
		$[14] = t9;
	} else {
		t8 = $[13];
		t9 = $[14];
	}
	useEffect(t8, t9);
	let t10;
	if ($[15] !== fetchNoteFiledForLoading || $[16] !== noteFiledFor?.pageList) {
		t10 = () => {
			if (!fetchNoteFiledForLoading && noteFiledFor?.pageList?.length > 0) {
				const searchObj = {
					eagerLoad: true,
					pageNumber: 0,
					pageSize: 100,
					filterElements: [{
						searchField: "iD",
						searchValue: noteFiledFor?.pageList?.map(_temp).join(","),
						searchOperator: "IN"
					}],
					getAll: true
				};
				setIsLoading(true);
				apiMutate(ConfigService.productV1ApiUrl, "UdpNote/search", {
					method: "post",
					data: searchObj
				}, {}).then((resp) => {
					if (resp && resp.data) setNoteList(resp.data?.pageList);
					else setNoteList([]);
				}).finally(() => {
					setIsLoading(false);
				});
			} else setNoteList([]);
		};
		$[15] = fetchNoteFiledForLoading;
		$[16] = noteFiledFor?.pageList;
		$[17] = t10;
	} else t10 = $[17];
	let t11;
	if ($[18] !== fetchNoteFiledForLoading || $[19] !== noteFiledFor) {
		t11 = [
			fetchNoteFiledForLoading,
			noteFiledFor,
			setIsLoading
		];
		$[18] = fetchNoteFiledForLoading;
		$[19] = noteFiledFor;
		$[20] = t11;
	} else t11 = $[20];
	useEffect(t10, t11);
	let t12;
	if ($[21] !== fetchNoteFiledForLoading || $[22] !== isLoading || $[23] !== noteList) {
		t12 = isLoading || fetchNoteFiledForLoading ? /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(LinearProgress$1, null)) : noteList?.length > 0 ? noteList.map((note_0, index) => /* @__PURE__ */ React.createElement(TransactionNote, {
			key: index,
			note: note_0,
			onClick: () => handleViewNote(note_0)
		})) : /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(EmptyStateDisplay, { primaryText: "No Notes" }));
		$[21] = fetchNoteFiledForLoading;
		$[22] = isLoading;
		$[23] = noteList;
		$[24] = t12;
	} else t12 = $[24];
	let t13;
	if ($[25] !== classes.notes || $[26] !== t12) {
		t13 = /* @__PURE__ */ React.createElement("div", { className: classes.notes }, t12);
		$[25] = classes.notes;
		$[26] = t12;
		$[27] = t13;
	} else t13 = $[27];
	let t14;
	if ($[28] !== t) {
		t14 = t("View Note");
		$[28] = t;
		$[29] = t14;
	} else t14 = $[29];
	let t15;
	if ($[30] !== businessObjectKey || $[31] !== fetchNoteTypesLoading || $[32] !== noteTypes || $[33] !== selectedNote) {
		t15 = /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(TransactionNoteForm, {
			initialValues: selectedNote,
			businessObjectKey,
			viewOnly: true,
			noteTypes,
			fetchNoteTypeLoading: fetchNoteTypesLoading
		}));
		$[30] = businessObjectKey;
		$[31] = fetchNoteTypesLoading;
		$[32] = noteTypes;
		$[33] = selectedNote;
		$[34] = t15;
	} else t15 = $[34];
	let t16;
	if ($[35] !== openSideSheet || $[36] !== t14 || $[37] !== t15) {
		t16 = /* @__PURE__ */ React.createElement(SideSheet, {
			title: t14,
			width: 600,
			open: openSideSheet,
			onClose: handleCloseSideSheet
		}, t15);
		$[35] = openSideSheet;
		$[36] = t14;
		$[37] = t15;
		$[38] = t16;
	} else t16 = $[38];
	let t17;
	if ($[39] !== t13 || $[40] !== t16) {
		t17 = /* @__PURE__ */ React.createElement("div", null, t13, t16);
		$[39] = t13;
		$[40] = t16;
		$[41] = t17;
	} else t17 = $[41];
	return t17;
};
function _temp(obj) {
	return obj?.noteID;
}
//#endregion
export { TransactionNotes };

//# sourceMappingURL=TransactionNotes.mjs.map