from __future__ import annotations

from typing import Mapping, NewType

from typing_extensions import NotRequired, Unpack

from haitch._attrs import AttributeValue, GlobalAttrs
from haitch._void_elements._void_element import VoidElement

BaseElement = NewType("BaseElement", VoidElement)
"""A `<base>` element."""


def base(
    *,
    extra_attrs: Mapping[str, AttributeValue] = {},
    **attrs: Unpack[BaseAttrs],
) -> BaseElement:
    """Specifies the base URL to use for all relative URLs in a document.

    Note: there can only be one `<base>` element in a document.

    <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base>
    """
    el = VoidElement("base")(**attrs, **extra_attrs)
    return BaseElement(el)


class BaseAttrs(GlobalAttrs):
    """Attributes for the `<base>` element.

    Warning: A `<base>` element must have an `href` attribute, a target attribute, or both.
    If at least one of these attributes are specified, the `<base>` element must come
    before other elements with attribute values that are URLs, such as a `<link>`'s `href`
    attribute.

    <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base>
    """

    href: NotRequired[str]
    """The base URL to be used throughout the document for relative URLs.

    <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base#href>
    """

    target: NotRequired[str]
    """A keyword or author-defined name of the default browsing context to display.

    The following keywords have special meaning: `_self`, `_blank`, `_parent`, `_top`.

    <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base#target>
    """
