"use strict";
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClientMethodHandler = void 0;
const __1 = require("..");
const bindings_1 = require("../bindings");
/**
 * The MethodHandler which sends the commands to the Rust side.
 */
class ClientMethodHandler {
    /**
     * @param methodHandler The Rust method handler created in `ClientMethodHandler.create()`.
     */
    constructor(methodHandler) {
        this.methodHandler = methodHandler;
    }
    /**
     * @param options The client options.
     */
    static async create(options) {
        try {
            const methodHandler = await (0, bindings_1.createClient)(JSON.stringify(options));
            return new ClientMethodHandler(methodHandler);
        }
        catch (error) {
            throw (0, __1.errorHandle)(error);
        }
    }
    async destroy() {
        try {
            (0, bindings_1.destroyClient)(this.methodHandler);
        }
        catch (error) {
            throw (0, __1.errorHandle)(error);
        }
    }
    /**
     * Call a client method.
     *
     * @param method The client method.
     * @returns A promise that resolves to a JSON string response holding the result of the client method.
     */
    async callMethod(method) {
        return (0, bindings_1.callClientMethod)(this.methodHandler, JSON.stringify(method)).catch((error) => {
            throw (0, __1.errorHandle)(error);
        });
    }
    /**
     * Listen to MQTT events.
     *
     * @param topics The topics to listen to.
     * @param callback The callback to be called when an MQTT event is received.
     */
    async listenMqtt(topics, callback) {
        try {
            (0, bindings_1.listenMqtt)(this.methodHandler, topics, callback);
        }
        catch (error) {
            throw (0, __1.errorHandle)(error);
        }
    }
}
exports.ClientMethodHandler = ClientMethodHandler;
//# sourceMappingURL=client-method-handler.js.map