"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module RpcInterface
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BentleyCloudRpcManager = exports.BentleyCloudRpcConfiguration = void 0;
const RpcManager_1 = require("../../RpcManager");
const RpcConfiguration_1 = require("../core/RpcConfiguration");
const RpcConstants_1 = require("../core/RpcConstants");
const RpcRequest_1 = require("../core/RpcRequest");
const BentleyCloudRpcProtocol_1 = require("./BentleyCloudRpcProtocol");
const RpcRoutingToken_1 = require("../core/RpcRoutingToken");
/** Operating parameters for Bentley cloud RPC interface deployments.
 * @beta
 */
class BentleyCloudRpcConfiguration extends RpcConfiguration_1.RpcConfiguration {
}
exports.BentleyCloudRpcConfiguration = BentleyCloudRpcConfiguration;
/** Access-Control header values for backend servers that serve frontends using BentleyCloudRpcProtocol. */
BentleyCloudRpcConfiguration.accessControl = {
    allowOrigin: "*",
    allowMethods: "POST, GET, OPTIONS",
    allowHeaders: "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, X-Correlation-Id, X-Session-Id, X-Application-Id, X-Application-Version, X-User-Id, X-Protocol-Version",
};
/** Coordinates usage of RPC interfaces for Bentley cloud deployments.
 * @beta
 */
class BentleyCloudRpcManager extends RpcManager_1.RpcManager {
    /** @beta Initializes BentleyCloudRpcManager for the frontend of an application. */
    static initializeClient(params, interfaces, routing = RpcRoutingToken_1.RpcRoutingToken.default) {
        return BentleyCloudRpcManager.performInitialization(params, interfaces, routing);
    }
    /** @beta Initializes BentleyCloudRpcManager for the backend of an application. */
    static initializeImpl(params, interfaces) {
        return BentleyCloudRpcManager.performInitialization(params, interfaces);
    }
    static performInitialization(params, interfaces, routing = RpcRoutingToken_1.RpcRoutingToken.default) {
        const protocol = class extends (params.protocol || BentleyCloudRpcProtocol_1.BentleyCloudRpcProtocol) {
            constructor() {
                super(...arguments);
                this.pathPrefix = params.uriPrefix || "";
                this.info = params.info;
            }
        };
        const config = class extends BentleyCloudRpcConfiguration {
            constructor() {
                super(...arguments);
                this.interfaces = () => interfaces;
                this.protocol = new protocol(this);
                this.routing = routing;
            }
        };
        for (const def of interfaces) {
            RpcConfiguration_1.RpcConfiguration.assignWithRouting(def, routing, config);
        }
        const instance = RpcConfiguration_1.RpcConfiguration.obtain(config);
        if (params.pathPrefix) {
            instance.protocol.pathPrefix = params.pathPrefix;
        }
        RpcConfiguration_1.RpcConfiguration.initializeInterfaces(instance);
        if (params.pendingRequestListener) {
            const listener = params.pendingRequestListener;
            RpcRequest_1.RpcRequest.events.addListener((type, request) => {
                if (type === RpcConstants_1.RpcRequestEvent.PendingUpdateReceived && request.protocol === instance.protocol) {
                    listener(type, request);
                }
            });
        }
        return instance;
    }
}
exports.BentleyCloudRpcManager = BentleyCloudRpcManager;
//# sourceMappingURL=BentleyCloudRpcManager.js.map