import { PHXAxiosInstance } from '../../../axiosInstance';
import { HEADER_KEY_PATH_DASHBOARD, requestMaxDuration } from '../../../utils/constants';
import * as Sentry from '@sentry/nextjs';
const grpcQueryV5 = async ({ isDelay = true, query, variables, persistedId, hostname, isQuery, schemaGroup, }) => {
    let queryResult;
    const startTime = performance.now();
    const pathUrl = isQuery ? '/query' : '/mutation';
    try {
        queryResult = await PHXAxiosInstance.post(pathUrl, {
            source: 'dashboard',
            query,
            variables,
            persisted_id: persistedId,
            schemaGroup,
        }, {
            headers: {
                hostname,
                [HEADER_KEY_PATH_DASHBOARD]: window.location.pathname,
            },
        });
    }
    catch (error) {
        Sentry.captureException(error);
        throw new Error('Error with Axios request: ' + error.message);
    }
    const endTime = performance.now();
    const duration = endTime - startTime;
    // Nếu thời gian gọi nhanh quá, delay thêm để tăng trải nghiệm loading không bị giật
    if (duration < requestMaxDuration && isDelay) {
        console.info('promise delay v3...');
        await new Promise((r) => setTimeout(r, 100));
    }
    return queryResult;
};
export async function PHXClientQueryV5({ query, variables, isDelay = true, persistedId, hostname, schemaGroup, }) {
    return grpcQueryV5({
        query,
        variables,
        isDelay,
        hostname: hostname || window.location.origin,
        persistedId,
        isQuery: true,
        schemaGroup,
    });
}
export async function PHXClientMutationV5({ query, variables, isDelay = true, persistedId, hostname, schemaGroup, }) {
    return grpcQueryV5({
        query,
        variables,
        isDelay,
        hostname: hostname || window.location.origin,
        persistedId,
        isQuery: false,
        schemaGroup,
    });
}
//# sourceMappingURL=PHXGrpcClientV5.js.map