import PropTypes from 'prop-types';
import React, { useMemo, useEffect } from 'react';
import { Field } from '../../utilities/form';
import { UdpEnums } from '../../enums/unitySystemEnums';
import { FluentTextFieldAutoComplete } from '../inputs/textField/FluentTextFieldAutoComplete';
import { usePromotedMethodEntity } from '../../hooks/usePromotedMethodEntity';
import { useAxiosMutate } from '../../utilities/useAxiosMutate';
import { getSanitizedName } from '../../utilities/entities/entityUtilities';
import { useForm } from 'react-final-form';
import { useUser } from '../../utilities/auth/useUser';
import { TagEnums } from '../../enums/tagEnums';
import { ConfigService } from '../../configService';
export const UdpCrudFormFields = (props) => {
    var _a, _b;
    const { entityName, formData, existingTagAssociation } = props;
    const form = useForm();
    const user = useUser();
    const sanitizedEntityName = getSanitizedName(entityName);
    const IS_FOR_LOOKUP = (sanitizedEntityName === null || sanitizedEntityName === void 0 ? void 0 : sanitizedEntityName.toLowerCase()) === UdpEnums.Udp_Lookup.toLowerCase();
    const IS_FOR_TAGS = ![
        TagEnums.TagAssociationSanitizedName.toLowerCase(),
        TagEnums.TagSanitizedName.toLowerCase(),
        TagEnums.TagTypeSanitizedName.toLowerCase()
    ].includes(sanitizedEntityName === null || sanitizedEntityName === void 0 ? void 0 : sanitizedEntityName.toLowerCase());
    const { data: udpObjectInfoEntity } = usePromotedMethodEntity('udpObjectInfo', true);
    const { data: udpTagEntity } = usePromotedMethodEntity('udpTag', true);
    const [{ data: udpObjectInfoList }, fetchUdpObjectInfoList] = useAxiosMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${(_a = udpObjectInfoEntity === null || udpObjectInfoEntity === void 0 ? void 0 : udpObjectInfoEntity.unityBaseGetAllMethod) === null || _a === void 0 ? void 0 : _a.apiMethodId}`, {
        method: 'post'
    });
    const [{ data: udpTagList }, fetchUdpTagList] = useAxiosMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${(_b = udpTagEntity === null || udpTagEntity === void 0 ? void 0 : udpTagEntity.unityBaseGetAllMethod) === null || _b === void 0 ? void 0 : _b.apiMethodId}`, {
        method: 'post'
    });
    const initialTagValues = useMemo(() => {
        let existingTagIds = existingTagAssociation === null || existingTagAssociation === void 0 ? void 0 : existingTagAssociation.map((tag) => tag.id);
        let tagValues = udpTagList
            ? udpTagList.filter((tag) => existingTagIds.includes(tag.id))
            : [];
        return tagValues;
    }, [existingTagAssociation, udpTagList]);
    const initialDomainNameValue = useMemo(() => {
        let domainName = formData === null || formData === void 0 ? void 0 : formData.domainName;
        if (domainName && udpObjectInfoList) {
            domainName = getSanitizedName(domainName);
            return udpObjectInfoList.find((updObjectInfo) => updObjectInfo.sanitizedName === domainName);
        }
        return '';
    }, [formData, udpObjectInfoList]);
    const lookupFields = useMemo(() => {
        return (React.createElement(Field, { 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 (React.createElement(Field, { 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 === null || udpObjectInfoEntity === void 0 ? void 0 : 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 === null || udpTagEntity === void 0 ? void 0 : udpTagEntity.unityBaseGetAllMethod) {
            fetchUdpTagList({ data: {} });
        }
    }, [udpTagEntity, fetchUdpTagList]);
    return (React.createElement(React.Fragment, null, IS_FOR_LOOKUP ? lookupFields : null, IS_FOR_TAGS ? tagFields : null));
};
UdpCrudFormFields.propTypes = {
    /**
     * Entity name for promoted entity
     */
    entityName: PropTypes.string
};
//# sourceMappingURL=UdpCrudFormFields.js.map