"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.parseRequest = parseRequest;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
/* eslint-disable @typescript-eslint/no-deprecated */
function parseHeaders(protocol, req) {
    const headerNames = protocol.serializedClientRequestContextHeaderNames;
    const parsedHeaders = {
        id: req.header(headerNames.id) || "",
        applicationId: req.header(headerNames.applicationId) || "",
        applicationVersion: req.header(headerNames.applicationVersion) || "",
        sessionId: req.header(headerNames.sessionId) || "",
        authorization: (headerNames.authorization ? req.header(headerNames.authorization) : "") ?? "",
    };
    return parsedHeaders;
}
function parseFromPath(operation) {
    const decoded = operation.encodedRequest ? Buffer.from(operation.encodedRequest, "base64").toString("binary") : "";
    return core_common_1.RpcSerializedValue.create(decoded);
}
async function parseFromBody(req) {
    const contentType = core_common_1.WebAppRpcProtocol.computeContentType(req.header(core_common_1.WEB_RPC_CONSTANTS.CONTENT));
    if (contentType === core_common_1.RpcContentType.Binary) {
        const objects = JSON.stringify([core_common_1.MarshalingBinaryMarker.createDefault()]);
        const data = [req.body];
        return core_common_1.RpcSerializedValue.create(objects, data);
    }
    else if (contentType === core_common_1.RpcContentType.Multipart) {
        return core_common_1.RpcMultipart.parseRequest(req);
    }
    else {
        return core_common_1.RpcSerializedValue.create(req.body);
    }
}
/** @internal */
async function parseRequest(protocol, req) {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const operation = protocol.getOperationFromPath(req.url);
    const parsedHeaders = parseHeaders(protocol, req);
    const request = {
        ...parsedHeaders,
        operation: {
            interfaceDefinition: operation.interfaceDefinition,
            operationName: operation.operationName,
            interfaceVersion: operation.interfaceVersion,
        },
        method: req.method,
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        path: req.url,
        parameters: operation.encodedRequest ? parseFromPath(operation) : await parseFromBody(req),
        caching: operation.encodedRequest ? core_common_1.RpcResponseCacheControl.Immutable : core_common_1.RpcResponseCacheControl.None,
    };
    request.ip = req.ip;
    request.protocolVersion = 0;
    if (protocol.protocolVersionHeaderName) {
        const version = req.header(protocol.protocolVersionHeaderName);
        if (version) {
            request.protocolVersion = parseInt(version, 10);
        }
    }
    if (!request.id) {
        throw new core_common_1.IModelError(core_bentley_1.BentleyStatus.ERROR, `Invalid request: Missing required activity ID.`);
    }
    return request;
}
//# sourceMappingURL=request.js.map