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

export async function blockUnnecessaryRequests(page: Page) {
  await page.route('**/*', (route) => {
    const url = route.request().url();
    const resourceType = route.request().resourceType();
    
    // Block analytics and tracking
    if (
      url.includes('google-analytics.com') ||
      url.includes('googletagmanager.com') ||
      url.includes('facebook.com') ||
      url.includes('hotjar.com') ||
      url.includes('mixpanel.com') ||
      url.includes('intercom.io') ||
      url.includes('tawk.to') ||
      url.includes('crisp.chat') ||
      url.includes('drift.com')
    ) {
      return route.abort();
    }

    // Block images except essential ones
    if (resourceType === 'image') {
      if (!url.includes('shopify') && 
          !url.includes('checkout') &&
          !url.includes('stripe') &&
          !url.includes('logo')) {
        return route.abort();
      }
    }

    // Block videos and fonts
    if (resourceType === 'media' || resourceType === 'font') {
      return route.abort();
    }

    route.continue();
  });
}