"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GrpcWebImpl = exports.SpyRPCServiceSubscribeSignedVAADesc = exports.SpyRPCServiceDesc = exports.SpyRPCServiceClientImpl = exports.SubscribeSignedVAAResponse = exports.SubscribeSignedVAARequest = exports.FilterEntry = exports.EmitterFilter = exports.protobufPackage = void 0;
/* eslint-disable */
const long_1 = __importDefault(require("long"));
const grpc_web_1 = require("@improbable-eng/grpc-web");
const minimal_1 = __importDefault(require("protobufjs/minimal"));
const publicrpc_1 = require("../../publicrpc/v1/publicrpc");
const rxjs_1 = require("rxjs");
const browser_headers_1 = require("browser-headers");
const operators_1 = require("rxjs/operators");
exports.protobufPackage = "spy.v1";
const baseEmitterFilter = { chainId: 0, emitterAddress: "" };
exports.EmitterFilter = {
    encode(message, writer = minimal_1.default.Writer.create()) {
        if (message.chainId !== 0) {
            writer.uint32(8).int32(message.chainId);
        }
        if (message.emitterAddress !== "") {
            writer.uint32(18).string(message.emitterAddress);
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof minimal_1.default.Reader ? input : new minimal_1.default.Reader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = Object.assign({}, baseEmitterFilter);
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.chainId = reader.int32();
                    break;
                case 2:
                    message.emitterAddress = reader.string();
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromJSON(object) {
        const message = Object.assign({}, baseEmitterFilter);
        if (object.chainId !== undefined && object.chainId !== null) {
            message.chainId = publicrpc_1.chainIDFromJSON(object.chainId);
        }
        else {
            message.chainId = 0;
        }
        if (object.emitterAddress !== undefined && object.emitterAddress !== null) {
            message.emitterAddress = String(object.emitterAddress);
        }
        else {
            message.emitterAddress = "";
        }
        return message;
    },
    toJSON(message) {
        const obj = {};
        message.chainId !== undefined &&
            (obj.chainId = publicrpc_1.chainIDToJSON(message.chainId));
        message.emitterAddress !== undefined &&
            (obj.emitterAddress = message.emitterAddress);
        return obj;
    },
    fromPartial(object) {
        const message = Object.assign({}, baseEmitterFilter);
        if (object.chainId !== undefined && object.chainId !== null) {
            message.chainId = object.chainId;
        }
        else {
            message.chainId = 0;
        }
        if (object.emitterAddress !== undefined && object.emitterAddress !== null) {
            message.emitterAddress = object.emitterAddress;
        }
        else {
            message.emitterAddress = "";
        }
        return message;
    },
};
const baseFilterEntry = {};
exports.FilterEntry = {
    encode(message, writer = minimal_1.default.Writer.create()) {
        if (message.emitterFilter !== undefined) {
            exports.EmitterFilter.encode(message.emitterFilter, writer.uint32(10).fork()).ldelim();
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof minimal_1.default.Reader ? input : new minimal_1.default.Reader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = Object.assign({}, baseFilterEntry);
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.emitterFilter = exports.EmitterFilter.decode(reader, reader.uint32());
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromJSON(object) {
        const message = Object.assign({}, baseFilterEntry);
        if (object.emitterFilter !== undefined && object.emitterFilter !== null) {
            message.emitterFilter = exports.EmitterFilter.fromJSON(object.emitterFilter);
        }
        else {
            message.emitterFilter = undefined;
        }
        return message;
    },
    toJSON(message) {
        const obj = {};
        message.emitterFilter !== undefined &&
            (obj.emitterFilter = message.emitterFilter
                ? exports.EmitterFilter.toJSON(message.emitterFilter)
                : undefined);
        return obj;
    },
    fromPartial(object) {
        const message = Object.assign({}, baseFilterEntry);
        if (object.emitterFilter !== undefined && object.emitterFilter !== null) {
            message.emitterFilter = exports.EmitterFilter.fromPartial(object.emitterFilter);
        }
        else {
            message.emitterFilter = undefined;
        }
        return message;
    },
};
const baseSubscribeSignedVAARequest = {};
exports.SubscribeSignedVAARequest = {
    encode(message, writer = minimal_1.default.Writer.create()) {
        for (const v of message.filters) {
            exports.FilterEntry.encode(v, writer.uint32(10).fork()).ldelim();
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof minimal_1.default.Reader ? input : new minimal_1.default.Reader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = Object.assign({}, baseSubscribeSignedVAARequest);
        message.filters = [];
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.filters.push(exports.FilterEntry.decode(reader, reader.uint32()));
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromJSON(object) {
        const message = Object.assign({}, baseSubscribeSignedVAARequest);
        message.filters = [];
        if (object.filters !== undefined && object.filters !== null) {
            for (const e of object.filters) {
                message.filters.push(exports.FilterEntry.fromJSON(e));
            }
        }
        return message;
    },
    toJSON(message) {
        const obj = {};
        if (message.filters) {
            obj.filters = message.filters.map((e) => e ? exports.FilterEntry.toJSON(e) : undefined);
        }
        else {
            obj.filters = [];
        }
        return obj;
    },
    fromPartial(object) {
        const message = Object.assign({}, baseSubscribeSignedVAARequest);
        message.filters = [];
        if (object.filters !== undefined && object.filters !== null) {
            for (const e of object.filters) {
                message.filters.push(exports.FilterEntry.fromPartial(e));
            }
        }
        return message;
    },
};
const baseSubscribeSignedVAAResponse = {};
exports.SubscribeSignedVAAResponse = {
    encode(message, writer = minimal_1.default.Writer.create()) {
        if (message.vaaBytes.length !== 0) {
            writer.uint32(10).bytes(message.vaaBytes);
        }
        return writer;
    },
    decode(input, length) {
        const reader = input instanceof minimal_1.default.Reader ? input : new minimal_1.default.Reader(input);
        let end = length === undefined ? reader.len : reader.pos + length;
        const message = Object.assign({}, baseSubscribeSignedVAAResponse);
        message.vaaBytes = new Uint8Array();
        while (reader.pos < end) {
            const tag = reader.uint32();
            switch (tag >>> 3) {
                case 1:
                    message.vaaBytes = reader.bytes();
                    break;
                default:
                    reader.skipType(tag & 7);
                    break;
            }
        }
        return message;
    },
    fromJSON(object) {
        const message = Object.assign({}, baseSubscribeSignedVAAResponse);
        message.vaaBytes = new Uint8Array();
        if (object.vaaBytes !== undefined && object.vaaBytes !== null) {
            message.vaaBytes = bytesFromBase64(object.vaaBytes);
        }
        return message;
    },
    toJSON(message) {
        const obj = {};
        message.vaaBytes !== undefined &&
            (obj.vaaBytes = base64FromBytes(message.vaaBytes !== undefined ? message.vaaBytes : new Uint8Array()));
        return obj;
    },
    fromPartial(object) {
        const message = Object.assign({}, baseSubscribeSignedVAAResponse);
        if (object.vaaBytes !== undefined && object.vaaBytes !== null) {
            message.vaaBytes = object.vaaBytes;
        }
        else {
            message.vaaBytes = new Uint8Array();
        }
        return message;
    },
};
class SpyRPCServiceClientImpl {
    constructor(rpc) {
        this.rpc = rpc;
        this.SubscribeSignedVAA = this.SubscribeSignedVAA.bind(this);
    }
    SubscribeSignedVAA(request, metadata) {
        return this.rpc.invoke(exports.SpyRPCServiceSubscribeSignedVAADesc, exports.SubscribeSignedVAARequest.fromPartial(request), metadata);
    }
}
exports.SpyRPCServiceClientImpl = SpyRPCServiceClientImpl;
exports.SpyRPCServiceDesc = {
    serviceName: "spy.v1.SpyRPCService",
};
exports.SpyRPCServiceSubscribeSignedVAADesc = {
    methodName: "SubscribeSignedVAA",
    service: exports.SpyRPCServiceDesc,
    requestStream: false,
    responseStream: true,
    requestType: {
        serializeBinary() {
            return exports.SubscribeSignedVAARequest.encode(this).finish();
        },
    },
    responseType: {
        deserializeBinary(data) {
            return Object.assign(Object.assign({}, exports.SubscribeSignedVAAResponse.decode(data)), { toObject() {
                    return this;
                } });
        },
    },
};
class GrpcWebImpl {
    constructor(host, options) {
        this.host = host;
        this.options = options;
    }
    unary(methodDesc, _request, metadata) {
        var _a;
        const request = Object.assign(Object.assign({}, _request), methodDesc.requestType);
        const maybeCombinedMetadata = metadata && this.options.metadata
            ? new browser_headers_1.BrowserHeaders(Object.assign(Object.assign({}, (_a = this.options) === null || _a === void 0 ? void 0 : _a.metadata.headersMap), metadata === null || metadata === void 0 ? void 0 : metadata.headersMap))
            : metadata || this.options.metadata;
        return new Promise((resolve, reject) => {
            grpc_web_1.grpc.unary(methodDesc, {
                request,
                host: this.host,
                metadata: maybeCombinedMetadata,
                transport: this.options.transport,
                debug: this.options.debug,
                onEnd: function (response) {
                    if (response.status === grpc_web_1.grpc.Code.OK) {
                        resolve(response.message);
                    }
                    else {
                        const err = new Error(response.statusMessage);
                        err.code = response.status;
                        err.metadata = response.trailers;
                        reject(err);
                    }
                },
            });
        });
    }
    invoke(methodDesc, _request, metadata) {
        var _a;
        // Status Response Codes (https://developers.google.com/maps-booking/reference/grpc-api/status_codes)
        const upStreamCodes = [2, 4, 8, 9, 10, 13, 14, 15];
        const DEFAULT_TIMEOUT_TIME = 3000;
        const request = Object.assign(Object.assign({}, _request), methodDesc.requestType);
        const maybeCombinedMetadata = metadata && this.options.metadata
            ? new browser_headers_1.BrowserHeaders(Object.assign(Object.assign({}, (_a = this.options) === null || _a === void 0 ? void 0 : _a.metadata.headersMap), metadata === null || metadata === void 0 ? void 0 : metadata.headersMap))
            : metadata || this.options.metadata;
        return new rxjs_1.Observable((observer) => {
            const upStream = () => {
                const client = grpc_web_1.grpc.invoke(methodDesc, {
                    host: this.host,
                    request,
                    transport: this.options.streamingTransport || this.options.transport,
                    metadata: maybeCombinedMetadata,
                    debug: this.options.debug,
                    onMessage: (next) => observer.next(next),
                    onEnd: (code, message) => {
                        if (code === 0) {
                            observer.complete();
                        }
                        else if (upStreamCodes.includes(code)) {
                            setTimeout(upStream, DEFAULT_TIMEOUT_TIME);
                        }
                        else {
                            observer.error(new Error(`Error ${code} ${message}`));
                        }
                    },
                });
                observer.add(() => client.close());
            };
            upStream();
        }).pipe(operators_1.share());
    }
}
exports.GrpcWebImpl = GrpcWebImpl;
var globalThis = (() => {
    if (typeof globalThis !== "undefined")
        return globalThis;
    if (typeof self !== "undefined")
        return self;
    if (typeof window !== "undefined")
        return window;
    if (typeof global !== "undefined")
        return global;
    throw "Unable to locate global object";
})();
const atob = globalThis.atob ||
    ((b64) => globalThis.Buffer.from(b64, "base64").toString("binary"));
function bytesFromBase64(b64) {
    const bin = atob(b64);
    const arr = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; ++i) {
        arr[i] = bin.charCodeAt(i);
    }
    return arr;
}
const btoa = globalThis.btoa ||
    ((bin) => globalThis.Buffer.from(bin, "binary").toString("base64"));
function base64FromBytes(arr) {
    const bin = [];
    for (const byte of arr) {
        bin.push(String.fromCharCode(byte));
    }
    return btoa(bin.join(""));
}
if (minimal_1.default.util.Long !== long_1.default) {
    minimal_1.default.util.Long = long_1.default;
    minimal_1.default.configure();
}
