# Path: lib/file_strip/comments.py
# class Comments(object):
#     """Comment strip class."""
# 
#     styles = []
# 
#     def __init__(self, style=None, preserve_lines=False):
#         """Initialize."""
# 
#         self.preserve_lines = preserve_lines
#         self.call = self.__get_style(style)
# 
#     @classmethod
#     def add_style(cls, style, fn):
#         """Add comment style."""
# 
#         if style not in cls.__dict__:
#             setattr(cls, style, fn)
#             cls.styles.append(style)
# 
#     def __get_style(self, style):
#         """Get the comment style."""
# 
#         if style in self.styles:
#             return getattr(self, style)
#         else:
#             raise CommentException(style)
# 
#     def strip(self, text):
#         """Strip comments."""
# 
#         return self.call(text, self.preserve_lines)


import re
from .comments import Comments
def strip_dangling_commas(text, preserve_lines=False):
    """Strip dangling commas."""

    regex = JSON_PATTERN

    def remove_comma(g, preserve_lines):
        """Remove comma."""

        if preserve_lines:
            # ,] -> ] else ,} -> }
            if g["square_comma"] is not None:
                return g["square_ws"] + g["square_bracket"]
            else:
                return g["curly_ws"] + g["curly_bracket"]
        else:
            # ,] -> ] else ,} -> }
            return g["square_bracket"] if g["square_comma"] else g["curly_bracket"]

    def evaluate(m, preserve_lines):
        """Search for dangling comma."""

        g = m.groupdict()
        return remove_comma(g, preserve_lines) if g["code"] is None else g["code"]

    return ''.join(map(lambda m: evaluate(m, preserve_lines), regex.finditer(text)))


def strip_comments(text, preserve_lines=False):
    """Strip JavaScript like comments."""


# Path: lib/file_strip/comments.py
# class Comments(object):
#     """Comment strip class."""
# 
#     styles = []
# 
#     def __init__(self, style=None, preserve_lines=False):
#         """Initialize."""
# 
#         self.preserve_lines = preserve_lines
#         self.call = self.__get_style(style)
# 
#     @classmethod
#     def add_style(cls, style, fn):
#         """Add comment style."""
# 
#         if style not in cls.__dict__:
#             setattr(cls, style, fn)
#             cls.styles.append(style)
# 
#     def __get_style(self, style):
#         """Get the comment style."""
# 
#         if style in self.styles:
#             return getattr(self, style)
#         else:
#             raise CommentException(style)
# 
#     def strip(self, text):
#         """Strip comments."""
# 
#         return self.call(text, self.preserve_lines)


# Path: lib/file_strip/json.py
import re
from .comments import Comments

def strip_dangling_commas(text, preserve_lines=False):
    """Strip dangling commas."""

    regex = JSON_PATTERN

    def remove_comma(g, preserve_lines):
        """Remove comma."""

        if preserve_lines:
            # ,] -> ] else ,} -> }
            if g["square_comma"] is not None:
                return g["square_ws"] + g["square_bracket"]
            else:
                return g["curly_ws"] + g["curly_bracket"]
        else:
            # ,] -> ] else ,} -> }
            return g["square_bracket"] if g["square_comma"] else g["curly_bracket"]

    def evaluate(m, preserve_lines):
        """Search for dangling comma."""

        g = m.groupdict()
        return remove_comma(g, preserve_lines) if g["code"] is None else g["code"]

    return ''.join(map(lambda m: evaluate(m, preserve_lines), regex.finditer(text)))


def strip_comments(text, preserve_lines=False):
    """Strip JavaScript like comments."""


    return Comments('json', preserve_lines).strip(text)