# Copyright (c) 2006-2026  Andrey Golovizin
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


"""
LaTeX output backend.

>>> from pybtex.richtext import Tag, HRef
>>> latex = Backend()
>>> print(Tag("em", "").render(latex))
<BLANKLINE>
>>> print(Tag("em", "Non-", "empty").render(latex))
\\emph{Non-empty}
>>> print(HRef("/", "").render(latex))
<BLANKLINE>
>>> print(HRef("/", "Non-", "empty").render(latex))
\\href{/}{Non-empty}
>>> print(HRef("http://example.org/", "http://example.org/").render(latex))
\\url{http://example.org/}
>>> print(Tag("sup", "hello").render(latex))
\\textsuperscript{hello}
"""

import codecs
from types import MappingProxyType

import latexcodec  # noqa: F401

from pybtex.backends import BaseBackend


class Backend(BaseBackend):
    default_suffix = ".bbl"
    symbols = MappingProxyType(
        {"ndash": "--", "newblock": "\n\\newblock ", "nbsp": "~"}
    )
    tags = MappingProxyType(
        {
            "em": "emph",
            "strong": None,
            "i": "textit",
            "b": "textbf",
            "tt": "texttt",
            "sup": "textsuperscript",
            "sub": "textsubscript",
        }
    )

    def __init__(self, encoding=None):
        super().__init__(encoding)
        self.latex_encoding = "ulatex+" + self.encoding

    def format_str(self, str_):
        return codecs.encode(str_, self.latex_encoding)

    def format_tag(self, tag_name, text):
        tag = self.tags.get(tag_name)
        if tag is None:
            return f"{{{text}}}" if text else ""
        return rf"\{tag}{{{text}}}" if text else ""

    def format_href(self, url, text, external=False):
        if not text:
            return ""
        opts = "[pdfnewwindow]" if external else ""
        if text == self.format_str(url):
            return f"\\url{{{url}}}"
        return rf"\href{opts}{{{url}}}{{{text}}}"

    def format_protected(self, text):
        """
        >>> from pybtex.richtext import Protected
        >>> print(Protected("CTAN").render_as("latex"))
        {CTAN}
        """

        return f"{{{text}}}"

    def write_prologue(self):
        if self.formatted_bibliography.preamble:
            self.output(self.formatted_bibliography.preamble + "\n")

        longest_label = self.formatted_bibliography.get_longest_label()
        self.output(f"\\begin{{thebibliography}}{{{longest_label}}}")

    def write_epilogue(self):
        self.output("\n\n\\end{thebibliography}\n")

    def write_entry(self, key, label, text):
        self.output(f"\n\n\\bibitem[{label}]{{{key}}}\n")
        self.output(text)
