/**
 * Reown AppKit integration for wallet connections
 */
import { createAppKit } from "@reown/appkit";
import { EthersAdapter } from "@reown/appkit-adapter-ethers";
import { mainnet, arbitrum, optimism, base, linea, } from "@reown/appkit/networks";
// AppKit instance
let appKit = null;
let isInitialized = false;
const walletChangeCallbacks = [];
// Supported networks
const networks = [mainnet, arbitrum, optimism, base, linea];
/**
 * Initialize Reown AppKit with the given project ID
 */
export async function initReown(projectId, _options) {
    if (isInitialized && appKit) {
        return;
    }
    if (!projectId) {
        console.warn("No Reown project ID provided, skipping initialization");
        return;
    }
    try {
        // Create Ethers adapter for EVM chains
        const ethersAdapter = new EthersAdapter();
        // Metadata for the app
        const metadata = {
            name: "M0 Swap",
            description: "Cross-chain token swaps",
            url: typeof window !== "undefined"
                ? window.location.origin
                : "https://m0.xyz",
            icons: ["https://m0.xyz/logo.png"],
        };
        // Create AppKit instance
        appKit = createAppKit({
            adapters: [ethersAdapter],
            networks: [mainnet, arbitrum, optimism, base, linea],
            projectId,
            metadata,
            features: {
                analytics: false,
                email: false,
                socials: false,
            },
        });
        // Subscribe to account changes
        appKit.subscribeAccount((account) => {
            const address = account.address || null;
            walletChangeCallbacks.forEach((cb) => cb(address));
        });
        isInitialized = true;
        console.log("Reown AppKit initialized successfully");
    }
    catch (error) {
        console.error("Failed to initialize Reown AppKit:", error);
        isInitialized = false;
    }
}
/**
 * Check if Reown is initialized
 */
export function isReownInitialized() {
    return isInitialized && appKit !== null;
}
/**
 * Open the wallet connection modal
 */
export function openReownModal() {
    if (!appKit) {
        console.warn("Reown not initialized - openReownModal is a no-op");
        return;
    }
    appKit.open();
}
/**
 * Open the network selection modal
 */
export function openReownNetworkModal() {
    if (!appKit) {
        console.warn("Reown not initialized - openReownNetworkModal is a no-op");
        return;
    }
    appKit.open({ view: "Networks" });
}
/**
 * Disconnect the current wallet
 */
export async function disconnectReown() {
    if (!appKit) {
        return;
    }
    await appKit.disconnect();
}
/**
 * Get the currently connected address
 */
export function getReownAddress() {
    if (!appKit) {
        return null;
    }
    return appKit.getAddress() || null;
}
/**
 * Get the current chain ID
 */
export function getReownChainId() {
    if (!appKit) {
        return null;
    }
    const caipNetwork = appKit.getCaipNetwork();
    return caipNetwork ? Number(caipNetwork.id) : null;
}
/**
 * Subscribe to wallet changes
 */
export function onReownWalletChange(callback) {
    walletChangeCallbacks.push(callback);
}
/**
 * Get the EVM provider for transactions
 */
export async function getReownEvmProvider() {
    if (!appKit) {
        return null;
    }
    const provider = appKit.getWalletProvider();
    return provider || null;
}
/**
 * Get the Solana provider for transactions
 * Note: Solana adapter not yet integrated - this will throw an error
 */
export async function getReownSolanaProvider() {
    throw new Error("Solana wallet connections via Reown are not yet supported. " +
        "Please use the legacy Solana wallet adapter or disable SVM chains when using reownProjectId.");
}
/**
 * Switch to a specific EVM chain
 */
export async function switchReownChain(chainId) {
    if (!appKit) {
        throw new Error("Reown not initialized");
    }
    const network = networks.find((n) => n.id === chainId);
    if (!network) {
        throw new Error(`Chain ${chainId} not supported`);
    }
    await appKit.switchNetwork(network);
}
/**
 * Clean up Reown callbacks (but preserve the AppKit singleton)
 * The AppKit instance is preserved because it's expensive to recreate
 * and other component instances may still need it
 */
export function cleanupReown() {
    // Only clear callbacks, don't destroy the singleton
    walletChangeCallbacks.length = 0;
}
//# sourceMappingURL=reown.js.map
