import { useState, useEffect, useCallback, useMemo } from 'react';
import { useCatalogStore } from '../stores/catalogStore';
import { useAxiosGet } from '../utilities/useAxiosGet';
import { getSanitizedName } from '../utilities/entities/entityUtilities';
import { ConfigService } from '../configService';
export const usePromotedMethodEntity = (entityName, compareSanitizedName = false, apiCatalogId, productId = ConfigService.config.UNITY_PRODUCT_ID) => {
    const [data, setData] = useState(undefined);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(undefined);
    const { currentApiCatalogId, setPromotedEntityMap, promotedEntityMap } = useCatalogStore();
    const acId = apiCatalogId || currentApiCatalogId;
    const setErrorState = useCallback((e) => {
        setLoading(false);
        setError(e);
    }, []);
    const setSuccessState = useCallback((response) => {
        setError(undefined);
        setLoading(false);
        setData(response);
    }, []);
    const cachedData = useMemo(() => {
        var _a, _b;
        if (!entityName) {
            return null;
        }
        let entityNameKey = (entityName === null || entityName === void 0 ? void 0 : entityName.toLowerCase()) || '';
        if (compareSanitizedName) {
            entityNameKey = getSanitizedName(entityName).toLowerCase();
        }
        if ((_b = (_a = promotedEntityMap === null || promotedEntityMap === void 0 ? void 0 : promotedEntityMap[acId]) === null || _a === void 0 ? void 0 : _a[productId]) === null || _b === void 0 ? void 0 : _b[entityNameKey]) {
            return promotedEntityMap[acId][productId][entityNameKey];
        }
        return null;
    }, [acId, productId, entityName, promotedEntityMap, compareSanitizedName]);
    const [{ data: promotedEntity, loading: promotedEntityLoading, error: promotedEntityError }] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${acId}/promotedMethodEntities/${entityName}/product/${productId}?compareSanitizedName=${compareSanitizedName}`, {}, !!!acId || !!!productId || !!!entityName || !!cachedData, false);
    useEffect(() => {
        var _a;
        let isMounted = true;
        if (entityName) {
            let entityNameKey = entityName.toLowerCase();
            let promotedEntityName = (_a = promotedEntity === null || promotedEntity === void 0 ? void 0 : promotedEntity.entityName) === null || _a === void 0 ? void 0 : _a.toLowerCase();
            if (compareSanitizedName) {
                entityNameKey = getSanitizedName(entityName).toLowerCase();
                if (promotedEntityName) {
                    promotedEntityName = getSanitizedName(promotedEntityName);
                }
            }
            if (cachedData) {
                setSuccessState(cachedData);
                return () => {
                    isMounted = false;
                };
            }
            if (promotedEntity && promotedEntityName === entityNameKey) {
                if (isMounted) {
                    const updatedPromotedEntityMap = Object.assign({}, promotedEntityMap);
                    if (updatedPromotedEntityMap[acId]) {
                        if (updatedPromotedEntityMap[acId][productId]) {
                            if (updatedPromotedEntityMap[acId][productId][entityNameKey]) {
                                updatedPromotedEntityMap[acId][productId][entityNameKey] =
                                    promotedEntity;
                            }
                            else {
                                updatedPromotedEntityMap[acId][productId] = Object.assign(Object.assign({}, updatedPromotedEntityMap[acId][productId]), { [entityNameKey]: promotedEntity });
                            }
                        }
                        else {
                            updatedPromotedEntityMap[acId][productId] = {
                                [entityNameKey]: promotedEntity
                            };
                        }
                    }
                    else {
                        updatedPromotedEntityMap[acId] = {
                            [productId]: { [entityNameKey]: promotedEntity }
                        };
                    }
                    setPromotedEntityMap(updatedPromotedEntityMap);
                    setSuccessState(updatedPromotedEntityMap[acId][productId][entityNameKey]);
                }
            }
        }
        if (promotedEntityError) {
            setErrorState();
        }
    }, [
        promotedEntity,
        acId,
        productId,
        entityName,
        promotedEntityMap,
        setSuccessState,
        setErrorState,
        promotedEntityError,
        setPromotedEntityMap,
        cachedData,
        compareSanitizedName
    ]);
    return {
        data,
        loading: promotedEntityLoading || loading,
        error
    };
};
//# sourceMappingURL=usePromotedMethodEntity.js.map