"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClientAccessTokenFromBackend = exports.getProcessEnvFromBackend = exports.exposeBackendCallbacks = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const backend_itwin_client_1 = require("@bentley/backend-itwin-client");
const CallbackUtils_1 = require("@bentley/certa/lib/utils/CallbackUtils");
const itwin_client_1 = require("@bentley/itwin-client");
const getEnvCallbackName = "getEnv";
const getClientAccessTokenCallbackName = "getClientAccessToken";
function exposeBackendCallbacks() {
    CallbackUtils_1.registerBackendCallback(getEnvCallbackName, () => {
        return JSON.stringify(process.env);
    });
    CallbackUtils_1.registerBackendCallback(getClientAccessTokenCallbackName, async (clientConfiguration) => {
        const authClient = new backend_itwin_client_1.AgentAuthorizationClient(clientConfiguration);
        const token = await authClient.getAccessToken();
        return JSON.stringify(token.toJSON());
    });
}
exports.exposeBackendCallbacks = exposeBackendCallbacks;
async function getProcessEnvFromBackend() {
    return JSON.parse(await CallbackUtils_1.executeBackendCallback(getEnvCallbackName));
}
exports.getProcessEnvFromBackend = getProcessEnvFromBackend;
async function getClientAccessTokenFromBackend(clientConfiguration) {
    const tokenString = await CallbackUtils_1.executeBackendCallback(getClientAccessTokenCallbackName, clientConfiguration);
    return itwin_client_1.AccessToken.fromJson(JSON.parse(tokenString));
}
exports.getClientAccessTokenFromBackend = getClientAccessTokenFromBackend;
//# sourceMappingURL=SideChannels.js.map