import { expect, Locator, Page } from "@playwright/test";

const DEFAULT_VIEWPORT = { width: 1920, height: 1080 };

export const verifyScreenshot = async (
    snapshotName: string,
    target: Page | Locator,
    mask?: Locator[],
    tolerance: number = 0,
    viewport: { width: number; height: number } = DEFAULT_VIEWPORT
) => {
    const page = 'page' in target ? target.page() : target;
    await page.setViewportSize(viewport);

    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(1_000);

    await expect(async () => {
        expect(await target.screenshot({ mask })).toMatchSnapshot(snapshotName, {
            maxDiffPixelRatio: tolerance
        });
    }).toPass();
};

export const takeScreenshot = async (snapshotName: string, target: Page | Locator, mask?: Locator[]) => {
    expect(await target.screenshot({
        mask
    })).toMatchSnapshot(snapshotName)
}