"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default")); var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.tsx var src_exports = {}; __export(src_exports, { WalletProvider: () => WalletProvider, useWallet: () => useWallet }); module.exports = __toCommonJS(src_exports); var import_react_store = require("@tanstack/react-store"); var import_algosdk = __toESM(require("algosdk"), 1); var React = __toESM(require("react"), 1); __reExport(src_exports, require("avm-wallet"), module.exports); var WalletContext = React.createContext(void 0); var useWallet = () => { const context = React.useContext(WalletContext); if (!context) { throw new Error("useWallet must be used within the WalletProvider"); } const { manager, avmAlgodClient, setAVMAlgodClient } = context; const activeNetwork = (0, import_react_store.useStore)(manager.store, (state) => state.activeNetwork); const setActiveNetwork = async (networkId) => { if (networkId === activeNetwork) { return; } console.info(`[React] Creating Algodv2 client for ${networkId}...`); const { token = "", baseServer, port = "", headers = {} } = manager.networkConfig[networkId]; const newClient = new import_algosdk.default.Algodv2(token, baseServer, port, headers); setAVMAlgodClient(newClient); manager.store.setState((state) => ({ ...state, activeNetwork: networkId })); console.info(`[React] \u2705 Active network set to ${networkId}.`); }; const walletAVMStateMap = (0, import_react_store.useStore)(manager.store, (state) => state.wallets); const avmActiveWalletId = (0, import_react_store.useStore)(manager.store, (state) => state.avmActiveWallet); const wallets = React.useMemo(() => { return [...manager.wallets.values()].map((wallet) => { const walletAVMState = walletAVMStateMap[wallet.id]; return { id: wallet.id, metadata: wallet.metadata, accounts: walletAVMState?.accounts ?? [], activeAccount: walletAVMState?.activeAccount ?? null, isConnected: !!walletAVMState, isActive: wallet.id === avmActiveWalletId, connect: (args) => wallet.connect(args), disconnect: () => wallet.disconnect(), setActive: () => wallet.setActive(), setActiveAccount: (addr) => wallet.setActiveAccount(addr) }; }); }, [manager, walletAVMStateMap, avmActiveWalletId]); const avmActiveWallet = avmActiveWalletId ? manager.getWallet(avmActiveWalletId) || null : null; const avmActiveWalletAVMState = avmActiveWalletId ? walletAVMStateMap[avmActiveWalletId] || null : null; const avmActiveWalletAccounts = avmActiveWalletAVMState?.accounts ?? null; const avmActiveWalletAddresses = avmActiveWalletAccounts?.map((account) => account.address) ?? null; const activeAccount = avmActiveWalletAVMState?.activeAccount ?? null; const activeAddress = activeAccount?.address ?? null; const signTransactions = (txnGroup, indexesToSign) => { if (!avmActiveWallet) { throw new Error("No active wallet"); } return avmActiveWallet.signTransactions(txnGroup, indexesToSign); }; const transactionSigner = (txnGroup, indexesToSign) => { if (!avmActiveWallet) { throw new Error("No active wallet"); } return avmActiveWallet.transactionSigner(txnGroup, indexesToSign); }; return { wallets, avmAlgodClient, activeNetwork, avmActiveWallet, avmActiveWalletAccounts, avmActiveWalletAddresses, activeAccount, activeAddress, setActiveNetwork, setAVMAlgodClient, signTransactions, transactionSigner }; }; var WalletProvider = ({ manager, children }) => { const [avmAlgodClient, setAVMAlgodClient] = React.useState(manager.avmAlgodClient); React.useEffect(() => { manager.avmAlgodClient = avmAlgodClient; }, [avmAlgodClient, manager]); const resumedRef = React.useRef(false); React.useEffect(() => { const resumeSessions = async () => { try { await manager.resumeSessions(); } catch (error) { console.error("Error resuming sessions:", error); } }; if (!resumedRef.current) { resumeSessions(); resumedRef.current = true; } }, [manager]); return /* @__PURE__ */ React.createElement(WalletContext.Provider, { value: { manager, avmAlgodClient, setAVMAlgodClient } }, children); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WalletProvider, useWallet, ...require("avm-wallet") }); //# sourceMappingURL=index.cjs.map