import { useState, useEffect, useCallback } from 'react';
import { apiMutate } from '../utilities/useAxiosMutate';
import { ConfigService } from '../configService';
import { useTransactionStore } from '../stores/transactionStore';
export const useTransactionType = (options = {}) => {
    const { transactionTypeId, domainName, domainFieldType, domainFieldValue } = options;
    const [resolvedId, setResolvedId] = useState(transactionTypeId || null);
    const [data, setData] = useState(undefined);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(undefined);
    const [promise, setPromise] = useState(undefined);
    const { transactionTypesMap, setTransactionTypesMap } = useTransactionStore();
    const setSuccessState = useCallback((response) => {
        setError(undefined);
        setLoading(false);
        setData(response);
    }, []);
    // ---------------------------------------------------------
    // EFFECT 1:
    // Resolve transactionTypeId if using domainName
    // ---------------------------------------------------------
    useEffect(() => {
        if (transactionTypeId) {
            setResolvedId(transactionTypeId);
            return;
        }
        if (!domainName)
            return;
        setLoading(true);
        const domainPromise = apiMutate(ConfigService.productV1ApiUrl, `udpTransactionType/domain/${domainName}${domainFieldType && domainFieldValue
            ? `?domainFieldType=${domainFieldType}&domainFieldValue=${domainFieldValue}`
            : ''}`, {});
        domainPromise
            .then((res) => {
            var _a;
            const newId = (_a = res.data) === null || _a === void 0 ? void 0 : _a.transactionTypeId;
            if (newId) {
                setResolvedId(newId);
                setTransactionTypesMap({ [newId]: res.data });
                setSuccessState(res.data);
            }
            else {
                setError('Transaction type not found for domain.');
            }
        })
            .catch(setError)
            .finally(() => setPromise(domainPromise));
    }, [transactionTypeId, domainName, domainFieldType, domainFieldValue]);
    // ---------------------------------------------------------
    // EFFECT 2:
    // Fetch transaction type once resolvedId is known
    // ---------------------------------------------------------
    useEffect(() => {
        if (!resolvedId)
            return;
        // Check cache first
        if (transactionTypesMap[resolvedId]) {
            setSuccessState(transactionTypesMap[resolvedId]);
            return;
        }
        setLoading(true);
        const fetchPromise = apiMutate(ConfigService.productV1ApiUrl, `udpTransactionType/${resolvedId}?withAssociationData=true`, {});
        fetchPromise
            .then((res) => {
            const transactionType = res.data;
            setTransactionTypesMap({ [resolvedId]: transactionType });
            setSuccessState(transactionType);
        })
            .catch(setError)
            .finally(() => setPromise(fetchPromise));
    }, [
        resolvedId,
        transactionTypesMap,
        setTransactionTypesMap,
        setSuccessState
    ]);
    return { data, loading, error, promise };
};
//# sourceMappingURL=useTransactionType.js.map