#
# Copyright © 2006 - 2017 Michal Čihař <michal@cihar.com>
#
# This file is part of Stardicter <https://cihar.com/software/slovnik/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""Special codecs filter to remove accents."""

import unicodedata

SIMPLE_MAPS = {
    "ACUTE ACCENT": "'",
    "NO-BREAK SPACE": " ",
    "THIN SPACE": " ",
    "MULTIPLICATION SIGN": "x",
    "DEGREE SIGN": "<degree>",
    # §
    "SECTION SIGN": "<paragraph>",
    # ÷
    "DIVISION SIGN": "/",
    # „
    "DOUBLE LOW-9 QUOTATION MARK": '"',
    # “
    "LEFT DOUBLE QUOTATION MARK": '"',
    "RIGHT DOUBLE QUOTATION MARK": '"',
    "RIGHT SINGLE QUOTATION MARK": "'",
    "LEFT SINGLE QUOTATION MARK": "'",
    # –
    "EN DASH": "-",
}


def deaccent(exc):
    """Removes accents on string conversion errors."""
    if not isinstance(exc, UnicodeEncodeError):
        raise TypeError(f"don't know how to handle {exc}")
    result = []
    for current in exc.object[exc.start : exc.end]:
        if current in {"\x93", "\x94", "\x84"}:
            result.append('"')
            continue
        if current == "\x92":
            result.append("'")
            continue
        name = unicodedata.name(current)
        if name[:18] == "LATIN SMALL LETTER":
            result.append(name[19].lower())
        elif name[:20] == "LATIN CAPITAL LETTER":
            result.append(name[21])
        elif name in SIMPLE_MAPS:
            result.append(SIMPLE_MAPS[name])
        else:
            raise ValueError(f"Can not convert to ASCII: {current!r} ({name})")
    return ("".join(result), exc.end)
