"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 HTTPoverXMPP_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HTTPoverXMPP = void 0;
const ErrorManager_1 = require("../common/ErrorManager");
const Utils_1 = require("../common/Utils");
const PubSub = require("pubsub-js");
const GenericService_1 = require("./GenericService");
const LOG_ID = "HTTPoverXMPP/SVCE - ";
const API_ID = "API_CALL - ";
let HTTPoverXMPP = HTTPoverXMPP_1 = 
/**
 * @module
 * @name HTTPoverXMPP
 * @version 2.42.0-lts.4 - Documentation generated on 2026/04/24T11:38:22.926Z
 * @public
 * @description
 *      This service manages an HTTP over XMPP requests system.<br>
 */
class HTTPoverXMPP extends GenericService_1.GenericService {
    static getClassName() { return 'HTTPoverXMPP'; }
    getClassName() { return HTTPoverXMPP_1.getClassName(); }
    static getAccessorName() { return 'httpoverxmpp'; }
    getAccessorName() { return HTTPoverXMPP_1.getAccessorName(); }
    constructor(_core, _eventEmitter, _http, _logger, _startConfig) {
        super(_logger, LOG_ID, _eventEmitter);
        this._protocol = null;
        this._host = null;
        this._port = null;
        this.setLogLevels(this);
        this._xmpp = null;
        this._rest = null;
        this._s2s = null;
        this._options = {};
        this._useXMPP = false;
        this._useS2S = false;
        this._eventEmitter = _eventEmitter;
        this._logger = _logger;
        this._startConfig = _startConfig;
        this._protocol = _http.protocol;
        this._host = _http.host;
        this._port = _http.port;
        this._core = _core;
        this.avatarDomain = this._host.split(".").length === 2 ? this._protocol + "://cdn." + this._host + ":" + this._port : this._protocol + "://" + this._host + ":" + this._port;
        // this._eventEmitter.on("evt_internal_createrbvoice", this.onCreateRBVoice.bind(this));
        // this._eventEmitter.on("evt_internal_deleterbvoice", this.onDeleteRBVoice.bind(this));
    }
    start(_options) {
        let that = this;
        that.initStartDate();
        return new Promise(async function (resolve, reject) {
            try {
                that._xmpp = that._core._xmpp;
                that._rest = that._core._rest;
                that._options = _options;
                that._s2s = that._core._s2s;
                that._useXMPP = that._options.useXMPP;
                that._useS2S = that._options.useS2S;
                //that._rbvoice = [];
                that.attachHandlers();
                that.setStarted();
                resolve(undefined);
            }
            catch (err) {
                return reject();
            }
        });
    }
    stop() {
        let that = this;
        return new Promise(function (resolve, reject) {
            try {
                that._xmpp = null;
                that._rest = null;
                if (that.hTTPoverXMPPHandlerToken) {
                    that.hTTPoverXMPPHandlerToken.forEach((token) => PubSub.unsubscribe(token));
                }
                that.hTTPoverXMPPHandlerToken = [];
                //that._rbvoice = null;
                that.setStopped();
                resolve(undefined);
            }
            catch (err) {
                return reject(err);
            }
        });
    }
    async init(useRestAtStartup) {
        let that = this;
        that.setInitialized();
    }
    attachHandlers() {
        let that = this;
        //that.httpoverxmppEventHandler = that._xmpp.httpoverxmppEventHandler;
        that.hTTPoverXMPPHandlerToken = [
        //PubSub.subscribe( that._xmpp.hash + "." + that.rbvoiceEventHandler.MESSAGE_MANAGEMENT, that.rbvoiceEventHandler.onManagementMessageReceived.bind(that.rbvoiceEventHandler)),
        //PubSub.subscribe( that._xmpp.hash + "." + that.rbvoiceEventHandler.MESSAGE_ERROR, that.rbvoiceEventHandler.onErrorMessageReceived.bind(that.rbvoiceEventHandler))
        ];
    }
    //region Rainbow HTTPoverXMPP
    /**
     * @public
     * @nodered true
     * @method get
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to send a GET http request to an XMPP server supporting Xep0332. <br>
     * @param {string} urlToGet The url to request
     * @param {Object} headers={} The Http Headers used to web request.
     * @param {string} httpoverxmppserver_jid the jid of the http over xmpp server used to retrieve the HTTP web request. default value is the jid of the account running the SDK.
     * @return {Promise<any>} An object of the result
     */
    get(urlToGet, headers = {}, httpoverxmppserver_jid) {
        let that = this;
        that._logger.log(that.INFOAPI, LOG_ID + API_ID + "(get) is urlToGet defined : ", (0, Utils_1.isDefined)(urlToGet));
        return new Promise(async (resolve, reject) => {
            if (!urlToGet) {
                that._logger.log(that.ERROR, LOG_ID + "(get) Parameter 'urlToGet' is missing or null");
                throw ErrorManager_1.ErrorManager.getErrorManager().BAD_REQUEST();
            }
            if (!httpoverxmppserver_jid) {
                httpoverxmppserver_jid = that._xmpp.fullJid;
            }
            try {
                let node = await that._xmpp.getHTTPoverXMPP(urlToGet, httpoverxmppserver_jid, headers);
                that._logger.log(that.DEBUG, "(get) - sent.");
                that._logger.log(that.INTERNAL, "(get) - result : ", node);
                let xmlNodeStr = node ? node.toString() : "<xml></xml>";
                let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
                resolve(reqObj);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(get) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(get) Error : ", err);
                return reject(err);
            }
        });
    }
    /**
     * @public
     * @nodered true
     * @method discoverHTTPoverXMPP
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to send a discover presence to a bare jid to find the resources availables. <br>
     * @param {Object} headers={} The Http Headers used to web request.
     * @param {string} httpoverxmppserver_jid the jid of the http over xmpp server used to retrieve the HTTP web request. default value is the jid of the account running the SDK.
     * @return {Promise<any>} An object of the result
     */
    discoverHTTPoverXMPP(headers = {}, httpoverxmppserver_jid) {
        let that = this;
        that._logger.log(that.INFOAPI, LOG_ID + API_ID + "(discoverHTTPoverXMPP) is headers defined : ", (0, Utils_1.isDefined)(headers));
        return new Promise(async (resolve, reject) => {
            if (!httpoverxmppserver_jid) {
                httpoverxmppserver_jid = that._rest.account.jid;
            }
            try {
                let node = await that._xmpp.discoverHTTPoverXMPP(httpoverxmppserver_jid, headers);
                that._logger.log(that.DEBUG, "(discoverHTTPoverXMPP) - sent.");
                that._logger.log(that.INTERNAL, "(discoverHTTPoverXMPP) - result : ", node);
                let xmlNodeStr = node ? node.toString() : "<xml></xml>";
                let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
                resolve(reqObj);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(discoverHTTPoverXMPP) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(discoverHTTPoverXMPP) Error : ", err);
                return reject(err);
            }
        });
    }
    /**
     * @public
     * @nodered true
     * @method trace
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to send a TRACE http request to an XMPP server supporting Xep0332. TRACE is only used for debugging <br>
     * @param {string} urlToTrace The url to request
     * @param {Object} headers={} The Http Headers used to web request.
     * @param {string} httpoverxmppserver_jid the jid of the http over xmpp server used to retrieve the HTTP web request. default value is the jid of the account running the SDK.
     * @return {Promise<any>} An object of the result
     */
    trace(urlToTrace, headers = {}, httpoverxmppserver_jid) {
        let that = this;
        that._logger.log(that.INFOAPI, LOG_ID + API_ID + "(trace) is urlToTrace defined : ", (0, Utils_1.isDefined)(urlToTrace));
        return new Promise(async (resolve, reject) => {
            if (!urlToTrace) {
                that._logger.log(that.ERROR, LOG_ID + "(trace) Parameter 'urlToTrace' is missing or null");
                throw ErrorManager_1.ErrorManager.getErrorManager().BAD_REQUEST();
            }
            if (!httpoverxmppserver_jid) {
                httpoverxmppserver_jid = that._xmpp.fullJid;
            }
            try {
                that._logger.log(that.INTERNAL, LOG_ID + "(trace) Parameter urlToTrace : ", urlToTrace, ", headers : ", headers, ", httpoverxmppserver_jid : ", httpoverxmppserver_jid);
                let node = await that._xmpp.traceHTTPoverXMPP(urlToTrace, httpoverxmppserver_jid, headers);
                that._logger.log(that.DEBUG, "(trace) - sent.");
                that._logger.log(that.INTERNAL, "(trace) - result : ", node);
                let xmlNodeStr = node ? node.toString() : "<xml></xml>";
                let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
                resolve(reqObj);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(trace) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(trace) Error : ", err);
                return reject(err);
            }
        });
    }
    /**
     * @public
     * @nodered true
     * @method head
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to send a HEAD http request to an XMPP server supporting Xep0332. <br>
     * @param {string} urlToHead The url to request
     * @param {Object} headers={} The Http Headers used to web request.
     * @param {string} httpoverxmppserver_jid the jid of the http over xmpp server used to retrieve the HTTP web request. default value is the jid of the account running the SDK.
     * @return {Promise<any>} An object of the result
     */
    head(urlToHead, headers = {}, httpoverxmppserver_jid) {
        let that = this;
        that._logger.log(that.INFOAPI, LOG_ID + API_ID + "(head) is urlToHead defined : ", (0, Utils_1.isDefined)(urlToHead));
        return new Promise(async (resolve, reject) => {
            if (!urlToHead) {
                that._logger.log(that.ERROR, LOG_ID + "(head) Parameter 'urlToGet' is missing or null");
                throw ErrorManager_1.ErrorManager.getErrorManager().BAD_REQUEST();
            }
            if (!httpoverxmppserver_jid) {
                httpoverxmppserver_jid = that._xmpp.fullJid;
            }
            try {
                let node = await that._xmpp.headHTTPoverXMPP(urlToHead, httpoverxmppserver_jid, headers);
                that._logger.log(that.DEBUG, "(head) - sent.");
                that._logger.log(that.INTERNAL, "(head) - result : ", node);
                let xmlNodeStr = node ? node.toString() : "<xml></xml>";
                let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
                resolve(reqObj);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(head) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(head) Error : ", err);
                return reject(err);
            }
        });
    }
    /**
     * @public
     * @nodered true
     * @method post
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to send a POST http request to an XMPP server supporting Xep0332. <br>
     * @param {string} urlToPost The url to request
     * @param {Object} headers={} The Http Headers used to web request.
     * @param {string} data The body data of the http request.
     * @param {string} httpoverxmppserver_jid the jid of the http over xmpp server used to retrieve the HTTP web request. default value is the jid of the account running the SDK.
     * @return {Promise<any>} An object of the result
     */
    post(urlToPost, headers = {}, data, httpoverxmppserver_jid) {
        let that = this;
        that._logger.log(that.INFOAPI, LOG_ID + API_ID + "(post) is urlToPost defined : ", (0, Utils_1.isDefined)(urlToPost));
        return new Promise(async (resolve, reject) => {
            if (!urlToPost) {
                that._logger.log(that.ERROR, LOG_ID + "(post) Parameter 'urlToPost' is missing or null");
                throw ErrorManager_1.ErrorManager.getErrorManager().BAD_REQUEST();
            }
            if (!httpoverxmppserver_jid) {
                httpoverxmppserver_jid = that._xmpp.fullJid;
            }
            try {
                let node = await that._xmpp.postHTTPoverXMPP(urlToPost, httpoverxmppserver_jid, headers, data);
                that._logger.log(that.DEBUG, "(post) - sent.");
                that._logger.log(that.INTERNAL, "(post) - result : ", node);
                let xmlNodeStr = node ? node.toString() : "<xml></xml>";
                let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
                resolve(reqObj);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(post) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(post) Error : ", err);
                return reject(err);
            }
        });
    }
    /**
     * @public
     * @nodered true
     * @method put
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to send a PUT http request to an XMPP server supporting Xep0332. <br>
     * @param {string} urlToPost The url to request
     * @param {Object} headers={} The Http Headers used to web request.
     * @param {string} data The body data of the http request.
     * @param {string} httpoverxmppserver_jid the jid of the http over xmpp server used to retrieve the HTTP web request. default value is the jid of the account running the SDK.
     * @return {Promise<any>} An object of the result
     */
    put(urlToPost, headers = {}, data, httpoverxmppserver_jid) {
        let that = this;
        that._logger.log(that.INFOAPI, LOG_ID + API_ID + "(put) is urlToPost defined : ", (0, Utils_1.isDefined)(urlToPost));
        return new Promise(async (resolve, reject) => {
            if (!urlToPost) {
                that._logger.log(that.ERROR, LOG_ID + "(put) Parameter 'urlToPost' is missing or null");
                throw ErrorManager_1.ErrorManager.getErrorManager().BAD_REQUEST();
            }
            if (!httpoverxmppserver_jid) {
                httpoverxmppserver_jid = that._xmpp.fullJid;
            }
            try {
                let node = await that._xmpp.putHTTPoverXMPP(urlToPost, httpoverxmppserver_jid, headers, data);
                that._logger.log(that.DEBUG, "(put) - sent.");
                that._logger.log(that.INTERNAL, "(put) - result : ", node);
                let xmlNodeStr = node ? node.toString() : "<xml></xml>";
                let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
                resolve(reqObj);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(put) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(put) Error : ", err);
                return reject(err);
            }
        });
    }
    /**
     * @public
     * @nodered true
     * @method delete
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to send a DELETE http request to an XMPP server supporting Xep0332. <br>
     * @param {string} urlToPost The url to request
     * @param {Object} headers={} The Http Headers used to web request.
     * @param {string} data The body data of the http request.
     * @param {string} httpoverxmppserver_jid the jid of the http over xmpp server used to retrieve the HTTP web request. default value is the jid of the account running the SDK.
     * @return {Promise<any>} An object of the result
     */
    delete(urlToPost, headers = {}, data, httpoverxmppserver_jid) {
        let that = this;
        that._logger.log(that.INFOAPI, LOG_ID + API_ID + "(delete) is urlToPost defined : ", (0, Utils_1.isDefined)(urlToPost));
        return new Promise(async (resolve, reject) => {
            if (!urlToPost) {
                that._logger.log(that.ERROR, LOG_ID + "(delete) Parameter 'urlToPost' is missing or null");
                throw ErrorManager_1.ErrorManager.getErrorManager().BAD_REQUEST();
            }
            if (!httpoverxmppserver_jid) {
                httpoverxmppserver_jid = that._xmpp.fullJid;
            }
            try {
                let node = await that._xmpp.deleteHTTPoverXMPP(urlToPost, httpoverxmppserver_jid, headers, data);
                that._logger.log(that.DEBUG, "(delete) - sent.");
                that._logger.log(that.INTERNAL, "(delete) - result : ", node);
                let xmlNodeStr = node ? node.toString() : "<xml></xml>";
                let reqObj = await (0, Utils_1.getJsonFromXML)(xmlNodeStr);
                resolve(reqObj);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(delete) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(delete) Error : ", err);
                return reject(err);
            }
        });
    }
    /**
     * @private
     * @nodered true
     * @method discover
     * @since 2.10.0
     * @instance
     * @async
     * @category Rainbow HTTPoverXMPP
     * @description
     *    This API allows to get the supported XMPP services. <br>
     * @return {Promise<any>} An object of the result
     */
    discover() {
        let that = this;
        return new Promise(async (resolve, reject) => {
            try {
                let result = await that._xmpp.discover();
                that._logger.log(that.DEBUG, "(discover) - sent.");
                that._logger.log(that.INTERNAL, "(discover) - result : ", result);
                resolve(result);
            }
            catch (err) {
                that._logger.log(that.ERROR, LOG_ID + "(discover) Error.");
                that._logger.log(that.INTERNALERROR, LOG_ID + "(discover) Error : ", err);
                return reject(err);
            }
        });
    }
};
exports.HTTPoverXMPP = HTTPoverXMPP;
exports.HTTPoverXMPP = HTTPoverXMPP = HTTPoverXMPP_1 = __decorate([
    (0, Utils_1.logEntryExit)(LOG_ID),
    (0, Utils_1.isStarted)([])
    /**
     * @module
     * @name HTTPoverXMPP
     * @version 2.42.0-lts.4 - Documentation generated on 2026/04/24T11:38:22.926Z
     * @public
     * @description
     *      This service manages an HTTP over XMPP requests system.<br>
     */
], HTTPoverXMPP);
module.exports.HTTPoverXMPP = HTTPoverXMPP;
//# sourceMappingURL=HTTPoverXMPPService.js.map