"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericRESTService = void 0;
const btoa = require("btoa");
const CryptoJS = require("crypto-js");
const Utils_js_1 = require("../common/Utils.js");
const LevelLogs_js_1 = require("../common/LevelLogs.js");
let packageVersion = require("../../package.json");
class GenericRESTService extends LevelLogs_js_1.LevelLogs {
    constructor(_logger, logId = "UNDF/SVCE - ") {
        super();
        this.startingInfos = {
            constructorDate: new Date(),
            startDate: new Date(),
            startedDate: new Date(),
            initilizedDate: new Date(),
            readyDate: new Date()
        };
        this.setLogLevels(this);
        let that = this;
        that._logger = _logger;
        if (logId) {
            that._logId = logId;
        }
        // that._logger.log("debug", that._logId + "(GenericService::constructor) " );
        that.setConstructed();
    }
    setConstructed() {
        let that = this;
        that.startingInfos.constructorDate = new Date();
        that._logger.log(that.INFO, that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
    }
    set p_token(value) {
        this._token = value;
    }
    set p_credentials(value) {
        this._credentials = value;
    }
    set p_application(value) {
        this._application = value;
    }
    set p_auth(value) {
        this._auth = value;
    }
    get token() {
        return this._token;
    }
    get credentials() {
        return this._credentials;
    }
    get application() {
        return this._application;
    }
    get auth() {
        return this._auth;
    }
    isUserCredentialsLogin() {
        return ((0, Utils_js_1.isDefinedAndNotEmpty)(this._credentials.login) && (0, Utils_js_1.isDefinedAndNotEmpty)(this._credentials.password) && (0, Utils_js_1.isNotDefinedOrEmpty)(this._credentials.apikey));
    }
    isAPIKeyCredentialsLogin() {
        return ((0, Utils_js_1.isNotDefinedOrEmpty)(this._credentials.login) && (0, Utils_js_1.isNotDefinedOrEmpty)(this._credentials.password) && (0, Utils_js_1.isDefinedAndNotEmpty)(this._credentials.apikey));
    }
    get p_decodedtokenRest() {
        return this._decodedtokenRest;
    }
    set p_decodedtokenRest(value) {
        this._decodedtokenRest = value;
    }
    getRequestHeader(accept = undefined) {
        let that = this;
        let headers = {
            //    "Authorization": "Bearer " + that._token,
            "Accept": accept || "application/json",
            "Range": undefined,
            "x-rainbow-client": "sdk_node",
            "x-rainbow-client-version": packageVersion.version,
            "x-rainbow-client-id": that.application ? that.application.appID : "",
            "x-rainbow-correlation-id": (0, Utils_js_1.makeId)(16)
            //"x-rainbow-request-node-id" :  makeId(9)
        };
        //let caller = arguments.callee.caller.toString();
        // let caller = arguments.callee.caller.prototype;
        //console.log("caller : ", caller);
        if (that._credentials.apikey) {
            /*var uuid = require('uuid');
            var btoa = require('btoa');
            var cryptojs = require('crypto-js');
            //*/
            headers['x-rainbow-api-key'] = that._credentials.apikey;
            let challenge = (0, Utils_js_1.makeId)(22);
            //console.log('challenge: ' + challenge);
            headers['x-rainbow-challenge'] = challenge;
            let xapp = "XXX";
            // Basic <base64encode(appId:sha256(appSecret challenge))> (Concatenation of appSecret and provided challenge, hashed with sha256).
            let appIdNet = that.application.appID;
            //console.log('appIdNet: ' + appIdNet);
            let appSecretNet = that.application.appSecret;
            //console.log('appSecretNet: ' + appSecretNet);
            let toEncrypt = appSecretNet + challenge;
            //console.log('appSecretNet + challenge : ' + toEncrypt);
            let encrypted = CryptoJS.SHA256(toEncrypt).toString();
            //console.log('appSecretNet + challenge encrypted : ' + encrypted);
            let appCode = appIdNet + ':' + encrypted;
            //console.log('appIdNet:encrypted : ' + appCode);
            let base64 = btoa(appCode);
            //console.log('base64: ' + base64);
            xapp = base64;
            headers["x-rainbow-app-auth"] = "Basic " + xapp;
        }
        else {
            headers["Authorization"] = "Bearer " + that._token;
        }
        return headers;
    }
    getRequestHeaderLowercaseAccept(accept = undefined) {
        let that = this;
        let headers = {
            //"Authorization": "Bearer " + that._token,
            "accept": accept || "application/json",
            "x-rainbow-client": "sdk_node",
            "x-rainbow-client-version": packageVersion.version,
            "x-rainbow-client-id": that.application ? that.application.appID : "",
            "x-rainbow-correlation-id": (0, Utils_js_1.makeId)(16)
            //"x-rainbow-request-node-id" :  makeId(9)
        };
        if (that._credentials.apikey) {
            /*var uuid = require('uuid');
            var btoa = require('btoa');
            var cryptojs = require('crypto-js');
            //*/
            headers['x-rainbow-api-key'] = that._credentials.apikey;
            let challenge = (0, Utils_js_1.makeId)(22);
            //   console.log('challenge: ' + challenge);
            headers['x-rainbow-challenge'] = challenge;
            let xapp = "XXX";
            // Basic <base64encode(appId:sha256(appSecret challenge))> (Concatenation of appSecret and provided challenge, hashed with sha256).
            let appIdNet = that.application.appID;
            // console.log('appIdNet: ' + appIdNet);
            let appSecretNet = that.application.appSecret;
            //console.log('appSecretNet: ' + appSecretNet);
            let toEncrypt = appSecretNet + challenge;
            //            console.log('appSecretNet + challenge : ' + toEncrypt);
            let encrypted = CryptoJS.SHA256(toEncrypt).toString();
            //          console.log('appSecretNet + challenge encrypted : ' + encrypted);
            let appCode = appIdNet + ':' + encrypted;
            //        console.log('appIdNet:encrypted : ' + appCode);
            let base64 = btoa(appCode);
            //      console.log('base64: ' + base64);
            xapp = base64;
            headers["x-rainbow-app-auth"] = "Basic " + xapp;
        }
        else {
            headers["Authorization"] = "Bearer " + that._token;
        }
        return headers;
    }
    getRequestHeaderWithRange(accept = undefined, range = undefined) {
        let that = this;
        let header = that.getRequestHeader(accept);
        header.Range = range;
        return header;
    }
    getPostHeader(contentType = undefined) {
        let that = this;
        let header = that.getRequestHeader(undefined);
        let type = contentType || "application/json";
        header["Content-Type"] = type;
        return header;
    }
    ;
    getPostHeaderWithRange(accept = undefined, initialSize = undefined, minRange = undefined, maxRange = undefined) {
        let that = this;
        let header = this.getRequestHeader(accept);
        // Content-Range: bytes 0-1048575/2960156
        //header["Content-Range"] = "bytes " + minRange + "-" + maxRange + "/" + initialSize;
        return header;
    }
    ;
    getLoginHeader(auth = undefined, password = undefined) {
        let that = this;
        let headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "x-rainbow-client": "sdk_node",
            "x-rainbow-client-version": packageVersion.version,
            "x-rainbow-client-id": that.application ? that.application.appID : "",
            "x-rainbow-correlation-id": (0, Utils_js_1.makeId)(16)
            //"x-rainbow-request-node-id" :  makeId(9)
        };
        if (that._credentials.apikey) {
            /*var uuid = require('uuid');
            var btoa = require('btoa');
            var cryptojs = require('crypto-js');
            //*/
            headers['x-rainbow-api-key'] = that._credentials.apikey;
            let challenge = (0, Utils_js_1.makeId)(22);
            //  console.log('challenge: ' + challenge);
            headers['x-rainbow-challenge'] = challenge;
            let xapp = "XXX";
            // Basic <base64encode(appId:sha256(appSecret challenge))> (Concatenation of appSecret and provided challenge, hashed with sha256).
            let appIdNet = that.application.appID;
            //            console.log('appIdNet: ' + appIdNet);
            let appSecretNet = that.application.appSecret;
            //          console.log('appSecretNet: ' + appSecretNet);
            let toEncrypt = appSecretNet + challenge;
            //        console.log('appSecretNet + challenge : ' + toEncrypt);
            let encrypted = CryptoJS.SHA256(toEncrypt).toString();
            //      console.log('appSecretNet + challenge encrypted : ' + encrypted);
            let appCode = appIdNet + ':' + encrypted;
            //    console.log('appIdNet:encrypted : ' + appCode);
            let base64 = btoa(appCode);
            //  console.log('base64: ' + base64);
            xapp = base64;
            headers["x-rainbow-app-auth"] = "Basic " + xapp;
        }
        else {
            headers["Authorization"] = "Basic " + (auth || that._auth);
            let toEncrypt = that._application.appSecret + (password || that._credentials.password);
            //that._logger.log(that.DEBUG, LOG_ID + "toEncrypt : " + toEncrypt);
            let encrypted = CryptoJS.SHA256(toEncrypt).toString();
            //that._logger.log(that.DEBUG, LOG_ID + "encrypted : " + encrypted);
            let base64 = btoa(that._application.appID + ':' + encrypted);
            //that._logger.log(that.DEBUG, LOG_ID + "base64 : " + base64);
            if (that._application.appSecret && base64 && base64.length) {
                headers["x-rainbow-app-auth"] = "Basic " + base64 || "";
            }
        }
        return headers;
    }
    ;
    getDefaultHeader() {
        let that = this;
        return {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "x-rainbow-client": "sdk_node",
            "x-rainbow-client-version": packageVersion.version,
            "x-rainbow-client-id": that.application ? that.application.appID : "",
            "x-rainbow-correlation-id": (0, Utils_js_1.makeId)(16)
            //"x-rainbow-request-node-id" :  makeId(9)
        };
    }
    ;
}
exports.GenericRESTService = GenericRESTService;
module.exports = { 'GenericRESTService': GenericRESTService };
//# sourceMappingURL=GenericRESTService.js.map