/*---------------------------------------------------------------------------------------------
* 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
 */
import { RpcManager } from "../../RpcManager";
import { RpcConfiguration } from "../core/RpcConfiguration";
import { RpcRequestEvent } from "../core/RpcConstants";
import { RpcRequest } from "../core/RpcRequest";
import { BentleyCloudRpcProtocol } from "./BentleyCloudRpcProtocol";
import { RpcRoutingToken } from "../core/RpcRoutingToken";
/** Operating parameters for Bentley cloud RPC interface deployments.
 * @beta
 */
export class BentleyCloudRpcConfiguration extends RpcConfiguration {
}
/** 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
 */
export class BentleyCloudRpcManager extends RpcManager {
    /** @beta Initializes BentleyCloudRpcManager for the frontend of an application. */
    static initializeClient(params, interfaces, routing = 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.default) {
        const protocol = class extends (params.protocol || 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.assignWithRouting(def, routing, config);
        }
        const instance = RpcConfiguration.obtain(config);
        if (params.pathPrefix) {
            instance.protocol.pathPrefix = params.pathPrefix;
        }
        RpcConfiguration.initializeInterfaces(instance);
        if (params.pendingRequestListener) {
            const listener = params.pendingRequestListener;
            RpcRequest.events.addListener((type, request) => {
                if (type === RpcRequestEvent.PendingUpdateReceived && request.protocol === instance.protocol) {
                    listener(type, request);
                }
            });
        }
        return instance;
    }
}
//# sourceMappingURL=BentleyCloudRpcManager.js.map