import { ConfigService } from "../configService/index.mjs";
import { apiMutate } from "../utilities/useAxiosMutate.mjs";
import { useTransactionStore } from "../stores/transactionStore.mjs";
import { c } from "react-compiler-runtime";
import { useEffect, useState } from "react";
//#region src/hooks/useTransactionType.js
const useTransactionType = (t0) => {
	const $ = c(24);
	let t1;
	if ($[0] !== t0) {
		t1 = t0 === void 0 ? {} : t0;
		$[0] = t0;
		$[1] = t1;
	} else t1 = $[1];
	const { transactionTypeId, domainName, domainFieldType, domainFieldValue } = t1;
	const [resolvedId, setResolvedId] = useState(transactionTypeId || null);
	const [data, setData] = useState(void 0);
	const [loading, setLoading] = useState(true);
	const [error, setError] = useState(void 0);
	const [promise, setPromise] = useState(void 0);
	const { transactionTypesMap, setTransactionTypesMap } = useTransactionStore();
	let t2;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = (response) => {
			setError(void 0);
			setLoading(false);
			setData(response);
		};
		$[2] = t2;
	} else t2 = $[2];
	const setSuccessState = t2;
	let t3;
	if ($[3] !== domainFieldType || $[4] !== domainFieldValue || $[5] !== domainName || $[6] !== setTransactionTypesMap || $[7] !== transactionTypeId) {
		t3 = () => {
			if (transactionTypeId) {
				setResolvedId(transactionTypeId);
				return;
			}
			if (!domainName) return;
			setLoading(true);
			const domainPromise = apiMutate(ConfigService.productV1ApiUrl, `udpTransactionType/domain/${domainName}${domainFieldType && domainFieldValue ? `?domainFieldType=${domainFieldType}&domainFieldValue=${domainFieldValue}` : ""}`, {});
			domainPromise.then((res) => {
				const newId = res.data?.transactionTypeId;
				if (newId) {
					setResolvedId(newId);
					setTransactionTypesMap({ [newId]: res.data });
					setSuccessState(res.data);
				} else setError("Transaction type not found for domain.");
			}).catch(setError).finally(() => setPromise(domainPromise));
		};
		$[3] = domainFieldType;
		$[4] = domainFieldValue;
		$[5] = domainName;
		$[6] = setTransactionTypesMap;
		$[7] = transactionTypeId;
		$[8] = t3;
	} else t3 = $[8];
	let t4;
	if ($[9] !== domainFieldType || $[10] !== domainFieldValue || $[11] !== domainName || $[12] !== transactionTypeId) {
		t4 = [
			transactionTypeId,
			domainName,
			domainFieldType,
			domainFieldValue
		];
		$[9] = domainFieldType;
		$[10] = domainFieldValue;
		$[11] = domainName;
		$[12] = transactionTypeId;
		$[13] = t4;
	} else t4 = $[13];
	useEffect(t3, t4);
	let t5;
	let t6;
	if ($[14] !== resolvedId || $[15] !== setTransactionTypesMap || $[16] !== transactionTypesMap) {
		t5 = () => {
			if (!resolvedId) return;
			if (transactionTypesMap[resolvedId]) {
				setSuccessState(transactionTypesMap[resolvedId]);
				return;
			}
			setLoading(true);
			const fetchPromise = apiMutate(ConfigService.productV1ApiUrl, `udpTransactionType/${resolvedId}?withAssociationData=true`, {});
			fetchPromise.then((res_0) => {
				const transactionType = res_0.data;
				setTransactionTypesMap({ [resolvedId]: transactionType });
				setSuccessState(transactionType);
			}).catch(setError).finally(() => setPromise(fetchPromise));
		};
		t6 = [
			resolvedId,
			transactionTypesMap,
			setTransactionTypesMap,
			setSuccessState
		];
		$[14] = resolvedId;
		$[15] = setTransactionTypesMap;
		$[16] = transactionTypesMap;
		$[17] = t5;
		$[18] = t6;
	} else {
		t5 = $[17];
		t6 = $[18];
	}
	useEffect(t5, t6);
	let t7;
	if ($[19] !== data || $[20] !== error || $[21] !== loading || $[22] !== promise) {
		t7 = {
			data,
			loading,
			error,
			promise
		};
		$[19] = data;
		$[20] = error;
		$[21] = loading;
		$[22] = promise;
		$[23] = t7;
	} else t7 = $[23];
	return t7;
};
//#endregion
export { useTransactionType };

//# sourceMappingURL=useTransactionType.mjs.map