import { useState, useEffect } from 'react';
import { PHXClientQueryV3 } from './GRPC/PHXGrpcClientV3';
export default function PHXUseQueryDataV3({ dependencies, query, variables, requiredFields, }) {
    const [isLoading, setIsLoading] = useState(true);
    const [data, setData] = useState();
    const [error, setError] = useState(null);
    const isValidVariables = () => {
        // Nếu không có variables, trả về true
        if (!variables)
            return true;
        // Nếu có requiredFields, kiểm tra chỉ các key trong requiredFields
        if (requiredFields) {
            return requiredFields.every((key) => variables[key] !== undefined && variables[key] !== null && variables[key] !== '');
        }
        // Nếu không có requiredFields, kiểm tra toàn bộ variables
        return Object.values(variables).every((value) => value !== undefined && value !== null && value !== '');
    };
    useEffect(() => {
        const fetchData = async () => {
            if (!isValidVariables()) {
                return;
            }
            try {
                const response = await PHXClientQueryV3({
                    query,
                    ...(variables && { variables }),
                });
                setData(response.data);
            }
            catch (err) {
                setError('An error occurred while fetching data.');
                console.error(err);
            }
            finally {
                setIsLoading(false);
            }
        };
        fetchData();
    }, Array.isArray(dependencies) ? [...dependencies] : []); // Ensure dependencies is an array
    return { isLoading, data, error };
}
//# sourceMappingURL=PHXUseQueryDataV3.js.map