# Path: lib/utils/debug.py
# def Debug(classification, text):
#     if 'all' in print_classifications or classification in print_classifications:
#         print("ArcticTypescript: %s: %s" % (classification.ljust(8), text))
#     if classification not in possible_classifications:
#         print("ArcticTypescript: debug: got unknown debug message classification: %s. " \
#             "Consider adding this to possible_classifications" % classification)
#     sys.stdout.flush()
# 
# def max_calls(limit = 1500, name=""):
#     """Decorator which allows its wrapped function to be called `limit` times"""
#     def decorator(func):
#         # Disable limit:
#         return func
#         @wraps(func)
#         def wrapper(*args, **kwargs):
#             calls = getattr(wrapper, 'calls', 0)
#             calls += 1
#             setattr(wrapper, 'calls', calls)
#             fname = name if name != "" else func.__name__
# 
#             if calls == limit + 1:
#                 Debug('max_calls', "LIMIT !! ## !!: Fkt %s has %i calls, stop" % (fname, calls - 1))
# 
#             if calls >= limit + 1:
#                 return None
# 
#             Debug('max_calls', "CALL: Fkt %s has %i calls -> +1" % (fname, calls - 1))
# 
#             return func(*args, **kwargs)
#         setattr(wrapper, 'calls', 0)
#         return wrapper
#     return decorator
# 
# Path: lib/utils/fileutils.py
# def fn2k(filename):
#     """ shortcut for filename2key """
#     return filename2key(filename)


import sublime
import json
import re
from ..utils import max_calls, Debug
from ..utils.fileutils import fn2k
            #text.append(re.sub(r'^.*?:\s*', '', e['text'].replace('\r','')))
            text.extend(self._flatten_errortext(e['text']))
            line += 1

            a = (e['start']['line']-1, e['start']['character']-1)
            b = (e['end']['line']-1, e['end']['character']-1)
            line_to_pos[line] = (a,b)
            line_to_file[line] = e['file']


        if len(self.lasterrors) == 0:
            text.append("\n\nno errors")

        text.append('\n')

        self.line_to_file = line_to_file
        self.line_to_pos = line_to_pos
        self.text = ''.join(text)


    def _flatten_errortext(self, text_or_dict):
        text = []
        if isinstance(text_or_dict, str):
            text.append(text_or_dict.replace('\r',''))
        elif isinstance(text_or_dict, dict) and 'messageText' in text_or_dict:
            text.append(text_or_dict['messageText'])
            if 'next' in text_or_dict and 'messageText' in text_or_dict['next']:
                text.append('\n   >')
                text.append(text_or_dict['next']['messageText'])
        else:

# Path: lib/utils/debug.py
# def Debug(classification, text):
#     if 'all' in print_classifications or classification in print_classifications:
#         print("ArcticTypescript: %s: %s" % (classification.ljust(8), text))
#     if classification not in possible_classifications:
#         print("ArcticTypescript: debug: got unknown debug message classification: %s. " \
#             "Consider adding this to possible_classifications" % classification)
#     sys.stdout.flush()
# 
# def max_calls(limit = 1500, name=""):
#     """Decorator which allows its wrapped function to be called `limit` times"""
#     def decorator(func):
#         # Disable limit:
#         return func
#         @wraps(func)
#         def wrapper(*args, **kwargs):
#             calls = getattr(wrapper, 'calls', 0)
#             calls += 1
#             setattr(wrapper, 'calls', calls)
#             fname = name if name != "" else func.__name__
# 
#             if calls == limit + 1:
#                 Debug('max_calls', "LIMIT !! ## !!: Fkt %s has %i calls, stop" % (fname, calls - 1))
# 
#             if calls >= limit + 1:
#                 return None
# 
#             Debug('max_calls', "CALL: Fkt %s has %i calls -> +1" % (fname, calls - 1))
# 
#             return func(*args, **kwargs)
#         setattr(wrapper, 'calls', 0)
#         return wrapper
#     return decorator
# 
# Path: lib/utils/fileutils.py
# def fn2k(filename):
#     """ shortcut for filename2key """
#     return filename2key(filename)


# Path: lib/system/Errors.py
import sublime
import json
import re
from ..utils import max_calls, Debug
from ..utils.fileutils import fn2k

            #text.append(re.sub(r'^.*?:\s*', '', e['text'].replace('\r','')))
            text.extend(self._flatten_errortext(e['text']))
            line += 1

            a = (e['start']['line']-1, e['start']['character']-1)
            b = (e['end']['line']-1, e['end']['character']-1)
            line_to_pos[line] = (a,b)
            line_to_file[line] = e['file']


        if len(self.lasterrors) == 0:
            text.append("\n\nno errors")

        text.append('\n')

        self.line_to_file = line_to_file
        self.line_to_pos = line_to_pos
        self.text = ''.join(text)


    def _flatten_errortext(self, text_or_dict):
        text = []
        if isinstance(text_or_dict, str):
            text.append(text_or_dict.replace('\r',''))
        elif isinstance(text_or_dict, dict) and 'messageText' in text_or_dict:
            text.append(text_or_dict['messageText'])
            if 'next' in text_or_dict and 'messageText' in text_or_dict['next']:
                text.append('\n   >')
                text.append(text_or_dict['next']['messageText'])
        else:

            Debug('error', 'typescript-tools error["text"] has unexpected type %s.' % type(text_or_dict))