"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PHXGrpcQueryAllSchool = void 0;
exports.PHXClientQueryDataCenterV3 = PHXClientQueryDataCenterV3;
exports.PHXClientQueryDevPortalV3 = PHXClientQueryDevPortalV3;
exports.PHXClientQueryV3 = PHXClientQueryV3;
exports.PHXClientMutationV3 = PHXClientMutationV3;
exports.PHXClientMutationDataCenterV3 = PHXClientMutationDataCenterV3;
exports.PHXClientMutationDevPortalV3 = PHXClientMutationDevPortalV3;
const tslib_1 = require("tslib");
const axiosInstance_1 = require("../../../axiosInstance");
const constants_1 = require("../../../utils/constants");
const Sentry = tslib_1.__importStar(require("@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 axiosInstance_1.PHXAxiosInstance.post(pathUrl, {
            source: 'dashboard',
            query,
            variables,
            persisted_id: persistedId,
        }, {
            headers: {
                hostname,
                [constants_1.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 < constants_1.requestMaxDuration && isDelay) {
        console.info('promise delay v3...');
        await new Promise((r) => setTimeout(r, 100));
    }
    return queryResult;
};
async function PHXClientQueryDataCenterV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: constants_1.GRPC_HOSTNAME_DATA_CENTER,
        persistedId,
        isQuery: true,
    });
}
async function PHXClientQueryDevPortalV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: constants_1.GRPC_HOSTNAME_DEV_PORTAL,
        persistedId,
        isQuery: true,
    });
}
async function PHXClientQueryV3({ query, variables, isDelay = true, persistedId, hostname, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: hostname || window.location.origin,
        persistedId,
        isQuery: true,
    });
}
async function PHXClientMutationV3({ query, variables, isDelay = true, persistedId, hostname, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: hostname || window.location.origin,
        persistedId,
        isQuery: false,
    });
}
async function PHXClientMutationDataCenterV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: constants_1.GRPC_HOSTNAME_DATA_CENTER,
        persistedId,
        isQuery: false,
    });
}
async function PHXClientMutationDevPortalV3({ query, variables, isDelay = true, persistedId, }) {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: constants_1.GRPC_HOSTNAME_DEV_PORTAL,
        persistedId,
        isQuery: false,
    });
}
const PHXGrpcQueryAllSchool = async ({ isDelay = true, query, variables, persistedId }) => {
    return grpcQuery({
        query,
        variables,
        isDelay,
        hostname: constants_1.GRPC_HOSTNAME_QUERY_ALL_SCHOOL,
        persistedId,
        isQuery: true,
    });
};
exports.PHXGrpcQueryAllSchool = PHXGrpcQueryAllSchool;
//# sourceMappingURL=PHXGrpcClientV3.js.map