"""Link tails: the text that follows a link's closing bracket.

Responsibility: match the inline form `(destination "title")` and the full, collapsed and shortcut
reference forms against the source after a `]`. The destination and title grammar and the label
scanner come from `mdparser.refs`, and resolution goes through the reference map. The bracket
procedure that calls these matchers lives in `mdparser.inlines`.
"""

from dataclasses import dataclass

from mdparser.refs import (
    TITLE_CLOSERS,
    ReferenceMap,
    parse_destination,
    parse_title,
    scan_label,
    skip_whitespace_with_line_ending,
)


@dataclass(frozen=True)
class LinkTail:
    """A matched link tail: the decoded destination, the decoded title or None, and the index past it."""

    destination: str
    title: str | None
    end: int


def match_inline_link(raw: str, start: int) -> LinkTail | None:
    """Match `(destination "title")` at `start`, which must hold `(`, or return None.

    The destination is optional and comes first. A title must be separated from the destination by
    whitespace. Whitespace around each part is spaces, tabs and at most one line ending.
    """
    if raw[start : start + 1] != "(":
        return None
    index = skip_whitespace_with_line_ending(raw, start + 1)
    if raw[index : index + 1] == ")":
        return LinkTail("", None, index + 1)
    destination = parse_destination(raw, index)
    if destination is None:
        return None
    destination_text, index = destination

    after_space = skip_whitespace_with_line_ending(raw, index)
    title = None
    if after_space > index and raw[after_space : after_space + 1] in TITLE_CLOSERS:
        parsed_title = parse_title(raw, after_space)
        if parsed_title is None:
            return None
        title, index = parsed_title
        index = skip_whitespace_with_line_ending(raw, index)
    else:
        index = after_space
    if raw[index : index + 1] != ")":
        return None
    return LinkTail(destination_text, title, index + 1)


def match_reference_tail(raw: str, start: int, text: str, references: ReferenceMap) -> LinkTail | None:
    """Match a full, collapsed or shortcut reference at `start`, just past the link text `text`.

    A following label, `[label]` or `[]`, decides the form. The shortcut form applies only when no
    label follows, so a following label that does not resolve fails the whole link and the link text
    is not tried as a shortcut.
    """
    if raw[start : start + 2] == "[]":
        return _resolve(text, references, start + 2)
    label_end = scan_label(raw, start) if raw[start : start + 1] == "[" else None
    if label_end is not None:
        return _resolve(raw[start + 1 : label_end - 1], references, label_end)
    return _resolve(text, references, start)


def _resolve(label: str, references: ReferenceMap, end: int) -> LinkTail | None:
    """The definition for `label`, as a tail ending at `end`, or None when the label is not defined.

    The label must be a valid link label: 1 to 999 characters, not blank, with no unescaped
    brackets. Only then is it looked up, so the text of a link can serve as its own label.
    """
    if scan_label("[" + label + "]", 0) != len(label) + 2:
        return None
    reference = references.get(label)
    if reference is None:
        return None
    return LinkTail(reference.destination, reference.title, end)
