import { ConfigService } from "../configService/index.mjs";
import { getSanitizedName } from "../utilities/entities/entityUtilities.mjs";
import { useCatalogStore } from "../stores/catalogStore.mjs";
import { useAxiosGet } from "../utilities/useAxiosGet.mjs";
import { c } from "react-compiler-runtime";
import { useEffect, useState } from "react";
//#region src/hooks/usePromotedMethodEntity.js
const usePromotedMethodEntity = (entityName, t0, apiCatalogId, t1) => {
	const $ = c(18);
	const compareSanitizedName = t0 === void 0 ? false : t0;
	const productId = t1 === void 0 ? ConfigService.config.UNITY_PRODUCT_ID : t1;
	const [data, setData] = useState(void 0);
	const [loading, setLoading] = useState(true);
	const [error, setError] = useState(void 0);
	const { currentApiCatalogId, setPromotedEntityMap, promotedEntityMap } = useCatalogStore();
	const acId = apiCatalogId || currentApiCatalogId;
	let t2;
	if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
		t2 = (e) => {
			setLoading(false);
			setError(e);
		};
		$[0] = t2;
	} else t2 = $[0];
	const setErrorState = t2;
	let t3;
	if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
		t3 = (response) => {
			setError(void 0);
			setLoading(false);
			setData(response);
		};
		$[1] = t3;
	} else t3 = $[1];
	const setSuccessState = t3;
	let t4;
	bb0: {
		if (!entityName) {
			t4 = null;
			break bb0;
		}
		let entityNameKey = entityName?.toLowerCase() || "";
		if (compareSanitizedName) entityNameKey = getSanitizedName(entityName).toLowerCase();
		if (promotedEntityMap?.[acId]?.[productId]?.[entityNameKey]) {
			t4 = promotedEntityMap[acId][productId][entityNameKey];
			break bb0;
		}
		t4 = null;
	}
	const cachedData = t4;
	let t5;
	if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
		t5 = {};
		$[2] = t5;
	} else t5 = $[2];
	const [t6] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${acId}/promotedMethodEntities/${entityName}/product/${productId}?compareSanitizedName=${compareSanitizedName}`, t5, !!!acId || !!!productId || !!!entityName || !!cachedData, false);
	const { data: promotedEntity, loading: promotedEntityLoading, error: promotedEntityError } = t6;
	let t7;
	let t8;
	if ($[3] !== acId || $[4] !== cachedData || $[5] !== compareSanitizedName || $[6] !== entityName || $[7] !== productId || $[8] !== promotedEntity || $[9] !== promotedEntityError || $[10] !== promotedEntityMap || $[11] !== setPromotedEntityMap) {
		t7 = () => {
			let isMounted = true;
			if (entityName) {
				let entityNameKey_0 = entityName.toLowerCase();
				let promotedEntityName = promotedEntity?.entityName?.toLowerCase();
				if (compareSanitizedName) {
					entityNameKey_0 = getSanitizedName(entityName).toLowerCase();
					if (promotedEntityName) promotedEntityName = getSanitizedName(promotedEntityName);
				}
				if (cachedData) {
					setSuccessState(cachedData);
					return () => {
						isMounted = false;
					};
				}
				if (promotedEntity && promotedEntityName === entityNameKey_0) {
					if (isMounted) {
						const updatedPromotedEntityMap = { ...promotedEntityMap };
						if (updatedPromotedEntityMap[acId]) if (updatedPromotedEntityMap[acId][productId]) if (updatedPromotedEntityMap[acId][productId][entityNameKey_0]) updatedPromotedEntityMap[acId][productId][entityNameKey_0] = promotedEntity;
						else updatedPromotedEntityMap[acId][productId] = {
							...updatedPromotedEntityMap[acId][productId],
							[entityNameKey_0]: promotedEntity
						};
						else updatedPromotedEntityMap[acId][productId] = { [entityNameKey_0]: promotedEntity };
						else updatedPromotedEntityMap[acId] = { [productId]: { [entityNameKey_0]: promotedEntity } };
						setPromotedEntityMap(updatedPromotedEntityMap);
						setSuccessState(updatedPromotedEntityMap[acId][productId][entityNameKey_0]);
					}
				}
			}
			if (promotedEntityError) setErrorState();
		};
		t8 = [
			promotedEntity,
			acId,
			productId,
			entityName,
			promotedEntityMap,
			setSuccessState,
			setErrorState,
			promotedEntityError,
			setPromotedEntityMap,
			cachedData,
			compareSanitizedName
		];
		$[3] = acId;
		$[4] = cachedData;
		$[5] = compareSanitizedName;
		$[6] = entityName;
		$[7] = productId;
		$[8] = promotedEntity;
		$[9] = promotedEntityError;
		$[10] = promotedEntityMap;
		$[11] = setPromotedEntityMap;
		$[12] = t7;
		$[13] = t8;
	} else {
		t7 = $[12];
		t8 = $[13];
	}
	useEffect(t7, t8);
	const t9 = promotedEntityLoading || loading;
	let t10;
	if ($[14] !== data || $[15] !== error || $[16] !== t9) {
		t10 = {
			data,
			loading: t9,
			error
		};
		$[14] = data;
		$[15] = error;
		$[16] = t9;
		$[17] = t10;
	} else t10 = $[17];
	return t10;
};
//#endregion
export { usePromotedMethodEntity };

//# sourceMappingURL=usePromotedMethodEntity.mjs.map