"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RdpProtocol = void 0;
const isArray_1 = __importDefault(require("lodash/isArray"));
const config_1 = require("../../../config");
const stream_connection_interface_1 = require("../stream-connection.interface");
const stream_message_converter_1 = require("../stream-message-converter");
const protocol_interface_1 = require("./protocol.interface");
const rdp_types_interface_1 = require("./rdp-types.interface");
const lodash_1 = require("lodash");
class RdpProtocol {
    getProtocolName() {
        return 'rdp_streaming';
    }
    getProtocolType() {
        return config_1.StreamingType.RDP;
    }
    getPingPongConfig() {
        return void 0;
    }
    getInitialMessageSummary(res) {
        const { isClosedStatusMessage, isCompleteSnapshotMessage, isOpenStatusMessage } = this.getSummary(res);
        if (isOpenStatusMessage || isCompleteSnapshotMessage) {
            return protocol_interface_1.ProtocolInitialMessageSummary.Success;
        }
        else if (isClosedStatusMessage) {
            return protocol_interface_1.ProtocolInitialMessageSummary.Error;
        }
        return protocol_interface_1.ProtocolInitialMessageSummary.Pending;
    }
    getSummary(res) {
        const isClosedStatusMessage = !!('state' in res &&
            (res.state.code >= 300 ||
                res.state.stream === rdp_types_interface_1.RDPResponseStateType.NonStreaming ||
                res.state.stream === rdp_types_interface_1.RDPResponseStateType.Closed));
        const isOpenStatusMessage = !!('state' in res &&
            res.state.code >= 200 &&
            res.state.code < 300 &&
            res.state.stream !== rdp_types_interface_1.RDPResponseStateType.NonStreaming &&
            res.state.stream !== rdp_types_interface_1.RDPResponseStateType.Closed);
        const isCompleteSnapshotMessage = res.type === rdp_types_interface_1.RDPResponseType.Response;
        const responseId = res.streamID ? Number(res.streamID) : void 0;
        const stateMessage = 'state' in res ? res.state.message : void 0;
        return {
            isClosedStatusMessage,
            isOpenStatusMessage,
            isCompleteSnapshotMessage,
            // No explicit error messages for RDP protocol
            isErrorMessage: false,
            errorMessage: void 0,
            stateMessage,
            responseId,
        };
    }
    createRequest(id, params) {
        return this.createBasicRequest(id, params);
    }
    createCloseRequest(id) {
        return {
            streamID: String(id),
            method: rdp_types_interface_1.RDPRequestType.Close,
        };
    }
    createModifyRequest(id, params) {
        const basicRequest = this.createBasicRequest(id, params, rdp_types_interface_1.RDPRequestType.Modify);
        return (0, lodash_1.omitBy)(basicRequest, lodash_1.isEmpty);
    }
    createBasicRequest(id, params, method = rdp_types_interface_1.RDPRequestType.Subscribe) {
        const { name = [], domain, service, view, parameters } = params;
        return Object.assign({ streamID: String(id), method, context: domain, service,
            view, universe: (0, isArray_1.default)(name) ? name : [name], parameters: Object.assign({}, parameters) }, params.extendedParams);
    }
    getLoginMessage(loginParams) {
        const { authenticationToken, appKey, authorization } = loginParams;
        return {
            method: rdp_types_interface_1.RDPRequestType.Auth,
            streamID: String(stream_connection_interface_1.STREAM_FIRST_REQUEST_ID),
            token: authenticationToken,
            appKey,
            authorization,
        };
    }
    getStreamingRecoverMessage(api, isCompleted) {
        return {
            state: {
                code: 503,
                message: isCompleted ? (0, stream_message_converter_1.getStreamConnectionReconnected)(api) : (0, stream_message_converter_1.getStreamConnectionRecover)(api),
                status: isCompleted ? 'FailoverCompleted' : 'FailoverStarted',
                stream: rdp_types_interface_1.RDPResponseStateType.Open,
            },
            type: rdp_types_interface_1.RDPResponseType.Ack,
        };
    }
    getStreamingErrorMessage(api, afterReconnect) {
        return {
            state: {
                code: 500,
                message: afterReconnect ? (0, stream_message_converter_1.getStreamConnectionRecoverError)(api) : (0, stream_message_converter_1.getStreamConnectionError)(api),
                status: 'Error',
                stream: rdp_types_interface_1.RDPResponseStateType.Closed,
            },
            type: rdp_types_interface_1.RDPResponseType.Ack,
        };
    }
}
exports.RdpProtocol = RdpProtocol;
//# sourceMappingURL=data:application/json;base64, 