"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OMMStreamImpl = void 0;
const abstract_item_stream_1 = require("./abstract-item-stream");
const item_stream_error_1 = require("./item-stream-error");
const omm_stream_params_validator_1 = require("./omm-stream-params-validator");
const omm_stream_interface_1 = require("./omm-stream.interface");
const omm_protocol_1 = require("./protocol/omm-protocol");
const omm_types_interface_1 = require("./protocol/omm-types.interface");
class OMMStreamImpl extends abstract_item_stream_1.AbstractItemStream {
    /** @internal */
    constructor(session, params) {
        super(session, new omm_protocol_1.OmmProtocol(), params.api || omm_stream_interface_1.DEFAULT_OMM_STREAMING_CONNECTION_NAME);
        this.params = params;
        this.itemStreamEventsEmitter = this;
        const name = this.params.name;
        (0, omm_stream_params_validator_1.validateOMMStreamParams)({ session, name });
        this.name = name;
    }
    onRefresh(cb) {
        this.itemStreamEventsEmitter.on(omm_stream_interface_1.OMMStream.Event.Refresh, cb);
        return this;
    }
    onUpdate(cb) {
        this.itemStreamEventsEmitter.on(omm_stream_interface_1.OMMStream.Event.Update, cb);
        return this;
    }
    onStatus(cb) {
        this.itemStreamEventsEmitter.on(omm_stream_interface_1.OMMStream.Event.Status, cb);
        return this;
    }
    onError(cb) {
        this.itemStreamEventsEmitter.on(omm_stream_interface_1.OMMStream.Event.Error, cb);
        return this;
    }
    onComplete(cb) {
        this.itemStreamEventsEmitter.on(omm_stream_interface_1.OMMStream.Event.Complete, cb);
        return this;
    }
    getStreamConnection() {
        return this.session.getOMMStreamConnection(this.params.api || omm_stream_interface_1.DEFAULT_OMM_STREAMING_CONNECTION_NAME);
    }
    getRequestParams() {
        return {
            name: this.name,
            domain: this.params.domain,
            filter: this.params.filter,
            service: this.params.service,
            streaming: this.params.streaming,
            view: this.params.fields,
            extendedParams: this.params.extendedParams,
        };
    }
    emitMessage(message) {
        switch (message.Type) {
            case omm_types_interface_1.OMMResponseType.Refresh:
                this.itemStreamEventsEmitter.emit(omm_stream_interface_1.OMMStream.Event.Refresh, message, this);
                break;
            case omm_types_interface_1.OMMResponseType.Status:
                this.itemStreamEventsEmitter.emit(omm_stream_interface_1.OMMStream.Event.Status, message, this);
                break;
            case omm_types_interface_1.OMMResponseType.Update:
                this.itemStreamEventsEmitter.emit(omm_stream_interface_1.OMMStream.Event.Update, message, this);
                break;
            case omm_types_interface_1.OMMResponseType.Error:
                this.itemStreamEventsEmitter.emit(omm_stream_interface_1.OMMStream.Event.Error, new item_stream_error_1.ItemStreamError(message.Text, message), this);
        }
    }
}
exports.OMMStreamImpl = OMMStreamImpl;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib21tLXN0cmVhbS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9kZWxpdmVyeS9zdHJlYW0vb21tLXN0cmVhbS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFDQSxpRUFBNEQ7QUFDNUQsMkRBQXNEO0FBQ3RELCtFQUF3RTtBQUN4RSxpRUFTZ0M7QUFDaEMsMERBQXNEO0FBQ3RELHdFQUEwRjtBQUcxRixNQUFhLGFBQWMsU0FBUSx5Q0FBMkM7SUFJMUUsZ0JBQWdCO0lBQ2hCLFlBQVksT0FBZ0IsRUFBVSxNQUE4QjtRQUNoRSxLQUFLLENBQUMsT0FBTyxFQUFFLElBQUksMEJBQVcsRUFBRSxFQUFFLE1BQU0sQ0FBQyxHQUFHLElBQUksNERBQXFDLENBQUMsQ0FBQztRQURyRCxXQUFNLEdBQU4sTUFBTSxDQUF3QjtRQUg1RCw0QkFBdUIsR0FBRyxJQUFpQixDQUFDO1FBS2hELE1BQU0sSUFBSSxHQUFHLElBQUksQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDO1FBRTlCLElBQUEscURBQXVCLEVBQUMsRUFBRSxPQUFPLEVBQUUsSUFBSSxFQUFFLENBQUMsQ0FBQztRQUUzQyxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0F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