"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = PHXUseQueryDataV3;
const react_1 = require("react");
const PHXGrpcClientV3_1 = require("./GRPC/PHXGrpcClientV3");
function PHXUseQueryDataV3({ dependencies, query, variables, requiredFields, }) {
    const [isLoading, setIsLoading] = (0, react_1.useState)(true);
    const [data, setData] = (0, react_1.useState)();
    const [error, setError] = (0, react_1.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 !== '');
    };
    (0, react_1.useEffect)(() => {
        const fetchData = async () => {
            if (!isValidVariables()) {
                return;
            }
            try {
                const response = await (0, PHXGrpcClientV3_1.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