import { PHXAxiosInstance } from '../../../axiosInstance';
import { GRPC_HOSTNAME_DATA_CENTER, GRPC_HOSTNAME_DEV_PORTAL, GRPC_HOSTNAME_QUERY_ALL_SCHOOL, HEADER_KEY_PATH_DASHBOARD, requestMaxDuration, } from '../../../utils/constants';
import * as Sentry from '@sentry/nextjs';
const grpcQuery = async ({ isDelay = true, query, variables, persistedId, hostname, isQuery }) => {
    let queryResult;
    const startTime = performance.now();
    const pathUrl = isQuery ? '/query' : '/mutation';
    try {
        queryResult = await PHXAxiosInstance.post(pathUrl, {
            source: 'dashboard',
            query,
            variables,
            persisted_id: persistedId,
        }, {
            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 PHXClientQueryDataCenterV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: GRPC_HOSTNAME_DATA_CENTER,
        persistedId,
        isQuery: true,
    });
}
export async function PHXClientQueryDevPortalV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: GRPC_HOSTNAME_DEV_PORTAL,
        persistedId,
        isQuery: true,
    });
}
export async function PHXClientQueryV3({ query, variables, isDelay = true, persistedId, hostname, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: hostname || window.location.origin,
        persistedId,
        isQuery: true,
    });
}
export async function PHXClientMutationV3({ query, variables, isDelay = true, persistedId, hostname, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: hostname || window.location.origin,
        persistedId,
        isQuery: false,
    });
}
export async function PHXClientMutationDataCenterV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: GRPC_HOSTNAME_DATA_CENTER,
        persistedId,
        isQuery: false,
    });
}
export async function PHXClientMutationDevPortalV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: GRPC_HOSTNAME_DEV_PORTAL,
        persistedId,
        isQuery: false,
    });
}
export const PHXGrpcQueryAllSchool = async ({ isDelay = true, query, variables, persistedId }) => {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: GRPC_HOSTNAME_QUERY_ALL_SCHOOL,
        persistedId,
        isQuery: true,
    });
};
//# sourceMappingURL=PHXGrpcClientV3.js.map