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

export const allowAllCookies = async (page: Page) => {
    const popupLocator = page.locator("//button[contains(text(), 'Allow all')]");
    await page.addLocatorHandler(popupLocator, async () => {
        await popupLocator.click();
    })
}

export const closeNewsletterPopup = async (page: Page) => {
    await page.addLocatorHandler(
        page.locator('//div[@class="Avada-ContinueShoppingText"]'),
        async (locator) => {
            await locator.click();
        }
    );
}

export const closeWheel = async (page: Page) => {
    const closeButton = page.locator("//*[contains(@class, 'om-popup-close-x')]").first();
    await page.addLocatorHandler(closeButton, async () => {
        console.log("wating for om popup show");
        await page.waitForTimeout(2_000);
        const closeButtons = await page.locator("//*[contains(@class, 'om-popup-close-x')]").all();
        for (const button of closeButtons) {
            await button.click();
        }
    });

    // try {
    //     const popupLocator = page.locator("//div[contains(@class, 'om-wheel-position')]").first();

    //     // Check if popup is visible - with 3s timeout
    //     const isVisible = await popupLocator.isVisible({ timeout: 3000 }).catch(() => false);

    //     if (isVisible) {
    //         console.log('🎡 Found wheel popup - closing...');
    //         await page.waitForTimeout(500); // SHORT wait instead of 1500ms

    //         const closeMessages = ["Non, merci !", "No, thanks!"];
    //         const closeButtonXPath = closeMessages.map((message) => `contains(text(), '${message}')`);
    //         const closeMsg = await page.locator(`//span[${closeButtonXPath.join(' or ')}]`).all();

    //         for (const button of closeMsg) {
    //             if (await button.isVisible({ timeout: 2000 }).catch(() => false)) {
    //                 await button.click({ timeout: 3000 });
    //                 console.log('✅ Wheel popup closed');
    //                 return;
    //             }
    //         }

    //         const closeButtons = await page.locator("//div[contains(@class, 'om-popup-close')]").all();
    //         for (const button of closeButtons) {
    //             if (await button.isVisible({ timeout: 2000 }).catch(() => false)) {
    //                 await button.click({ timeout: 3000 });
    //                 console.log('✅ Wheel popup closed');
    //                 return;
    //             }
    //         }
    //     } else {
    //         console.log('ℹ️ Wheel popup not found - skipping');
    //     }
    // } catch (error) {
    //     console.warn('⚠️ Error closing wheel popup (OK to skip):', error);
    //     // Silent fail - don't crash test
    // }

}

export const handlePopupDiscount = async (page: Page) => {
    const popupLocator = page.locator("//div[@id='pge_c9Q0tcaMTu']/div[@class='om-global-close-button om-popup-close om-popup-close-v2']").first();
    await page.addLocatorHandler(popupLocator, async () => {
        await page.waitForTimeout(1500); // wait for popup stable
        await popupLocator.click();
    });
}

export const authorizedShoppayPopup = async (page: Page) => {
    try {
        const popupLocator = page.locator("//section[@data-testid='authorize-modal']");

        // Check if popup exists - with 5s timeout
        const isVisible = await popupLocator.isVisible({ timeout: 5000 }).catch(() => false);

        if (isVisible) {
            console.log('🔐 Found Shoppay authorization popup - closing...');
            const closeButton = page.locator("//button[@data-testid='authorize-modal-close-button']");
            await closeButton.click({ timeout: 5000 });
            console.log('✅ Shoppay popup closed');
        } else {
            console.log('ℹ️ Shoppay popup not found - skipping');
        }
    } catch (error) {
        console.warn('⚠️ Error closing Shoppay popup (OK to skip):', error);
        // Silent fail - don't crash test
    }
}