from xml.etree import cElementTree as eTree

from modelcommon.fiscalwrapper import FiscalizationException, FiscalizationErrorsCode
from mw_helper import format_currency
from typing import Optional


def get_error_fiscal_message(
    model,          # type: Optional[eTree.XML]
    exception,      # type: Exception
):
    # type: (...) -> Optional[str]

    if isinstance(exception, FiscalizationException):
        if exception.error_code == FiscalizationErrorsCode.FiscalMaxValueExceeded.value:
            formatted_value = format_currency(
                model=model,
                value=float(exception.message),
            )
            return "$MAXIMUM_AMOUNT_TO_FISCALIZE|{0}|{0}".format(formatted_value)

    return exception.message
