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

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


class PlaywrightText(_PlaywrightBase):
    async def count(self, selector: str) -> int:
        return await self._page.locator(selector).count()

    async def text_content_first(self, selector: str) -> str:
        return (await self._page.locator(selector).first.text_content()) or ""

    async def all_texts(self, selector: str) -> list[str]:
        loc = self._page.locator(selector)
        return await loc.all_text_contents()

    async def get_text_by_text(self, needle: str) -> str:
        return (await self._page.get_by_text(needle).first.text_content()) or ""

    async def inner_text(self, selector: str) -> str:
        return await self._page.locator(selector).first.inner_text()

    async def outer_html_nth(self, selector: str, index: int) -> str:
        loc = self._page.locator(selector).nth(index)
        return str(
            await loc.evaluate("el => el.outerHTML")  # pyright: ignore[reportAny]
        )
