
import { useState, useEffect, useCallback, useMemo } from 'react';
import { Web3Cache } from '../web3/web3Cache';
import { Result } from '../utils/asyncHandler';

const cache = new Web3Cache();

export function useBlockchainData(address) {
    const [data, setData] = useState(null);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);

    const fetchData = useCallback(async () => {
        try {
            const result = await cache.getWithCache(
                `data-${address}`,
                async () => {
                    
                    return { balance: '0x0', transactions: [] };
                }
            );
            setData(result);
            setLoading(false);
        } catch (err) {
            setError(err);
            setLoading(false);
        }
    }, [address]);

    useEffect(() => {
        fetchData();
    }, [fetchData]);

    const formattedData = useMemo(() => {
        if (!data) return null;
       
        return data;
    }, [data]);

    return { data: formattedData, loading, error, refresh: fetchData };
}