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