'use strict';

var stargate = require('@cosmjs/stargate');
var secretjs = require('secretjs');
var index = require('./index-BXuWPj5X.js');
require('@0xsquid/squid-types');
require('viem');
require('react');
require('@tanstack/react-query');
require('@cosmjs/encoding');
require('bitcoinjs-lib');
require('@bitcoinerlab/secp256k1');
require('axios');
require('@walletconnect/modal');
require('@walletconnect/universal-provider');
require('ripple-address-codec');
require('@solana/spl-token');
require('@solana/wallet-standard-wallet-adapter-base');
require('@solana/web3.js');
require('@stellar/stellar-sdk');
require('@mysten/wallet-standard');
require('ethers');
require('bignumber.js');
require('countries-list');
require('currency-symbol-map');
require('fuse.js');
require('zustand');
require('zustand/middleware');
require('wagmi');
require('@safe-global/safe-apps-sdk');
require('@wallet-standard/core');
require('@mysten/slush-wallet');
require('@mysten/sui/client');
require('wagmi/connectors');
require('@cosmjs/proto-signing');
require('@mysten/sui/transactions');
require('@cosmjs/cosmwasm-stargate');
require('@injectivelabs/sdk-ts/dist/cjs/core/stargate/index.js');
require('@0xsquid/sdk');
require('viem/chains');

/**
 * Fetch secret network token balance
 * Using the permit signature, see permit function for more details
 * @param secretJS
 * @param contract
 * @param chainId
 * @param walletAddress
 * @param permit
 * @returns
 */
const getTokenBalance = async (secretJS, contract, permit) => {
    if (permit) {
        const msg = {
            balance: {},
        };
        const result = await secretJS.query.compute.queryContract({
            contract_address: contract.address,
            code_hash: contract.codeHash,
            query: {
                with_permit: {
                    query: msg,
                    permit,
                },
            },
        });
        return result;
    }
    return -1;
};
const getPermit = async (chainId, contracts, address) => {
    const contractsString = contracts.join("_");
    const permKey = `perm_${chainId}_${contractsString}_${address}`;
    let permit;
    const permitStored = window.localStorage.getItem(permKey);
    if (permitStored)
        permit = JSON.parse(permitStored);
    // Not able to fetch permit signature from local storage,
    // Ask user to sign message
    if (!permit) {
        try {
            const result = await window.keplr.signAmino(chainId, address, {
                chain_id: chainId,
                account_number: "0",
                sequence: "0",
                fee: {
                    amount: [{ denom: "uscrt", amount: "0" }],
                    gas: "1",
                },
                msgs: [
                    {
                        type: "query_permit",
                        value: {
                            permit_name: "secret-bridge-balance",
                            allowed_tokens: contracts,
                            permissions: ["balance"],
                        },
                    },
                ],
                memo: "",
            }, {
                preferNoSetFee: true,
                preferNoSetMemo: true,
            });
            permit = {
                params: {
                    permit_name: "secret-bridge-balance",
                    allowed_tokens: contracts,
                    chain_id: chainId,
                    permissions: ["balance"],
                },
                signature: result.signature,
            };
            window.localStorage.setItem(permKey, JSON.stringify(permit));
        }
        catch (err) {
            console.error("--- PERMIT ERROR ---");
            console.error(err);
        }
    }
    return permit;
};
/**
 * Fetches the secret balance of the user
 * This has a different logic than the other balances because Secret network hides the balance of the user by design
 * So we need to fetch the balance in a different way
 */
const fetchAllSecretBalances = async (chainData, userAddress, secretTokens, keplr) => {
    if (!keplr)
        return [];
    // Enables app to utilize keplr's secret utilities
    await keplr.enable(index.CHAIN_IDS.SECRET);
    // Create a client that handles the query encryption
    const client = new secretjs.SecretNetworkClient({
        url: chainData.rest,
        chainId: index.CHAIN_IDS.SECRET,
        wallet: keplr.getOfflineSignerOnlyAmino(index.CHAIN_IDS.SECRET),
        encryptionUtils: keplr.getEnigmaUtils(index.CHAIN_IDS.SECRET),
        walletAddress: userAddress,
    });
    // Get secret tokens
    const permit = await getPermit(index.CHAIN_IDS.SECRET, secretTokens.map((st) => st.address), userAddress);
    // Fetching all balances in parallel
    const privateTokens = await Promise.all(secretTokens
        .filter((t) => !!t.codeHash)
        .map(async (token) => {
        const result = (await getTokenBalance(client, {
            address: token.address,
            codeHash: token.codeHash ?? "",
        }, permit));
        return {
            ...token,
            balance: index.formatBNToReadable(BigInt(result.balance.amount), token?.decimals),
        };
    }));
    // Use Stargate getBalance for SCRT
    const stargateClient = await stargate.StargateClient.connect(chainData.rpc);
    const nativeSecretToken = secretTokens.find((t) => t.address === "uscrt");
    const publicTokenBalance = await stargateClient.getBalance(userAddress, "uscrt");
    const publicTokenWithBalance = {
        ...nativeSecretToken,
        balance: index.formatBNToReadable(BigInt(publicTokenBalance.amount), nativeSecretToken?.decimals ?? 18),
    };
    return [...privateTokens, publicTokenWithBalance];
};

exports.fetchAllSecretBalances = fetchAllSecretBalances;
exports.getPermit = getPermit;
exports.getTokenBalance = getTokenBalance;
//# sourceMappingURL=secretService-BaRVTuK5.js.map
