import { Form } from "../../../../../utilities/form/Form.mjs";
import { Field } from "../../../../../utilities/form/Field.mjs";
import { ConfigService } from "../../../../../configService/index.mjs";
import { useHandleAxiosSnackbar } from "../../../../../utilities/apiHelpers.mjs";
import { apiMutate } from "../../../../../utilities/useAxiosMutate.mjs";
import { FluentTextFieldAutoComplete } from "../../../../../UI/inputs/textField/FluentTextFieldAutoComplete.mjs";
import { Typography as Typography$1 } from "../../../../../UI/dataDisplay/typography/Typography.mjs";
import { LinearProgress as LinearProgress$1 } from "../../../../../UI/loading/LinearProgress.mjs";
import { FluentTextField } from "../../../../../UI/inputs/textField/FluentTextField.mjs";
import { useAuthedUser } from "../../../../../utilities/auth/useAuthedUser.mjs";
import { c } from "react-compiler-runtime";
import React, { forwardRef, useState } from "react";
import { makeStyles } from "@material-ui/core";
import { useTranslation } from "react-i18next";
//#region src/udp/pages/UdpTransactionsPage/components/transactionNote/TransactionNoteForm.jsx
const useStyles = makeStyles((theme) => ({ loading: { marginBottom: theme.spacing(1) } }));
const TransactionNoteFormComponent = (props, ref) => {
	const $ = c(19);
	const { initialValues, viewOnly, businessObjectKey, closeSideSheet, noteTypes, fetchNoteTypesLoading } = props;
	const [isLoading, setIsLoading] = useState(false);
	const user = useAuthedUser();
	const { handleSuccessSnackbar, handleErrorSnackbar } = useHandleAxiosSnackbar();
	const { t } = useTranslation();
	const classes = useStyles();
	const addTransactionNote = _temp;
	const addNoteFiledFor = _temp2;
	let t0;
	if ($[0] !== businessObjectKey || $[1] !== closeSideSheet || $[2] !== handleErrorSnackbar || $[3] !== handleSuccessSnackbar || $[4] !== t || $[5] !== user?.id || $[6] !== user?.name) {
		t0 = async (values) => {
			setIsLoading(true);
			await addTransactionNote({ data: {
				typeCode: values?.noteType?.code,
				subject: values?.subject,
				content: values?.content,
				unityUserID: user?.id,
				userDisplayName: user?.name ?? ""
			} }).then(async (res) => {
				console.log("Transaction Note response: ", { res });
				if (res?.data?.id) await addNoteFiledFor({ data: {
					entityBusinessKey: businessObjectKey,
					noteId: res?.data?.id
				} }).then(() => {
					handleSuccessSnackbar(t("Transaction note has been added successfully."));
					if (closeSideSheet) closeSideSheet();
				});
			}).catch((res_0) => {
				handleErrorSnackbar(res_0, t("Unable to add transaction note."));
			});
			setIsLoading(false);
		};
		$[0] = businessObjectKey;
		$[1] = closeSideSheet;
		$[2] = handleErrorSnackbar;
		$[3] = handleSuccessSnackbar;
		$[4] = t;
		$[5] = user?.id;
		$[6] = user?.name;
		$[7] = t0;
	} else t0 = $[7];
	const handleSubmitForm = t0;
	let t1;
	if ($[8] !== businessObjectKey || $[9] !== classes || $[10] !== fetchNoteTypesLoading || $[11] !== handleSubmitForm || $[12] !== initialValues || $[13] !== isLoading || $[14] !== noteTypes || $[15] !== ref || $[16] !== t || $[17] !== viewOnly) {
		t1 = /* @__PURE__ */ React.createElement("div", null, fetchNoteTypesLoading ? /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(LinearProgress$1, null)) : /* @__PURE__ */ React.createElement("div", null, isLoading && /* @__PURE__ */ React.createElement("div", { className: classes.loading }, /* @__PURE__ */ React.createElement(LinearProgress$1, null)), /* @__PURE__ */ React.createElement(Form, {
			ref,
			onSubmit: handleSubmitForm,
			initialValues,
			id: `transactionNoteForm-${businessObjectKey}`,
			render: (t2) => {
				const { submitting } = t2;
				return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Field, {
					component: FluentTextFieldAutoComplete,
					id: "noteType",
					name: "noteType",
					optionKey: "name",
					disableClearable: true,
					label: t("Type"),
					options: noteTypes ?? [],
					disabled: viewOnly || submitting,
					onChange: _temp3,
					required: true
				}), /* @__PURE__ */ React.createElement(Field, {
					component: FluentTextField,
					id: "subject",
					name: "subject",
					label: t("Subject"),
					disabled: viewOnly || submitting,
					onChange: _temp4,
					required: true
				}), /* @__PURE__ */ React.createElement(Field, {
					component: FluentTextField,
					id: "content",
					name: "content",
					label: t("Details"),
					onChange: _temp5,
					multiline: true,
					rows: 5,
					maxRows: 10,
					disabled: viewOnly || submitting,
					required: true
				}), viewOnly && /* @__PURE__ */ React.createElement(Typography$1, { variant: "caption" }, t("Viewing note details. Use the Add Note action to create a new entry.")));
			}
		})));
		$[8] = businessObjectKey;
		$[9] = classes;
		$[10] = fetchNoteTypesLoading;
		$[11] = handleSubmitForm;
		$[12] = initialValues;
		$[13] = isLoading;
		$[14] = noteTypes;
		$[15] = ref;
		$[16] = t;
		$[17] = viewOnly;
		$[18] = t1;
	} else t1 = $[18];
	return t1;
};
const TransactionNoteForm = forwardRef(TransactionNoteFormComponent);
async function _temp(data) {
	return apiMutate(ConfigService.productV1ApiUrl, "UdpNote", { method: "post" }, data);
}
async function _temp2(data_0) {
	return apiMutate(ConfigService.productV1ApiUrl, "UdpNoteFiledFor", { method: "post" }, data_0);
}
function _temp3() {}
function _temp4() {}
function _temp5() {}
//#endregion
export { TransactionNoteForm };

//# sourceMappingURL=TransactionNoteForm.mjs.map