import type { Page } from '@playwright/test';

/**
 * Navigate within the Vue SPA without a full page reload.
 * Uses history.pushState + popstate to trigger Vue Router navigation,
 * preserving wallet connection state and reactive stores.
 *
 * For initial page loads, use page.goto() instead.
 */
export async function navigateSPA(page: Page, path: string): Promise<void> {
  await page.evaluate((p) => {
    window.history.pushState({}, '', p);
    window.dispatchEvent(new PopStateEvent('popstate'));
  }, path);
  // Vue Router processes the popstate event synchronously within evaluate(),
  // so the route transition has already started by the time we return here.
  // Callers must assert on a route-specific element (e.g. a heading) to confirm
  // the view has mounted — that assertion is the readiness signal.
  // waitForLoadState('networkidle') is not used because persistent WebSocket
  // connections (blockchain event subscriptions) prevent the network from ever
  // going idle, causing spurious timeouts.
}
