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

export class HomePage {
    page: Page;

    constructor(page: Page) {
        this.page = page;
    }

    xpathDiscountHeaderBarNew = "//div[contains(@class, 'essential_countdown_annoucement_bar_wrapper')]"
    xpathAnnouncementBar = "//div[contains(@class, 'announcement-bar')]";
    
    async goToHomePage(url: string, retries = 2) {
        for (let attempt = 0; attempt <= retries; attempt++) {
            try {
                console.log(`Navigating to ${url} (attempt ${attempt + 1}/${retries + 1})...`);
                await this.page.goto(url, {
                    waitUntil: 'domcontentloaded', // Don't wait for full load - saves 10-20s
                    timeout: 30000 // 30 seconds timeout
                });
                console.log('Page loaded successfully');
                return;
            } catch (error) {
                console.error(`Navigation failed on attempt ${attempt + 1}:`, error);
                
                if (attempt < retries) {
                    const delayMs = 2000 * (attempt + 1); // 2s, 4s delay
                    console.log(`⏳ Retrying in ${delayMs}ms...`);
                    await this.page.waitForTimeout(delayMs);
                } else {
                    throw new Error(`Failed to navigate to ${url} after ${retries + 1} retries: ${error}`);
                }
            }
        }
    }
}