from __future__ import annotations

from playwright.async_api import async_playwright, Browser, BrowserContext


async def build_browser_context(headless: bool, viewport: dict[str, int], locale: str, timezone: str) -> BrowserContext:
    playwright = await async_playwright().start()
    browser: Browser = await playwright.chromium.launch(headless=headless)
    context = await browser.new_context(
        viewport=viewport,
        locale=locale,
        timezone_id=timezone,
    )
    context.set_default_timeout(30000)
    return context


async def close_context(context: BrowserContext) -> None:
    await context.close()
    await context.browser.close()
    await context._playwright.stop()
