# scraper2_hj3415/app/nfs/adapters/out/playwright/capabilities/interaction.py
from __future__ import annotations

from playwright.async_api import TimeoutError as PwTimeoutError

from ._base import _PlaywrightBase  # pyright: ignore[reportPrivateUsage]


class PlaywrightInteraction(_PlaywrightBase):

    async def click(
        self,
        selector: str,
        *,
        index: int = 0,
        timeout_ms: int = 4_000,
        force: bool = False,
    ) -> None:
        loc = self._page.locator(selector).nth(index)
        await loc.click(timeout=timeout_ms, force=force)

    async def try_click(
        self,
        selector: str,
        *,
        index: int = 0,
        timeout_ms: int = 1_500,
        force: bool = False,
    ) -> bool:
        loc = self._page.locator(selector).nth(index)
        try:
            await loc.click(timeout=timeout_ms, trial=True, force=force)
            return True
        except PwTimeoutError:
            return False

    async def scroll_into_view(self, selector: str, *, index: int = 0) -> None:
        await self._page.locator(selector).nth(index).scroll_into_view_if_needed()
