# 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
# --------------------------------------- ERRORS -------------------------------------- #

class Errors(object):
    def __init__(self, project):
        self.project = project
        self.lasterrors = []
        self.message = "" # contains exception message if an error occured
        pass

    @max_calls()
    def start_recalculation(self):
        self.project.tsserver.errors(self.on_results)

    @max_calls()
    def on_results(self, errors):
        """ this is the callback from the async process if new errors have been calculated """
        try:
            self.failure = ""
            self.lasterrors = json.loads(errors)
            if isinstance(self.lasterrors, str):
                self.lasterrors = [ self._provide_better_explanations_for_tss_errors(self.lasterrors)]
                # self.lasterrors is a list or None
            if not isinstance(self.lasterrors, list):
                raise Warning("tss.js internal error: %s" % errors)
            self._provide_better_explanations_for_some_errors()
            self._tssjs_to_errorview()

        except BaseException as e: # Also catches JSON exceptions
            self.lasterrors = []
            self.failure = "%s" % e

# 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

# --------------------------------------- ERRORS -------------------------------------- #

class Errors(object):
    def __init__(self, project):
        self.project = project
        self.lasterrors = []
        self.message = "" # contains exception message if an error occured
        pass

    @max_calls()
    def start_recalculation(self):
        self.project.tsserver.errors(self.on_results)

    @max_calls()
    def on_results(self, errors):
        """ this is the callback from the async process if new errors have been calculated """
        try:
            self.failure = ""
            self.lasterrors = json.loads(errors)
            if isinstance(self.lasterrors, str):
                self.lasterrors = [ self._provide_better_explanations_for_tss_errors(self.lasterrors)]
                # self.lasterrors is a list or None
            if not isinstance(self.lasterrors, list):
                raise Warning("tss.js internal error: %s" % errors)
            self._provide_better_explanations_for_some_errors()
            self._tssjs_to_errorview()

        except BaseException as e: # Also catches JSON exceptions
            self.lasterrors = []
            self.failure = "%s" % e

            Debug('error', 'Internal ArcticTypescript error during show_errors: %s (Exception Message: %s)' % (errors, e))