"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const stompjs_1 = require("@stomp/stompjs");
const events = require("events");
const SockJS = require("sockjs-client");
class StompClient extends events.EventEmitter {
    constructor(options) {
        super();
        const { apiKey, url, client } = options;
        var self = this;
        this.url = url;
        this.client = client;
        this.apiKey = apiKey;
        this.headers = {
            'X-Authorization': 'Bearer ' + apiKey,
        };
        this.stompClient = new stompjs_1.Client({
            brokerURL: 'wss:' + this.url.split(':')[1] + '/ws',
            reconnectDelay: 5000,
            heartbeatIncoming: 5000,
            heartbeatOutgoing: 5000,
        });
        this.stompClient.onConnect = function (frame) {
            self.connected = true;
            self.connecting = false;
            self.register();
            self.emit('connected', frame);
        };
        this.stompClient.onDisconnect = function () {
            self.connected = false;
            self.connecting = false;
            self.emit('disconnected');
        };
        this.stompClient.onStompError = function (frame) {
            self.connected = false;
            self.connecting = false;
            console.error('Broker reported error: ' + frame.headers['message']);
            console.error('Additional details: ' + frame.body);
            self.emit('error', frame);
        };
    }
    connect() {
        var self = this;
        this.connecting = true;
        this.connected = false;
        this.stompClient.deactivate();
        setTimeout(() => {
            self.stompClient.webSocketFactory = function () {
                return new SockJS(self.url + '/ws');
            };
            self.stompClient.activate();
            self.checkConnected();
        }, 2000);
    }
    register() {
        var self = this;
        const { accountId, id } = this.client;
        if (this.connected) {
            this.stompClient.publish({
                headers: this.headers,
                destination: `/app/events/register/${accountId}/client/${id}/queues`,
                body: JSON.stringify({}),
            });
            self.accountSubscription = this.stompClient.subscribe(`/topic/events/${accountId}`, (msg) => {
                self.emit('msg', JSON.parse(msg.body));
            }, self.headers);
            self.clientSubscription = self.stompClient.subscribe(`/topic/call-events/${accountId}/client/${id}`, (msg) => {
                self.emit('msg', JSON.parse(msg.body));
            }, self.headers);
        }
    }
    checkConnected() {
        var self = this;
        setTimeout(() => {
            if (self.connecting && !self.connected) {
                console.error({ client: self.client }, 'Not connected');
                self.connect();
            }
            else {
                console.log('Connected', self.client);
            }
        }, 5000);
    }
    disconnect() {
        if (this.connected) {
            if (this.accountSubscription) {
                this.accountSubscription.unsubscribe();
            }
            if (this.clientSubscription) {
                this.clientSubscription.unsubscribe();
            }
            this.connecting = false;
            this.connected = false;
            this.stompClient.deactivate();
        }
    }
}
exports.default = StompClient;
