"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const socket_client_1 = require("./socket-client");
const debug = require('debug')('member-client');
/**
 * Member Node Client
 */
class MemberNodeClient {
    /**
     * Constructor
     *
     * @param {SocketClusterClient} socketClusterClient SocketCluster Client
     * @param {any}                 httpClient          HTTP Client
     */
    constructor(stateService) {
        this.stateService = stateService;
        this.token = '';
        this.connSubject = new rxjs_1.BehaviorSubject(false);
        this.events = new rxjs_1.Subject();
        this.socket = new socket_client_1.SocketClient(this.stateService);
        this.socket.on('close', () => {
            this.connSubject.next(false);
            return true;
        });
    }
    get events$() {
        return this.events.asObservable();
    }
    /**
     * Connect to Member Node
     *
     * @return void
     */
    connect() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.socket.connect();
        });
    }
    disconnect() {
        this.socket.disconnect();
    }
    connected() {
        return this.connSubject.asObservable();
    }
    /**
     * Send Request
     *
     * @param {any} messageBody Message Body JSON Object
     *
     * @returns {Promise<any>}
     */
    sendRequest(messageBody) {
        return __awaiter(this, void 0, void 0, function* () {
            debug('<- message %o', messageBody);
            this.events.next({ type: 'mnSocketRequest', payload: Object.assign(Object.assign({}, messageBody), { token: this.stateService.token }) });
            return new Promise((resolve, reject) => {
                this.socket.sendRequest([
                    {
                        MessageBody: Object.assign(Object.assign({}, messageBody), { token: this.stateService.token }),
                    },
                    (error, data) => {
                        debug('message -> %o', data);
                        this.events.next({ type: 'mnSocketResponse', payload: data });
                        if (error) {
                            reject(error);
                        }
                        resolve(data);
                    }
                ]);
            });
        });
    }
    /**
     * Get Wallets For Current User
     *
     * @returns {Promise<any>}
     */
    walletAccess() {
        return this.sendRequest({
            RequestName: 'getmywalletaccess',
        });
    }
    newWallet(account, walletName, walletType) {
        return this.sendRequest({
            RequestName: 'nw',
            walletName,
            account: String(account),
            walletType,
        });
    }
    newWalletLabel(walletId, option, label, iban) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'newwalletlabels',
                walletId,
                option,
                label,
                iban,
            });
        });
    }
    addresses(walletID) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'getapiwalletlabels',
                walletID,
            });
        });
    }
    me() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'gud',
            });
        });
    }
    messages(fromWalletId, toWalletId) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'msgget',
                from: fromWalletId,
                to: toWalletId,
            });
        });
    }
    sendMessage(fromWalletId, toWalletId, subject, body) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'msgsend',
                from: fromWalletId,
                to: toWalletId,
                subject,
                body,
            });
        });
    }
    encrypt(aliceWalletId, bobWalletId, message) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'msgsend',
                aliceWalletId,
                bobWalletId,
                message,
            });
        });
    }
    decrypt(aliceWalletId, bobWalletId, message) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'msgsend',
                aliceWalletId,
                bobWalletId,
                encryptedMessage: message,
            });
        });
    }
    getTx(walletId, txHash) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'repgettx',
                walletId,
                txHash,
            });
        });
    }
    getTxs() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest({
                RequestName: 'repgettxs',
            });
        });
    }
}
exports.MemberNodeClient = MemberNodeClient;
