import { PHXAxiosInstance } from '../../../axiosInstance';
import { GRPC_HOSTNAME_QUERY_ALL_SCHOOL, HEADER_KEY_PATH_DASHBOARD, HEADER_KEY_REQUIRED_SCHOOL_ID, requestMaxDuration, } from '../../../utils/constants';
import * as Sentry from '@sentry/nextjs';
const grpcQueryV6 = async ({ isDelay = true, query, variables, persistedId, isQuery, schemaGroup, hostname, }) => {
    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 && { hostname }), // hostname use to query all school
                [HEADER_KEY_PATH_DASHBOARD]: window.location.pathname,
                ...(!hostname && { [HEADER_KEY_REQUIRED_SCHOOL_ID]: true }),
            },
        });
    }
    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 v6...');
        await new Promise((r) => setTimeout(r, 100));
    }
    return queryResult;
};
export async function PHXClientQueryV6({ query, variables, isDelay = true, persistedId, schemaGroup }) {
    return grpcQueryV6({
        query,
        variables,
        isDelay,
        persistedId,
        isQuery: true,
        schemaGroup,
    });
}
export async function PHXClientMutationV6({ query, variables, isDelay = true, persistedId, schemaGroup }) {
    return grpcQueryV6({
        query,
        variables,
        isDelay,
        persistedId,
        isQuery: false,
        schemaGroup,
    });
}
export const PHXGrpcQueryAllSchoolV6 = async ({ isDelay = true, query, variables, persistedId, schemaGroup, }) => {
    return grpcQueryV6({
        query,
        variables,
        isDelay,
        hostname: GRPC_HOSTNAME_QUERY_ALL_SCHOOL,
        persistedId,
        isQuery: true,
        schemaGroup,
    });
};
//# sourceMappingURL=PHXGrpcClientV6.js.map