"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OmmProtocol = void 0;
const config_1 = require("../../../config");
const stream_connection_interface_1 = require("../stream-connection.interface");
const stream_message_converter_1 = require("../stream-message-converter");
const omm_types_interface_1 = require("./omm-types.interface");
const protocol_interface_1 = require("./protocol.interface");
class OmmProtocol {
    getProtocolName() {
        return 'tr_json2';
    }
    getProtocolType() {
        return config_1.StreamingType.OMM;
    }
    getPingPongConfig() {
        return {
            getPongMessage: () => ({ Type: omm_types_interface_1.OMMRequestType.Pong }),
            getPingMessage: () => ({ Type: omm_types_interface_1.OMMRequestType.Ping }),
            isPingMessage: res => res.Type === omm_types_interface_1.OMMResponseType.Ping,
            isPongMessage: res => res.Type === omm_types_interface_1.OMMResponseType.Pong,
        };
    }
    getInitialMessageSummary(res) {
        if (res.Type === omm_types_interface_1.OMMResponseType.Refresh && res.Complete === false) {
            return protocol_interface_1.ProtocolInitialMessageSummary.Pending;
        }
        // Order is important!
        // Complete shanpshot message has a non streaming state
        // Non streaming state is considered closed
        if (this.isSuccessStatusMessage(res) || this.isCompleteSnapshotMessage(res)) {
            return protocol_interface_1.ProtocolInitialMessageSummary.Success;
        }
        else if (this.hasClosedState(res) || this.isErrorMessage(res)) {
            return protocol_interface_1.ProtocolInitialMessageSummary.Error;
        }
        return protocol_interface_1.ProtocolInitialMessageSummary.Pending;
    }
    getSummary(res) {
        var _a;
        const responseId = 'ID' in res ? res.ID : void 0;
        const errorMessage = res.Type === omm_types_interface_1.OMMResponseType.Error ? res.Text : void 0;
        const stateMessage = 'State' in res ? (_a = res.State) === null || _a === void 0 ? void 0 : _a.Text : void 0;
        const isOpenStatusMessage = this.hasOpenState(res);
        const isClosedStatusMessage = this.hasClosedState(res);
        const isErrorMessage = this.isErrorMessage(res);
        const isCompleteSnapshotMessage = this.isCompleteSnapshotMessage(res);
        return {
            responseId,
            stateMessage,
            errorMessage,
            isErrorMessage,
            isOpenStatusMessage,
            isClosedStatusMessage,
            isCompleteSnapshotMessage,
        };
    }
    createRequest(id, params) {
        if (typeof params.name !== 'string') {
            throw new Error('Invalid name format!');
        }
        return Object.assign({ ID: id, Key: { Name: params.name, Service: params.service, Filter: params.filter }, Domain: params.domain, Streaming: params.streaming, Type: omm_types_interface_1.OMMRequestType.Request, View: params.view }, params.extendedParams);
    }
    createCloseRequest(id) {
        return {
            ID: id,
            Type: omm_types_interface_1.OMMRequestType.Close,
        };
    }
    getLoginMessage(loginParams) {
        const { position, applicationId, authorization, appKey, name, nameType, authenticationToken } = loginParams;
        return {
            ID: stream_connection_interface_1.STREAM_FIRST_REQUEST_ID,
            Domain: 'Login',
            Key: {
                Elements: {
                    ApplicationId: applicationId,
                    AppKey: appKey,
                    Position: position,
                    Authorization: authorization,
                    AuthenticationToken: authenticationToken,
                },
                Name: name,
                NameType: nameType,
            },
        };
    }
    getStreamingRecoverMessage(api, isCompleted) {
        return {
            Type: omm_types_interface_1.OMMResponseType.Status,
            State: {
                Data: isCompleted ? 'Ok' : 'Suspect',
                Stream: omm_types_interface_1.OMMResponseStateType.Open,
                Code: isCompleted ? 'FailoverCompleted' : 'FailoverStarted',
                Text: isCompleted ? (0, stream_message_converter_1.getStreamConnectionReconnected)(api) : (0, stream_message_converter_1.getStreamConnectionRecover)(api),
            },
        };
    }
    getStreamingErrorMessage(api, afterReconnect) {
        return {
            Type: omm_types_interface_1.OMMResponseType.Status,
            State: {
                Data: 'Suspect',
                Stream: omm_types_interface_1.OMMResponseStateType.Closed,
                Code: 'Error',
                Text: afterReconnect ? (0, stream_message_converter_1.getStreamConnectionRecoverError)(api) : (0, stream_message_converter_1.getStreamConnectionError)(api),
            },
        };
    }
    isSuccessStatusMessage(res) {
        return res.Type === omm_types_interface_1.OMMResponseType.Status && res.State.Stream === omm_types_interface_1.OMMResponseStateType.Open;
    }
    isCompleteSnapshotMessage(res) {
        return (res.Type === omm_types_interface_1.OMMResponseType.Refresh &&
            res.Complete !== false &&
            (!res.State || res.State.Stream === omm_types_interface_1.OMMResponseStateType.Open || res.State.Stream === omm_types_interface_1.OMMResponseStateType.NonStreaming));
    }
    isErrorMessage(res) {
        return res.Type === omm_types_interface_1.OMMResponseType.Error;
    }
    hasOpenState(res) {
        var _a;
        return 'State' in res && ((_a = res.State) === null || _a === void 0 ? void 0 : _a.Stream) === omm_types_interface_1.OMMResponseStateType.Open;
    }
    hasClosedState(res) {
        return !!('State' in res && res.State && res.State.Stream !== omm_types_interface_1.OMMResponseStateType.Open);
    }
}
exports.OmmProtocol = OmmProtocol;
//# sourceMappingURL=data:application/json;base64, 