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());
    });
};
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import base64 from 'base64-js';
import { EStreamEventTypes } from './types';
import { err, ok } from './result';
class GrpcAction {
    constructor(lndModule) {
        this.lnd = lndModule;
        this.lndEvent =
            Platform.OS === 'ios'
                ? new NativeEventEmitter(NativeModules.LightningEventEmitter)
                : new NativeEventEmitter(NativeModules.ReactNativeLightning);
    }
    checkGrpcReady() {
        return __awaiter(this, void 0, void 0, function* () {
            const res = yield this.lnd.currentState();
            if (!res) {
                throw new Error('Unable to determine LND state');
            }
            if (!res.lndRunning) {
                throw new Error('LND not started');
            }
            if (!res.grpcReady) {
                throw new Error('GRPC not ready');
            }
        });
    }
    sendCommand(method, buffer) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.checkGrpcReady();
            const serialisedReq = base64.fromByteArray(buffer);
            const { data: serializedResponse } = yield this.lnd.sendCommand(method, serialisedReq);
            if (serializedResponse === undefined) {
                throw new Error('Missing response');
            }
            return base64.toByteArray(serializedResponse);
        });
    }
    generateStreamId() {
        GrpcAction.streamCounter = GrpcAction.streamCounter + 1;
        return String(GrpcAction.streamCounter);
    }
    sendStreamCommand(method, buffer, onUpdate, onDone) {
        try {
            this.checkGrpcReady()
                .then(() => {
                const serialisedReq = base64.fromByteArray(buffer);
                const streamId = this.generateStreamId();
                this.lnd.sendStreamCommand(method, streamId, serialisedReq);
                this.lndEvent.addListener(EStreamEventTypes.StreamEvent, (res) => {
                    if (res && res.streamId === streamId) {
                        if (res.error === 'EOF') {
                            onDone(ok(true));
                            return;
                        }
                        else if (res.error) {
                            onUpdate(err(new Error(res.error)));
                        }
                        if (res.data) {
                            onUpdate(ok(base64.toByteArray(res.data)));
                        }
                    }
                });
            })
                .catch((e) => {
                onDone(err(e));
            });
        }
        catch (e) {
            onDone(err(e));
        }
    }
}
GrpcAction.streamCounter = 0;
export default GrpcAction;
//# sourceMappingURL=grpc.js.map