import { Field as Field$1 } from "../../utilities/form/Field.mjs";
import { ConfigService } from "../../configService/index.mjs";
import { UdpEnums } from "../../enums/unitySystemEnums.mjs";
import { useUser } from "../../utilities/auth/useUser.mjs";
import { useAxiosMutate } from "../../utilities/useAxiosMutate.mjs";
import { FluentTextFieldAutoComplete } from "../inputs/textField/FluentTextFieldAutoComplete.mjs";
import { getSanitizedName } from "../../utilities/entities/entityUtilities.mjs";
import { usePromotedMethodEntity } from "../../hooks/usePromotedMethodEntity.mjs";
import { TagEnums } from "../../enums/tagEnums.mjs";
import React, { useEffect, useMemo } from "react";
import { useForm } from "react-final-form";
import PropTypes from "prop-types";
//#region src/UI/forms/UdpCrudFormFields.jsx
const UdpCrudFormFields = (props) => {
	const { entityName, formData, existingTagAssociation } = props;
	const form = useForm();
	useUser();
	const sanitizedEntityName = getSanitizedName(entityName);
	const IS_FOR_LOOKUP = sanitizedEntityName?.toLowerCase() === UdpEnums.Udp_Lookup.toLowerCase();
	const IS_FOR_TAGS = ![
		TagEnums.TagAssociationSanitizedName.toLowerCase(),
		TagEnums.TagSanitizedName.toLowerCase(),
		TagEnums.TagTypeSanitizedName.toLowerCase()
	].includes(sanitizedEntityName?.toLowerCase());
	const { data: udpObjectInfoEntity } = usePromotedMethodEntity("udpObjectInfo", true);
	const { data: udpTagEntity } = usePromotedMethodEntity("udpTag", true);
	const [{ data: udpObjectInfoList }, fetchUdpObjectInfoList] = useAxiosMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${udpObjectInfoEntity?.unityBaseGetAllMethod?.apiMethodId}`, { method: "post" });
	const [{ data: udpTagList }, fetchUdpTagList] = useAxiosMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${udpTagEntity?.unityBaseGetAllMethod?.apiMethodId}`, { method: "post" });
	const initialTagValues = useMemo(() => {
		let existingTagIds = existingTagAssociation?.map((tag) => tag.id);
		return udpTagList ? udpTagList.filter((tag_0) => existingTagIds.includes(tag_0.id)) : [];
	}, [existingTagAssociation, udpTagList]);
	const initialDomainNameValue = useMemo(() => {
		let domainName = formData?.domainName;
		if (domainName && udpObjectInfoList) {
			domainName = getSanitizedName(domainName);
			return udpObjectInfoList.find((updObjectInfo) => updObjectInfo.sanitizedName === domainName);
		}
		return "";
	}, [formData, udpObjectInfoList]);
	const lookupFields = useMemo(() => {
		return /* @__PURE__ */ React.createElement(Field$1, {
			component: FluentTextFieldAutoComplete,
			initialValue: initialDomainNameValue,
			label: "Domain Name",
			name: "domainName",
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			options: udpObjectInfoList || [],
			optionKey: "sanitizedName",
			style: { marginTop: 8 }
		});
	}, [udpObjectInfoList, initialDomainNameValue]);
	const tagFields = useMemo(() => {
		return /* @__PURE__ */ React.createElement(Field$1, {
			component: FluentTextFieldAutoComplete,
			initialValue: initialTagValues,
			isMultiple: true,
			label: "Tags",
			name: "udpTags",
			fullWidth: true,
			variant: "outlined",
			margin: "normal",
			options: udpTagList || [],
			optionKey: "name",
			style: { marginTop: 8 }
		});
	}, [udpTagList, initialTagValues]);
	useEffect(() => {
		if (udpObjectInfoEntity?.unityBaseGetAllMethod && IS_FOR_LOOKUP) fetchUdpObjectInfoList({ data: {} });
	}, [
		udpObjectInfoEntity,
		fetchUdpObjectInfoList,
		IS_FOR_LOOKUP
	]);
	useEffect(() => {
		form.change("domainName", initialDomainNameValue);
	}, [form, initialDomainNameValue]);
	useEffect(() => {
		form.change("udpTags", initialTagValues);
	}, [form, initialTagValues]);
	useEffect(() => {
		if (udpTagEntity?.unityBaseGetAllMethod) fetchUdpTagList({ data: {} });
	}, [udpTagEntity, fetchUdpTagList]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, IS_FOR_LOOKUP ? lookupFields : null, IS_FOR_TAGS ? tagFields : null);
};
UdpCrudFormFields.propTypes = { entityName: PropTypes.string };
//#endregion
export { UdpCrudFormFields };

//# sourceMappingURL=UdpCrudFormFields.mjs.map