"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var RBVoiceEventHandler_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RBVoiceEventHandler = void 0;
const XMPPUtils_1 = require("../../common/XMPPUtils");
const Utils_1 = require("../../common/Utils");
const GenericHandler_1 = require("./GenericHandler");
const Utils = require("../../common/Utils");
const xml = require("@xmpp/xml");
const PromiseQueue = require("../../common/promiseQueue");
const prettydata = require("../pretty-data").pd;
const LOG_ID = "XMPP/HNDL/RBVOICE - ";
let RBVoiceEventHandler = RBVoiceEventHandler_1 = class RBVoiceEventHandler extends GenericHandler_1.GenericHandler {
    static getClassName() { return 'RBVoiceEventHandler'; }
    getClassName() { return RBVoiceEventHandler_1.getClassName(); }
    static getAccessorName() { return 'rbvoiceevent'; }
    getAccessorName() { return RBVoiceEventHandler_1.getAccessorName(); }
    constructor(xmppService, core) {
        super(xmppService);
        let that = this;
        this.MESSAGE = "jabber:client.message";
        this.IQ_RESULT = "jabber:client.iq.result";
        this.IQ_ERROR = "jabber:client.iq.error";
        this.MESSAGE_MANAGEMENT = "jabber:client.message.management";
        this.MESSAGE_ERROR = "jabber:client.message.error";
        this.MESSAGE_HEADLINE = "jabber:client.message.headline";
        /*
        this.MESSAGE_GROUPCHAT = "jabber:client.message.groupchat";
        this.MESSAGE_WEBRTC = "jabber:client.message.webrtc";
        this.MESSAGE_MANAGEMENT = "jabber:client.message.management";
        this.MESSAGE_ERROR = "jabber:client.message.error";
        this.MESSAGE_HEADLINE = "jabber:client.message.headline";
        this.MESSAGE_CLOSE = "jabber:client.message.headline";
        // */
        this.xmppUtils = XMPPUtils_1.XMPPUTils.getXMPPUtils();
        this._core = core;
        this.contactsService = core.contacts;
        this._profilesService = core.profiles;
        this.promiseQueue = PromiseQueue.createPromiseQueue(that._logger);
    }
    onIqResultReceived(msg, stanza) {
        let that = this;
        let children = stanza.children;
        children.forEach((node) => {
            switch (node.getName()) {
                case "default":
                    //that._logger.log(that.WARN, LOG_ID + "(handleXMPPConnection, onIqResultReceived) not managed - 'stanza'", node.getName());
                    break;
                default:
                //that
                //  .logger
                //.log("warn", LOG_ID + "(handleXMPPConnection, onIqResultReceived) child not managed for iq - 'stanza'", node.getName());
            }
        });
    }
    ;
    onErrorMessageReceived(msg, stanzaTab) {
        let that = this;
        let stanza = stanzaTab[0];
        let prettyStanza = stanzaTab[1];
        let jsonStanza = stanzaTab[2];
        try {
            if (stanza.getChild('no-store') != undefined) {
                // // Treated in conversation handler that._logger.log(that.ERROR, LOG_ID + "(onErrorMessageReceived) The 'to' of the message can not received the message");
            }
            else {
                //that._logger.log(that.ERROR, LOG_ID + "(onErrorMessageReceived) something goes wrong...");
                that._logger.log(that.ERROR, LOG_ID + "(onErrorMessageReceived) something goes wrong... : ", msg, "\n", prettyStanza);
                that.eventEmitter.emit("evt_internal_xmpperror", msg);
            }
        }
        catch (err) {
            // that._logger.log(that.ERROR, LOG_ID + "(onErrorMessageReceived) CATCH Error !!! ");
            that._logger.log(that.ERROR, LOG_ID + "(onErrorMessageReceived) CATCH Error !!! : ", err);
        }
    }
    ;
    async onMessageReceived(msg, stanzaTab) {
        let that = this;
        let stanza = stanzaTab[0];
        let prettyStanza = stanzaTab[1];
        let jsonStanza = stanzaTab[2];
        that._logger.log(that.INTERNAL, LOG_ID + "(onMessageReceived) _entering_ : ", msg, prettyStanza);
        try {
            let stanzaElem = stanza;
            //let that = this;
            /*let xmlNodeStr = stanza ? stanza.toString():"<xml></xml>";
            let reqObj = await getJsonFromXML(xmlNodeStr);
            that._logger.log(that.DEBUG, LOG_ID + "(onMessageReceived) reqObj : ", reqObj);
            // */
            that._logger.log(that.DEBUG, LOG_ID + "(onMessageReceived) jsonStanza : ", jsonStanza);
            // Ignore "Offline" message
            let delay = stanzaElem.getChild("delay");
            if (delay && delay.text() === "Offline Storage") {
                return true;
            }
            let from = stanza.attrs.from;
            let to = stanza.attrs.to;
        }
        catch (error) {
            // that._logger.log(that.ERROR, LOG_ID + "(onMessageReceived) CATCH Error !!! -- failure -- ");
            that._logger.log(that.ERROR, LOG_ID + "(onMessageReceived) CATCH Error !!! -- failure -- : ", error);
            //return true;
        }
        that._logger.log(that.DEBUG, LOG_ID + "(onMessageReceived) _exiting_");
        return true;
    }
    async onManagementMessageReceived(msg, stanzaTab) {
        let that = this;
        let stanza = stanzaTab[0];
        let prettyStanza = stanzaTab[1];
        let jsonStanza = stanzaTab[2];
        that._logger.log(that.INTERNAL, LOG_ID + "(onManagementMessageReceived) _entering_ : ", msg, prettyStanza);
        try {
            let stanzaElem = stanza;
            //let that = this;
            let xmlNodeStr = stanza ? stanza.toString() : "<xml></xml>";
            let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
            that._logger.log(that.DEBUG, LOG_ID + "(onManagementMessageReceived) reqObj : ", reqObj);
            // Ignore "Offline" message
            let delay = stanzaElem.getChild("delay");
            if (delay && delay.text() === "Offline Storage") {
                return true;
            }
            let from = stanza.attrs.from;
            let to = stanza.attrs.to;
        }
        catch (error) {
            // that._logger.log(that.ERROR, LOG_ID + "(onManagementMessageReceived) CATCH Error !!! -- failure -- ");
            that._logger.log(that.ERROR, LOG_ID + "(onManagementMessageReceived) CATCH Error !!! -- failure -- : ", error);
            //return true;
        }
        that._logger.log(that.DEBUG, LOG_ID + "(onManagementMessageReceived) _exiting_");
        return true;
    }
    async onHeadlineMessageReceived(msg, stanzaTab) {
        let that = this;
        let stanza = stanzaTab[0];
        let prettyStanza = stanzaTab[1];
        let jsonStanza = stanzaTab[2];
        that._logger.log(that.INTERNAL, LOG_ID + "(onHeadlineMessageReceived) _entering_ : ", msg, prettyStanza);
        try {
            let stanzaElem = stanza;
            //let that = this;
            //let xmlNodeStr = stanza ? stanza.toString():"<xml></xml>";
            let stanzaObj = jsonStanza; //await getJsonFromXML(xmlNodeStr);
            that._logger.log(that.DEBUG, LOG_ID + "(onHeadlineMessageReceived) stanzaObj : ", stanzaObj);
            if (stanzaObj && stanzaObj.message) {
                let from = stanzaObj.message.$attrs.from;
                let to = stanzaObj.message.$attrs.to;
                if (stanzaObj.message.telephony && stanzaObj.message.telephony.event) {
                    let evtParam = stanzaObj.message.telephony.event;
                    that.eventEmitter.emit("evt_internal_rbvoiceevent", evtParam);
                }
            }
        }
        catch (error) {
            // that._logger.log(that.ERROR, LOG_ID + "(onHeadlineMessageReceived) CATCH Error !!! -- failure -- ");
            that._logger.log(that.ERROR, LOG_ID + "(onHeadlineMessageReceived) CATCH Error !!! -- failure -- : ", error);
            //return true;
        }
        that._logger.log(that.DEBUG, LOG_ID + "(onHeadlineMessageReceived) _exiting_");
        return true;
    }
};
exports.RBVoiceEventHandler = RBVoiceEventHandler;
exports.RBVoiceEventHandler = RBVoiceEventHandler = RBVoiceEventHandler_1 = __decorate([
    (0, Utils_1.logEntryExit)(LOG_ID)
], RBVoiceEventHandler);
module.exports.RBVoiceEventHandler = RBVoiceEventHandler;
//# sourceMappingURL=RBVoiceEventHandler.js.map