import { useState, useEffect, useCallback } from 'react';
import { apiMutate } from '../utilities/useAxiosMutate';
import { useCatalogStore } from '../stores/catalogStore';
import { ConfigService } from '../configService';
export const usePromotedMethodEntities = (apiCatalogId) => {
    const [data, setData] = useState(undefined);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(undefined);
    const [promise, setPromise] = useState(undefined);
    const { setPromotedMethodEntitiesMap, promotedMethodEntitiesMap } = useCatalogStore();
    const setErrorState = (e) => {
        setLoading(false);
        setError(e);
    };
    const setSuccessState = useCallback((response) => {
        setError(undefined);
        setLoading(false);
        setData(response);
    }, []);
    useEffect(() => {
        let isMounted = true;
        if (!apiCatalogId)
            return;
        if (promotedMethodEntitiesMap[apiCatalogId]) {
            setSuccessState(promotedMethodEntitiesMap[apiCatalogId]);
            return () => {
                isMounted = false;
            };
        }
        const p = apiMutate(ConfigService.integrationV1ApiUrl, `apiCatalog/${apiCatalogId}/promotedMethodEntities?productId=${ConfigService.config.UNITY_PRODUCT_ID}`, {});
        p.then((response) => {
            if (isMounted) {
                setSuccessState(response.data);
                setPromotedMethodEntitiesMap({ [apiCatalogId]: response.data });
            }
        }).catch(setErrorState);
        setPromise(p);
        return () => {
            isMounted = false;
        };
    }, [
        apiCatalogId,
        promotedMethodEntitiesMap,
        setPromotedMethodEntitiesMap,
        setSuccessState
    ]);
    return { data, loading, error, promise };
};
//# sourceMappingURL=usePromotedMethodEntities.js.map