import { getWalletState, requireWalletClient } from "./walletconnect-service.mjs";
//#region extensions/crypto/src/services/veil-service.ts
/**
* Veil.cash Service — privacy pool integration for private transfers on Base.
*
* Uses @veil-cash/sdk for ZK proof generation and relayer submission.
* Supports ETH and USDC privacy pools.
*
* SDK provides:
*   - Keypair.fromSigner() — derive privacy keypair from Bankr sign API
*   - deposit() — deposit into privacy pool (public → private)
*   - withdraw() — withdraw from privacy pool (private → public)
*   - transfer() — private-to-private transfer
*   - balance() — check shielded balance
*
* Requires: @veil-cash/sdk npm package (optional dep, ~50KB).
*/
const SUPPORTED_ASSETS = {
	ETH: {
		address: "0x0000000000000000000000000000000000000000",
		decimals: 18,
		poolSize: "0.1"
	},
	USDC: {
		address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
		decimals: 6,
		poolSize: "100"
	}
};
var VeilService = class {
	sdk = null;
	keypair = null;
	keypairAddress = null;
	/**
	* Check if the Veil SDK is installed.
	*/
	async isAvailable() {
		try {
			await import("@veil-cash/sdk");
			return true;
		} catch {
			return false;
		}
	}
	/**
	* Get the SDK instance, lazily loaded.
	*/
	async getSdk() {
		if (this.sdk) return this.sdk;
		try {
			this.sdk = await import("@veil-cash/sdk");
			return this.sdk;
		} catch {
			throw new Error("@veil-cash/sdk not installed. Install with: pnpm add @veil-cash/sdk\nThis package provides ZK proof generation for private transactions on Base.");
		}
	}
	/**
	* Derive a privacy keypair from the connected wallet's signer.
	* Invalidates the cached keypair if the wallet address has changed.
	*/
	async getKeypair() {
		const currentAddress = getWalletState().address ?? null;
		if (this.keypair && this.keypairAddress !== currentAddress) {
			this.keypair = null;
			this.keypairAddress = null;
		}
		if (this.keypair) return this.keypair;
		const sdk = await this.getSdk();
		const walletClient = requireWalletClient();
		this.keypair = await sdk.Keypair.fromSigner(walletClient);
		this.keypairAddress = currentAddress;
		return this.keypair;
	}
	getSupportedAssets() {
		return Object.entries(SUPPORTED_ASSETS).map(([symbol, info]) => ({
			symbol,
			address: info.address,
			poolDenomination: info.poolSize
		}));
	}
	resolveAsset(input) {
		const upper = input.toUpperCase();
		const asset = SUPPORTED_ASSETS[upper];
		if (!asset) return null;
		return {
			symbol: upper,
			...asset
		};
	}
	async deposit(asset, amount) {
		const sdk = await this.getSdk();
		const keypair = await this.getKeypair();
		const assetInfo = this.resolveAsset(asset);
		if (!assetInfo) throw new Error(`Unsupported asset: ${asset}. Supported: ETH, USDC`);
		const state = getWalletState();
		if (!state.connected || !state.address) throw new Error("No wallet connected.");
		const walletClient = requireWalletClient();
		const result = await sdk.deposit({
			keypair,
			asset: assetInfo.address,
			amount,
			signer: walletClient,
			chainId: 8453
		});
		return {
			action: "deposit",
			asset: assetInfo.symbol,
			amount,
			txHash: result.txHash ?? null,
			noteHash: result.noteHash ?? null,
			note: result.note ?? "",
			status: result.txHash ? "confirmed" : "pending"
		};
	}
	async withdraw(asset, amount, recipient) {
		const sdk = await this.getSdk();
		const keypair = await this.getKeypair();
		const assetInfo = this.resolveAsset(asset);
		if (!assetInfo) throw new Error(`Unsupported asset: ${asset}. Supported: ETH, USDC`);
		const result = await sdk.withdraw({
			keypair,
			asset: assetInfo.address,
			amount,
			recipient,
			chainId: 8453,
			useRelayer: true
		});
		return {
			action: "withdraw",
			asset: assetInfo.symbol,
			amount,
			recipient,
			txHash: result.txHash ?? null,
			noteHash: result.noteHash ?? null,
			relayerFee: result.relayerFee ?? null,
			status: result.txHash ? "confirmed" : "submitted_to_relayer"
		};
	}
	async transfer(asset, amount, recipientPublicKey) {
		const sdk = await this.getSdk();
		const keypair = await this.getKeypair();
		const assetInfo = this.resolveAsset(asset);
		if (!assetInfo) throw new Error(`Unsupported asset: ${asset}. Supported: ETH, USDC`);
		const result = await sdk.transfer({
			keypair,
			asset: assetInfo.address,
			amount,
			recipientPublicKey,
			chainId: 8453
		});
		return {
			action: "transfer",
			asset: assetInfo.symbol,
			amount,
			txHash: result.txHash ?? null,
			noteHash: result.noteHash ?? null,
			status: result.txHash ? "confirmed" : "pending"
		};
	}
	async getBalance(asset) {
		const sdk = await this.getSdk();
		const keypair = await this.getKeypair();
		const assets = asset ? [this.resolveAsset(asset)].filter(Boolean) : Object.keys(SUPPORTED_ASSETS).map((s) => this.resolveAsset(s)).filter(Boolean);
		const balances = [];
		for (const a of assets) {
			if (!a) continue;
			try {
				const bal = await sdk.getBalance({
					keypair,
					asset: a.address,
					chainId: 8453
				});
				balances.push({
					asset: a.symbol,
					shielded: bal.shielded ?? "0",
					pendingDeposits: bal.pendingDeposits ?? "0",
					pendingWithdrawals: bal.pendingWithdrawals ?? "0"
				});
			} catch {
				balances.push({
					asset: a.symbol,
					shielded: "0",
					pendingDeposits: "0",
					pendingWithdrawals: "0"
				});
			}
		}
		return balances;
	}
};
let _instance = null;
function getVeilService() {
	if (!_instance) _instance = new VeilService();
	return _instance;
}
function resetVeilService() {
	_instance = null;
}
//#endregion
export { VeilService, getVeilService, resetVeilService };

//# sourceMappingURL=veil-service.mjs.map