"""Core placeholder text helpers."""

import random
import textwrap
from typing import Optional

_WORDS = [
    "lorem",
    "ipsum",
    "dolor",
    "amet",
    "consectetur",
    "adipiscing",
    "elit",
    "integer",
    "vulputate",
    "erat",
    "facilisis",
    "turpis",
    "dapibus",
    "risus",
    "dictum",
    "velit",
    "placerat",
    "suscipit",
    "interdum",
    "tincidunt",
    "hendrerit",
    "fringilla",
    "maximus",
    "auctor",
    "gravida",
    "metus",
    "semper",
    "posuere",
    "urna",
    "scelerisque",
    "aliquet",
    "faucibus",
    "vehicula",
    "porta",
    "quis",
    "nibh",
    "fermentum",
    "pulvinar",
    "tristique",
    "pellentesque",
    "iaculis",
    "luctus",
    "morbi",
    "malesuada",
    "convallis",
    "cras",
    "varius",
    "phasellus",
    "viverra",
    "ornare",
    "donec",
]


def _rng(seed: Optional[int]) -> random.Random:
    return random.Random(seed)


def placeholder_words(count: int = 5, *, seed: Optional[int] = None) -> str:
    """Return a space-delimited string of placeholder words."""
    if count < 1:
        raise ValueError("count must be at least 1")
    rng = _rng(seed)
    return " ".join(rng.choice(_WORDS) for _ in range(count))


def placeholder_sentences(sentences: int = 3, *, seed: Optional[int] = None) -> str:
    """Return multiple placeholder sentences separated by spaces."""
    if sentences < 1:
        raise ValueError("sentences must be at least 1")

    rng = _rng(seed)
    pieces = []
    for _ in range(sentences):
        length = rng.randint(6, 14)
        words = [rng.choice(_WORDS) for _ in range(length)]
        words[0] = words[0].capitalize()
        pieces.append(" ".join(words) + ".")

    return " ".join(pieces)


def placeholder_paragraph(
    sentences: int = 3,
    *,
    width: int = 72,
    seed: Optional[int] = None,
) -> str:
    """Return placeholder sentences wrapped to the desired width."""
    if width < 10:
        raise ValueError("width must be at least 10 characters")

    paragraph = placeholder_sentences(sentences=sentences, seed=seed)
    return textwrap.fill(paragraph, width=width)

