import { apiMutate } from "../../utilities/useAxiosMutate.mjs";
import { c } from "react-compiler-runtime";
import { ConfigService } from "udp-react-stencil-component-library";
import { useQuery } from "@tanstack/react-query";
//#region src/udp/tags/useTags.ts
const TAG_ASSOCIATION_ENDPOINT = "udpTagAssociation/search";
const BASE_SEARCH_PAYLOAD = {
	pageNumber: 1,
	pageSize: 200,
	filterElements: [],
	filterGroups: [],
	orderElements: [],
	groupingType: "",
	groupProperty: [],
	groupOperationList: [],
	eagerLoad: true,
	logicalSearchOperator: 1
};
const EMPTY_MAP = /* @__PURE__ */ new Map();
const fetchTagAssociations = async (entityBusinessKeyPrefix) => {
	return (await apiMutate(ConfigService.productV1ApiUrl, TAG_ASSOCIATION_ENDPOINT, { method: "post" }, { data: {
		...BASE_SEARCH_PAYLOAD,
		getAll: true,
		filterElements: [{
			searchField: "entityBusinessKey",
			searchOperator: "STARTSWITH",
			searchValue: entityBusinessKeyPrefix
		}]
	} }))?.data?.pageList ?? [];
};
const buildTagMap = (associations) => {
	const map = /* @__PURE__ */ new Map();
	for (const a of associations) {
		const id = a.entityBusinessKey?.split("|")[1];
		const tag = a.udpTagAssociationUdpTag;
		if (!id || !tag) continue;
		const existing = map.get(id);
		if (existing) existing.push(tag);
		else map.set(id, [tag]);
	}
	return map;
};
/**
* Fetch and cache tag associations for every record of a single entity type.
*
* Pass a type-only prefix ending in `|` — e.g.
* `Univerus.Unity.Starter.Project.Models.ChargeCodeTask|`.
*
* Returns a `Map<entityId, UdpTag[]>` keyed by the GUID portion of the
* business key. The query is shared across the app via TanStack Query, so
* multiple components calling this hook with the same prefix dedupe to a
* single request and stay in sync on invalidation.
*/
const useTags = (entityBusinessKeyPrefix, options) => {
	const $ = c(11);
	let t0;
	let t1;
	if ($[0] !== entityBusinessKeyPrefix) {
		t0 = [TAG_ASSOCIATION_ENDPOINT, entityBusinessKeyPrefix];
		t1 = () => fetchTagAssociations(entityBusinessKeyPrefix);
		$[0] = entityBusinessKeyPrefix;
		$[1] = t0;
		$[2] = t1;
	} else {
		t0 = $[1];
		t1 = $[2];
	}
	const t2 = options?.enabled !== false && !!entityBusinessKeyPrefix;
	let t3;
	if ($[3] !== t0 || $[4] !== t1 || $[5] !== t2) {
		t3 = {
			queryKey: t0,
			queryFn: t1,
			enabled: t2,
			select: buildTagMap
		};
		$[3] = t0;
		$[4] = t1;
		$[5] = t2;
		$[6] = t3;
	} else t3 = $[6];
	const query = useQuery(t3);
	const t4 = query.data ?? EMPTY_MAP;
	let t5;
	if ($[7] !== query.error || $[8] !== query.isLoading || $[9] !== t4) {
		t5 = {
			tagMap: t4,
			isLoading: query.isLoading,
			error: query.error
		};
		$[7] = query.error;
		$[8] = query.isLoading;
		$[9] = t4;
		$[10] = t5;
	} else t5 = $[10];
	return t5;
};
//#endregion
export { useTags };

//# sourceMappingURL=useTags.mjs.map