# -*- coding: utf-8;  -*-
# :-
# SPDX-FileCopyrightText: © 2026 Franz Glasner
# SPDX-License-Identifier: MIT
# :-
r"""Some helper utility functions and data.

"""

__all__ = [
    "REVERSED_STANDARD_TYPES",
    "string_to_defined_tokentype",
]


import pygments.token


REVERSED_STANDARD_TYPES = {}

# Side-effect on import
for _toktype, _cssstyle in pygments.token.STANDARD_TYPES.items():
    REVERSED_STANDARD_TYPES[_cssstyle] = _toktype
del _toktype, _cssstyle


def string_to_defined_tokentype(s):
    """Determine whether the token type `s` given as string is defined.

    :param str s: A token type string as in
                 :py:func:`pygments.token.string_to_tokentype`.
    :returns: An **existing** token if
              :py:func:`pygments.token.string_to_tokentype`
              would return an already existing token type,
              :py:obj:`None` otherwise.
    :rtype: :py:class:`pygments.token._TokenType` or :py:obj:`None`

    This implementation is needed because
    :py:func:`pygments.token.string_to_tokentype` synthesizes a new token
    on not yet existing token types.
    And :py:func:`is_token_subtype` works only on token instances.

    """
    ttype = pygments.token.Token
    ttype_prefix = "Token."
    if not s:
        return ttype
    for part in s.split("."):
        for subtype in ttype.subtypes:
            subtypename = str(subtype)
            # Remove prefix
            assert subtypename.startswith(ttype_prefix)
            subtypename = subtypename[len(ttype_prefix):]
            if subtypename == part:
                ttype = subtype
                ttype_prefix += "%s." % (part,)
                break
        else:
            return None
    return ttype
