"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevTools = void 0;
const core_common_1 = require("@itwin/core-common");
/**
 * Internal diagnostic utility for backends
 * @internal
 */
class DevTools {
    /** Create a new connection with a specific backend instance.
     * @param tokenProps The iModelToken that identifies that backend instance.
     * Supply a dummy token if contacting the backend without the Orchestrator.
     */
    static connectToBackendInstance(tokenProps) {
        return new DevTools(tokenProps);
    }
    /** Constructor */
    constructor(_tokenProps) {
        this._tokenProps = _tokenProps;
    }
    /** Measures the round trip times for one or more pings to the backend
     * @param count Number of pings to send to the backend
     * @return Result of ping test.
     */
    async ping(count) {
        const pings = new Array(count);
        const pingFn = async () => {
            const start = Date.now();
            await core_common_1.DevToolsRpcInterface.getClient().ping(this._tokenProps);
            return Date.now() - start;
        };
        for (let ii = 0; ii < count; ii++)
            pings[ii] = pingFn().catch(async () => undefined);
        const pingTimes = await Promise.all(pings);
        const min = pingTimes.reduce((acc, curr) => {
            if (!acc)
                return curr;
            if (!curr)
                return acc;
            return Math.min(acc, curr);
        }, undefined);
        const max = pingTimes.reduce((acc, curr) => {
            if (typeof acc === "undefined")
                return undefined;
            if (!curr)
                return curr;
            return Math.max(acc, curr);
        }, 0);
        const total = pingTimes.reduce((acc, curr) => {
            if (typeof acc === "undefined")
                return undefined;
            if (!curr)
                return undefined;
            return acc + curr;
        }, 0);
        const avg = total ? total / count : undefined;
        return { min, max, avg };
    }
    /** Returns JSON object with backend statistics */
    async stats(options = core_common_1.DevToolsStatsOptions.FormatUnits) {
        return core_common_1.DevToolsRpcInterface.getClient().stats(this._tokenProps, options);
    }
    // Returns JSON object with backend versions (application and iModelJs)
    async versions() {
        return core_common_1.DevToolsRpcInterface.getClient().versions(this._tokenProps);
    }
    /** Sets up a log level at the backend and returns the old log level */
    async setLogLevel(inLoggerCategory, newLevel) {
        return core_common_1.DevToolsRpcInterface.getClient().setLogLevel(this._tokenProps, inLoggerCategory, newLevel);
    }
}
exports.DevTools = DevTools;
//# sourceMappingURL=DevTools.js.map