import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { resolvePayRecipient, invoiceNote, assertInvoiceJurisdiction } from './pay-intent';
import { HOME, PageHead } from '../components/PageHead';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import type { PaymentDeliveryMode } from '@xln/core/api/public/runtime-module';
import { parseXlnInvoice, type ParsedXlnInvoice } from '$lib/utils/xlnInvoice';
import { Bar, DeltaBar, DeltaCaption } from '../components/Bars';
import { Icon } from '../components/Icons';
import { ScanSheet } from '../components/ScanSheet';
import { TokenPicker } from '../components/TokenPicker';
import { PaymentTopUp } from '../components/PaymentTopUp';
import { loadStackJurisdictions } from '../runtime/hosted';
import { nativeInvoiceJurisdictionKeys } from '../native/payment-request';
import { jurisdictionRef } from '../runtime/financial/swap';
import { paymentMode } from '../runtime/financial/payment-funding';
import { accountNetBalance } from '@xln/frontend/lib/utils/ui/accountBalance';
import { useApp } from '../runtime/store';
import { peekXLN } from '../runtime/xln-loader';
import { DELIVERY_OPTIONS, eligibleRoutes, quotePaymentRoutes, routeModeError, submitPayment, type PaymentRouteQuote } from '../runtime/financial/payments';
import { amountInputText, formatAmount, formatMoney, formatLimit, parseAmount, shortId } from '../runtime/format';
import { getTokenMeta } from '../runtime/token-meta';
import { usdOf } from '../runtime/financial/prices';
import { displayEntityName, useWallet, type AccountView } from '../runtime/views';

declare const __XLN_STACK_ORIGIN__: string;

/** Re-ask a retryable route quote (profiles still syncing) with doubling delays up to the cap:
 * each attempt re-syncs gossip profiles from the relay, so an unknown target must not poll it at 1 Hz. */
const QUOTE_RETRY_MS = 1_000;
const QUOTE_RETRY_MAX_MS = 30_000;

const looksLikeInvoice = (text: string): boolean => {
	const value = text.trim();
	return value.startsWith('xln://') || value.startsWith('http://') || value.startsWith('https://') || (value.startsWith('0x') && value.includes('?'));
};

export function Pay() {
	const navigate = useNavigate();
	const location = useLocation();
	const [params] = useSearchParams();
	const entityId = useApp(s => s.activeEntityId);
	const selectedTokenId = useApp(s => s.selectedTokenId);
	const setSelectedTokenId = useApp(s => s.setSelectedTokenId);
	const toast = useApp(s => s.toast);
	const commandReady = useApp(s => s.commandReady);
	const wallet = useWallet(entityId);

	const [toText, setToText] = useState(params.get('to')?.toLowerCase() ?? '');
	const [pickedEntityId, setPickedEntityId] = useState<string | null>(null);
	const [toFocused, setToFocused] = useState(false);
	const [invoice, setInvoice] = useState<ParsedXlnInvoice | null>(null);
	const [invoiceError, setInvoiceError] = useState<string | null>(null);
	const [amountText, setAmountText] = useState(params.get('amount') ?? '');
	const [tokenId, setTokenId] = useState(Number(params.get('token') ?? selectedTokenId) || selectedTokenId);
	const [deliveryMode, setDeliveryMode] = useState<PaymentDeliveryMode>(paymentMode(params.get('mode')));
	const [description, setDescription] = useState(params.get('desc') ?? '');
	const [noteOpen, setNoteOpen] = useState(Boolean(params.get('desc')));
	const [modeOpen, setModeOpen] = useState(false);
	const [routes, setRoutes] = useState<PaymentRouteQuote[] | null>(null);
	const [routeIndex, setRouteIndex] = useState(0);
	const [routeError, setRouteError] = useState<string | null>(null);
	const [quoting, setQuoting] = useState(false);
	const [sending, setSending] = useState(false);
	const [scanning, setScanning] = useState(false);
	const quoteSeq = useRef(0);
	const submissionStarted = useRef(false);

	const meta = getTokenMeta(tokenId);
	const self = wallet.entityId;

	const manualDescription = useRef(description);
	const invoiceSeq = useRef(0);
	const invoiceIdentity = jurisdictionRef(wallet.frame);
	const invoiceJurisdictions = useCallback(async (): Promise<string[]> => [
		wallet.jurisdiction, invoiceIdentity,
		...nativeInvoiceJurisdictionKeys(invoiceIdentity, await loadStackJurisdictions(__XLN_STACK_ORIGIN__)),
	], [wallet.jurisdiction, invoiceIdentity]);
	const clearInvoice = useCallback(() => {
		++invoiceSeq.current;
		setInvoice(null);
		setDescription(manualDescription.current);
		setNoteOpen(Boolean(manualDescription.current));
		setInvoiceError(null);
	}, []);

	useEffect(() => { ++invoiceSeq.current; }, [wallet.entityId, wallet.jurisdiction, invoiceIdentity]);

	const applyInvoice = useCallback(async (raw: string): Promise<boolean> => {
		const seq = ++invoiceSeq.current;
		try {
			const parsed = parseXlnInvoice(raw);
			const accepted = parsed.jurisdictionId ? await invoiceJurisdictions() : [];
			if (seq !== invoiceSeq.current) return false;
			assertInvoiceJurisdiction(parsed, accepted);
			const note = invoiceNote(parsed);
			setInvoice(parsed);
			setInvoiceError(null);
			setToText(parsed.targetEntityId);
			setPickedEntityId(parsed.targetEntityId);
			if (parsed.tokenId) setTokenId(parsed.tokenId);
			if (parsed.amount) setAmountText(parsed.amount);
			setDescription(note.description);
			setNoteOpen(Boolean(note.description) || note.locked);
			return true;
		} catch (error) {
			if (seq !== invoiceSeq.current) return false;
			clearInvoice();
			setPickedEntityId(null);
			setToText(raw);
			setInvoiceError(error instanceof Error ? error.message : String(error));
			return false;
		}
	}, [clearInvoice, invoiceJurisdictions]);

	// Deep link: /#pay/<invoice> lands here with the full href in navigation state.
	useEffect(() => {
		const state = location.state as { invoice?: string } | null;
		if (state?.invoice) void applyInvoice(state.invoice);
	}, [location.state, applyInvoice]);

	const known = useMemo(
		() =>
			wallet.summaries
				// Hubs on another chain carry the same labels; a direct payment stays inside our own jurisdiction.
				.filter(summary => !summary.jurisdiction?.name || !wallet.jurisdiction || summary.jurisdiction.name === wallet.jurisdiction)
				.map(summary => ({
					entityId: summary.entityId.toLowerCase(),
					label: summary.label || '',
					isHub: summary.isHub === true,
				}))
				.filter(entry => entry.entityId && entry.entityId !== self),
		[wallet.summaries, wallet.jurisdiction, self],
	);
	const recents = useMemo(() => wallet.accounts.filter(account => !account.disputed).slice(0, 4), [wallet.accounts]);

	const query = toText.trim().toLowerCase();
	const { target, ambiguous } = resolvePayRecipient(toText, pickedEntityId, known);
	const descriptionLocked = invoice !== null && invoiceNote(invoice).locked;
	const validTarget = Boolean(target);
	const suggestions = useMemo(() => {
		if (validTarget) return [];
		if (!query) return known;
		return known.filter(entry => entry.label.toLowerCase().includes(query) || entry.entityId.includes(query));
	}, [known, query, validTarget]);

	const parsedAmount = useMemo(() => {
		try {
			const value = parseAmount(amountText || '0', meta.decimals);
			return value > 0n ? value : null;
		} catch {
			return null;
		}
	}, [amountText, meta.decimals]);

	// PaymentPanel semantics: the largest single-account capacity, since one route uses one first hop.
	const payMax = wallet.accounts
		.filter(account => !account.disputed)
		.flatMap(account => account.tokens)
		.filter(token => token.tokenId === tokenId)
		.reduce((max, token) => (token.derived.outCapacity > max ? token.derived.outCapacity : max), 0n);
	const capacityVersion = wallet.accounts
		.map(account => `${account.counterpartyId}:${account.disputed}:${account.tokens.find(token => token.tokenId === tokenId)?.derived.outCapacity ?? 0n}`)
		.join('|');

	useEffect(() => {
		// Invalidate an in-flight quote even when the new draft cannot be quoted.
		const seq = ++quoteSeq.current;
		setRoutes(null);
		setRouteError(null);
		setRouteIndex(0);
		setQuoting(false);
		if (!self || !validTarget || !parsedAmount) return;
		setQuoting(true);
		let timer: ReturnType<typeof setTimeout>;
		let retryDelayMs = QUOTE_RETRY_MS;
		const attempt = (): void => {
			quotePaymentRoutes({ sourceEntityId: self, targetEntityId: target, tokenId, amount: parsedAmount })
				.then(found => {
					if (seq !== quoteSeq.current) return;
					setRouteError(null);
					setRoutes(found);
					setQuoting(false);
				})
				.catch((error: unknown) => {
					if (seq !== quoteSeq.current) return;
					// The graph search names raw entity ids; the person needs the reason, not the ids.
					const notFound = (error as { code?: unknown }).code === 'E_NOT_FOUND';
					setRouteError(
						notFound
							? 'No route can carry this amount right now, fees included. Try a smaller amount or top up.'
							: error instanceof Error ? error.message : String(error),
					);
					// A retryable read (route profiles still syncing after unlock) is
					// asked again for the same draft; anything else is final.
					const retry = error as { retryable?: unknown; retryAfterMs?: unknown };
					if (retry.retryable === true) {
						const hintMs = typeof retry.retryAfterMs === 'number' && retry.retryAfterMs > 0 ? retry.retryAfterMs : 0;
						timer = setTimeout(attempt, Math.max(retryDelayMs, hintMs));
						retryDelayMs = Math.min(retryDelayMs * 2, QUOTE_RETRY_MAX_MS);
						return;
					}
					setQuoting(false);
				});
		};
		timer = setTimeout(attempt, 250);
		return () => {
			clearTimeout(timer);
			quoteSeq.current += 1;
		};
	}, [self, target, validTarget, parsedAmount, tokenId, capacityVersion]);

	const usable = useMemo(() => (routes ? eligibleRoutes(routes, deliveryMode) : []), [routes, deliveryMode]);
	const chosen = usable[Math.min(routeIndex, Math.max(0, usable.length - 1))] ?? null;
	const modeError = routeModeError(chosen, deliveryMode);
	const firstHop = chosen?.path[1] ?? null;
	const hopAccount = firstHop ? (wallet.accounts.find(account => account.counterpartyId === firstHop) ?? null) : null;
	const hopToken = hopAccount?.tokens.find(token => token.tokenId === tokenId) ?? null;
	const enoughCapacity = Boolean(chosen && hopAccount && !hopAccount.disputed && hopToken && hopToken.derived.outCapacity >= chosen.senderAmount);

	const send = async (): Promise<void> => {
		if (submissionStarted.current || !self || !wallet.signerId || !validTarget || quoting || !chosen || !parsedAmount || modeError || !enoughCapacity) return;
		// React's disabled state is not a synchronous admission guard. Hold this
		// form's intent through successful navigation so a double click pays once.
		submissionStarted.current = true;
		setSending(true);
		try {
			// The active wallet may have changed since this invoice was applied.
			if (invoice) {
				const seq = invoiceSeq.current;
				assertInvoiceJurisdiction(invoice, invoice.jurisdictionId ? await invoiceJurisdictions() : []);
				if (seq !== invoiceSeq.current) throw new Error('Payment form changed; review the invoice again.');
			}
			await submitPayment({
				entityId: self,
				signerId: wallet.signerId,
				targetEntityId: target,
				tokenId,
				deliveryMode,
				description,
				route: chosen,
			});
			toast(`Payment submitted: ${formatMoney(chosen.recipientAmount, meta.decimals)} ${meta.symbol} to ${displayEntityName(wallet.names, target)}`);
			navigate('/');
		} catch (error) {
			toast(error instanceof Error ? error.message : String(error), 'danger');
			submissionStarted.current = false;
			setSending(false);
		}
	};

	const nameOf = (id: string): string => (id === self ? 'You' : displayEntityName(wallet.names, id));

	return (
		<div className="screen fade-in">
			<PageHead
				title="Pay"
				parent={HOME}
				actions={
					<button type="button" className="icon-btn" onClick={() => setScanning(true)} aria-label="Scan invoice" data-testid="pay-scan">
						<Icon name="scan" size={18} />
					</button>
				}
			/>

			<div className="two-col pay">
				<div className="stack">
					<div className="field">
						<div className="field-head">
							<span>To</span>
							{validTarget ? (
								<span className="st-settled" style={{ display: 'inline-flex', gap: 4, alignItems: 'center' }}>
									<Icon name="check" size={12} /> {displayEntityName(wallet.names, target)}
								</span>
							) : invoice ? (
								<span className="st-pending">invoice</span>
							) : null}
						</div>
						<div className="picker">
							<input
								className="input"
								style={{
									fontFamily: query.startsWith('0x') || looksLikeInvoice(query) ? 'var(--font-mono)' : 'var(--font-ui)',
									fontSize: 15,
								}}
								placeholder="Name, entity id or invoice"
								value={toText}
								spellCheck={false}
								data-testid="pay-to"
								onChange={event => {
									const value = event.target.value;
									setToText(value);
									setPickedEntityId(null);
									clearInvoice();
									setInvoiceError(null);
									if (looksLikeInvoice(value)) void applyInvoice(value);
								}}
								onFocus={() => setToFocused(true)}
								onBlur={() => setToFocused(false)}
							/>
							{toFocused && suggestions.length > 0 && (
								<div className="picker-menu" data-testid="pay-recipient-options">
									{suggestions.map(entry => (
										<button
											key={entry.entityId}
											type="button"
											className="picker-option"
											data-testid={`pay-suggestion-${entry.label || entry.entityId}`}
											onMouseDown={event => {
												event.preventDefault();
												setToText(entry.label || entry.entityId);
												setPickedEntityId(entry.entityId);
												clearInvoice();
												setToFocused(false);
											}}
										>
											<span className="t">
												{entry.label || 'Entity'}
												{entry.isHub ? <span className="chip hub">hub</span> : null}
											</span>
											<span className="hash">{shortId(entry.entityId, 10, 6)}</span>
										</button>
									))}
								</div>
							)}
						</div>
						{ambiguous ? <span role="alert">Several entities share this name. Pick a recipient by entity ID.</span> : null}
						{validTarget ? <span className="hash">{target}</span> : null}
						{invoiceError ? <span style={{ color: 'var(--dispute)', fontSize: 12.5 }}>{invoiceError}</span> : null}
					</div>

					{recents.length > 0 && !validTarget && (
						<div className="chips">
							{recents.map(account => (
								<button key={account.counterpartyId} type="button" onClick={() => {
									setToText(account.label);
									setPickedEntityId(account.counterpartyId);
									clearInvoice();
								}} data-testid={`pay-recent-${account.label}`}>
									{account.label}
									{account.isHub ? <span className="chip hub">hub</span> : null}
								</button>
							))}
							<button type="button" onClick={() => setScanning(true)}>
								<Icon name="scan" size={13} /> Scan
							</button>
						</div>
					)}

					<div className="field">
						<div className="field-head">
							<span>Amount</span>
							<button type="button" className="btn quiet num" style={{ fontSize: 12 }} disabled={payMax <= 0n} onClick={() => setAmountText(amountInputText(payMax, meta.decimals))}>
								Up to {formatLimit(payMax, meta.decimals)} instantly
							</button>
						</div>
						<div className="field-row">
							<input className="input big" placeholder="0.00" inputMode="decimal" value={amountText} onChange={event => setAmountText(event.target.value)} data-testid="pay-amount" />
							<TokenPicker
								tokenId={tokenId}
								onChange={id => {
									setTokenId(id);
									setSelectedTokenId(id);
								}}
							/>
						</div>
						{parsedAmount ? (
							<div style={{ marginTop: 6 }}>
								<div className="dcap" style={{ margin: '0 0 5px' }}>
									<span>This payment</span>
									<span>{formatMoney(chosen?.senderAmount ?? parsedAmount, meta.decimals)}</span>
								</div>
								<Bar segments={[{ usd: usdOf(tokenId, chosen?.senderAmount ?? parsedAmount), kind: 'this' }]} height={4} />
								{hopAccount && hopToken ? (
									<>
										<div className="dcap" style={{ margin: '10px 0 5px' }}>
											<span>Your account with {hopAccount.label}</span>
											<span>{formatMoney(hopToken.signed, meta.decimals)}</span>
										</div>
										<DeltaBar derived={hopToken.derived} tokenId={tokenId} />
										<DeltaCaption derived={hopToken.derived} format={value => formatLimit(value, meta.decimals)} />
									</>
								) : null}
							</div>
						) : null}
					</div>

					{quoting && (
						<p className="faint" style={{ fontSize: 12 }}>
							Quoting routes…
						</p>
					)}
					{routeError && validTarget && parsedAmount && <p style={{ color: 'var(--dispute)', fontSize: 13 }}>{routeError}</p>}
					{routes && routes.length > 0 && usable.length === 0 && <p style={{ color: 'var(--dispute)', fontSize: 13 }}>No route matches this delivery mode.</p>}

					{chosen && (
						<div className="card tight" data-testid="pay-quote" data-route-path={chosen.path.join(',')} data-sender-amount={chosen.senderAmount.toString()} data-recipient-amount={chosen.recipientAmount.toString()} data-fee-amount={chosen.totalFee.toString()}>
							<div className="kv mobile-only">
								<span className="k">Route</span>
								<span className="hops">
									{chosen.path.map((hop, index) => (
										<span key={`${hop}-${index}`} style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
											{index > 0 ? <Icon name="arrow" size={12} /> : null}
											<span className={`hop${hop === self ? ' me' : ''}`}>{nameOf(hop)}</span>
										</span>
									))}
								</span>
							</div>
							<div className="kv">
								<span className="k">Fee</span>
								<span className={`v ${chosen.totalFee === 0n ? 'st-settled' : 'num'}`} title={`${amountInputText(chosen.totalFee, meta.decimals)} ${meta.symbol}`}>
									{chosen.totalFee === 0n
										? 'Free'
										: chosen.totalFee * 100n < 10n ** BigInt(meta.decimals)
											? `under 0.01 ${meta.symbol}`
											: `${formatMoney(chosen.totalFee, meta.decimals)} ${meta.symbol}`}
								</span>
							</div>
							<div className="kv">
								<span className="k">Delivery</span>
								<span className="v">
									{DELIVERY_OPTIONS.find(option => option.value === deliveryMode)?.label}
									<button type="button" className="btn quiet" style={{ marginLeft: 8 }} onClick={() => setModeOpen(value => !value)}>
										Change
									</button>
								</span>
							</div>
							{modeOpen && (
								<div className="mode-grid fade-in" role="radiogroup" aria-label="Delivery" style={{ padding: '6px 0 10px' }}>
									{DELIVERY_OPTIONS.map(option => (
										<button
											key={option.value}
											type="button"
											role="radio"
											aria-checked={deliveryMode === option.value}
											className={`mode-card${deliveryMode === option.value ? ' active' : ''}`}
											onClick={() => {
												setDeliveryMode(option.value);
												setRouteIndex(0);
											}}
										>
											<span>
												{option.label}
												{option.recommended ? (
													<span className="chip" style={{ marginLeft: 6 }}>
														Default
													</span>
												) : null}
											</span>
											<span className="d">{option.description}</span>
										</button>
									))}
								</div>
							)}
							{usable.length > 1 && (
								<details className="stack" style={{ gap: 6, padding: '8px 0 10px' }}>
									<summary className="caps" style={{ cursor: 'pointer' }}>
										{usable.length - 1} other {usable.length - 1 === 1 ? 'route' : 'routes'}
									</summary>
									{usable.map((route, index) => (
										<button key={route.path.join('>')} data-route-path={route.path.join(',')} type="button" className={`route-card${route === chosen ? ' active' : ''}`} onClick={() => setRouteIndex(index)}>
											<span className="hops" style={{ justifyContent: 'flex-start' }}>
												{route.path.map((hop, hopIndex) => (
													<span key={`${hop}-${hopIndex}`} style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
														{hopIndex > 0 ? <Icon name="arrow" size={12} /> : null}
														<span className={`hop${hop === self ? ' me' : ''}`}>{nameOf(hop)}</span>
													</span>
												))}
											</span>
											<span className="meta">
												<span>Fee {formatAmount(route.totalFee, meta.decimals, meta.decimals)}</span>
												<span>You send {formatMoney(route.senderAmount, meta.decimals)}</span>
											</span>
										</button>
									))}
								</details>
							)}
						</div>
					)}
					{modeError && <p style={{ color: 'var(--dispute)', fontSize: 12.5 }}>{modeError}</p>}
					{validTarget && parsedAmount && !quoting && !enoughCapacity ? (
						<PaymentTopUp
							key={`${self}:${target}:${tokenId}`}
							wallet={wallet}
							draft={{
								entityId: self,
								to: target,
								amount: amountInputText(parsedAmount, meta.decimals),
								tokenId,
								description,
								deliveryMode,
							}}
							requiredAmount={chosen?.senderAmount ?? parsedAmount}
							routeAccount={hopAccount}
						/>
					) : null}

					<button type="button" className="btn quiet" style={{ alignSelf: 'flex-start' }} onClick={() => setNoteOpen(value => !value)}>
						{noteOpen ? 'Note' : 'Add a note'} <Icon name={noteOpen ? 'chevronDown' : 'chevronRight'} size={13} />
					</button>
					{noteOpen && (
						<div className="field fade-in">
							<input className="input" value={description} disabled={descriptionLocked} onChange={event => {
								manualDescription.current = event.target.value;
								setDescription(event.target.value);
							}} placeholder="What is this for?" data-testid="pay-note" />
						</div>
					)}

					<button
						type="button"
						className="btn primary"
						data-testid="pay-submit"
						disabled={!commandReady || !validTarget || !chosen || !parsedAmount || sending || quoting || !enoughCapacity || Boolean(modeError) || !wallet.signerId}
						onClick={() => void send()}
					>
						<Icon name="pay" size={15} />
						{sending ? 'Sending…' : chosen && parsedAmount ? `Pay ${formatMoney(chosen.senderAmount, meta.decimals)} ${meta.symbol}` : 'Pay'}
					</button>
				</div>

				<div className="aside desktop-only">
					{chosen ? (
						<div className="card">
							<h3 className="caps">Route</h3>
							<div className="tl">
								{chosen.path.map((hop, index) => {
									// pathfinding puts the forwarding fee on the edge leaving the node that charges it.
									const fee = chosen.hops[index]?.fee ?? 0n;
									return (
										<div key={`${hop}-${index}`} className="ev done">
											<div className="t">
												{nameOf(hop)}
												{wallet.hubs.has(hop) ? <span className="chip hub">hub</span> : null}
											</div>
											<div className="s">
												{index === 0
													? wallet.jurisdiction || 'sender'
													: index === chosen.path.length - 1
														? shortId(hop, 10, 6)
														: `forwards atomically · fee ${formatAmount(fee, meta.decimals, meta.decimals)}${chosen.totalFee !== fee ? ` of ${formatAmount(chosen.totalFee, meta.decimals, meta.decimals)} total` : ''}`}
											</div>
										</div>
									);
								})}
							</div>
						</div>
					) : null}
					{hopAccount && hopToken && chosen ? <BeforeAfter account={hopAccount} tokenId={tokenId} senderAmount={chosen.senderAmount} /> : null}
					<div className="card" style={{ display: 'flex', gap: 12, alignItems: 'flex-start' }}>
						<span style={{ color: 'var(--coll)' }}>
							<Icon name="shield" size={20} />
						</span>
						<div className="note">
							<b style={{ color: 'var(--ink)', fontWeight: 600 }}>Provable.</b> A payment is a frame signed by both sides of each account it crosses. Collateral is enforceable on-chain if a
							counterparty ever defaults.
						</div>
					</div>
				</div>
			</div>

			{scanning && (
				<ScanSheet
					onClose={() => setScanning(false)}
					onDecode={value => {
						setScanning(false);
						void applyInvoice(value).then(applied => {
							if (!applied) toast('That QR is not an xln invoice', 'danger');
						});
					}}
				/>
			)}
		</div>
	);
}

/**
 * The account with the first hop before and after this payment. "After" is the
 * canonical deriveDelta over the same delta with Δ moved by the sender debit, so
 * the user sees which components the payment consumes.
 */
function BeforeAfter({ account, tokenId, senderAmount }: { account: AccountView; tokenId: number; senderAmount: bigint }) {
	const xln = peekXLN();
	const token = account.tokens.find(entry => entry.tokenId === tokenId);
	if (!xln || !token) return null;
	const meta = getTokenMeta(tokenId);
	const money = (value: bigint): string => formatMoney(value, meta.decimals);
	const shifted = { ...token.delta, offdelta: token.delta.offdelta + (account.isLeft ? -senderAmount : senderAmount) };
	let after;
	try {
		after = xln.deriveDelta(shifted, account.isLeft);
	} catch {
		return null;
	}
	const signedAfter = accountNetBalance(after);
	const collateralUsed = token.derived.outCollateral - after.outCollateral;
	const creditUsed = token.derived.outOwnCredit - after.outOwnCredit;
	const debtNetted = token.derived.outPeerCredit - after.outPeerCredit;
	const parts = [
		debtNetted > 0n ? `${money(debtNetted)} nets what ${account.label} owes you` : '',
		collateralUsed > 0n ? `${money(collateralUsed)} is covered by collateral` : '',
		creditUsed > 0n ? `${money(creditUsed)} goes on your credit line of ${money(token.derived.ownCreditLimit)}` : '',
	].filter(Boolean);
	return (
		<div className="card">
			<h3 className="caps">Your account with {account.label}</h3>
			<div className="kv" style={{ paddingTop: 0 }}>
				<span className="k">Before</span>
				<span className="v num">
					{money(token.signed)}{' '}
					<span className="muted" style={{ fontWeight: 400 }}>
						{token.signed >= 0n ? 'owes you' : 'you owe'}
					</span>
				</span>
			</div>
			<DeltaBar derived={token.derived} tokenId={tokenId} />
			<DeltaCaption derived={token.derived} format={value => formatLimit(value, meta.decimals)} />
			<div className="kv" style={{ marginTop: 14 }}>
				<span className="k">After</span>
				<span className="v num">
					{money(signedAfter)}{' '}
					<span className="muted" style={{ fontWeight: 400 }}>
						{signedAfter >= 0n ? 'owes you' : 'you owe'}
					</span>
				</span>
			</div>
			<DeltaBar derived={after} tokenId={tokenId} />
			<DeltaCaption derived={after} format={value => formatLimit(value, meta.decimals)} />
			{parts.length > 0 ? (
				<p className="note" style={{ marginTop: 14 }}>
					{parts.join('. ')}. Nothing moves on-chain.
				</p>
			) : null}
		</div>
	);
}
