"use strict";
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.WalletMethodHandler = void 0;
const bindings_1 = require("../bindings");
const client_1 = require("../client");
const secret_manager_1 = require("../secret_manager");
const __1 = require("..");
// The WalletMethodHandler class interacts with methods with the rust bindings.
class WalletMethodHandler {
    /**
     * @param methodHandler The Rust method handler created in `WalletMethodHandler.create()`.
     */
    constructor(methodHandler) {
        this.methodHandler = methodHandler;
    }
    /**
     * @param options The wallet options.
     */
    static async create(options) {
        try {
            const methodHandler = await (0, bindings_1.createWallet)(JSON.stringify(options));
            return new WalletMethodHandler(methodHandler);
        }
        catch (error) {
            throw (0, __1.errorHandle)(error);
        }
    }
    /**
     * Call a wallet method on the Rust backend.
     *
     * @param method The wallet method to call.
     * @returns A promise that resolves to a JSON string response holding the result of the wallet method.
     */
    async callMethod(method) {
        return (0, bindings_1.callWalletMethod)(this.methodHandler, 
        // mapToObject is required to convert maps to array since they otherwise get serialized as `[{}]` even if not empty
        JSON.stringify(method, function mapToObject(_key, value) {
            if (value instanceof Map) {
                return Object.fromEntries(value);
            }
            else {
                return value;
            }
        }), this.methodHandler).catch((error) => {
            throw (0, __1.errorHandle)(error);
        });
    }
    /**
     * Listen to wallet events.
     *
     * @param eventTypes The wallet event types to listen for.
     * @param callback The callback function to call when an event is received.
     */
    async listen(eventTypes, callback) {
        return (0, bindings_1.listenWallet)(this.methodHandler, eventTypes, function (err, data) {
            const parsed = JSON.parse(data);
            callback(
            // Send back raw error instead of parsing
            err, parsed);
        }).catch((error) => {
            throw (0, __1.errorHandle)(error);
        });
    }
    async destroy() {
        try {
            await (0, bindings_1.destroyWallet)(this.methodHandler);
        }
        catch (error) {
            throw (0, __1.errorHandle)(error);
        }
    }
    async getClient() {
        try {
            const result = await (0, bindings_1.getClient)(this.methodHandler);
            return new client_1.Client(new client_1.ClientMethodHandler(result));
        }
        catch (error) {
            throw (0, __1.errorHandle)(error);
        }
    }
    /**
     * Get the secret manager associated with the wallet.
     */
    async getSecretManager() {
        try {
            const result = await (0, bindings_1.getSecretManager)(this.methodHandler);
            return new secret_manager_1.SecretManager(new secret_manager_1.SecretManagerMethodHandler(result));
        }
        catch (error) {
            throw (0, __1.errorHandle)(error);
        }
    }
}
exports.WalletMethodHandler = WalletMethodHandler;
//# sourceMappingURL=wallet-method-handler.js.map