# Path: webdiff/localfilediff.py
# class LocalFileDiff(object):
#     def __init__(self, a_root, a_path, b_root, b_path, is_move):
#         """A before/after file pair on local disk
# 
#         Args:
#             a_path, b_path: full paths to the files on disk. Either (but not
#                 both) may be empty.
#             a_root, b_root: Paths to the root of diff.
#             is_move: Is this a pure move between the two files?
#         """
#         assert (a_path != '') or (b_path != '')
#         self.a_path = a_path
#         self.b_path = b_path
#         self.a_root = a_root
#         self.b_root = b_root
#         self.is_move = is_move
# 
#     @property
#     def a(self):
#         if self.a_path == '':
#             return ''
#         return os.path.relpath(self.a_path, self.a_root)
# 
#     @property
#     def b(self):
#         if self.b_path == '':
#             return ''
#         return os.path.relpath(self.b_path, self.b_root)
# 
#     @property
#     def type(self):
#         if self.a_path == '':
#             return 'add'
#         elif self.b_path == '':
#             return 'delete'
#         elif self.is_move:
#             return 'move'
#         return 'change'
# 
#     def __repr__(self):
#         return '%s/%s (%s)' % (self.a, self.b, self.type)
# 
# Path: webdiff/util.py
# class ImageMagickNotAvailableError(Exception):
# class ImageMagickError(Exception):
# def memoize(obj):
#     def memoizer(*args, **kwargs):
# def contentHash(path):
# def are_files_identical(path1, path2):
# def image_metadata(path):
# def is_imagemagick_available():
# def generate_pdiff_image(before_path, after_path):
# def generate_dilated_pdiff_image(diff_path):
# def get_pdiff_bbox(diff_path):
#     PIPE = subprocess.PIPE


from collections import defaultdict
from webdiff.localfilediff import LocalFileDiff
from webdiff import util
import copy
import os


def pair_files(a_files, b_files):
    '''Paths must be relative to the diff root for each side.'''
    pairs = []
    for f in a_files[:]:
        if f in b_files:
            i = a_files.index(f)
            j = b_files.index(f)
            pairs.append((f, f))
            del a_files[i]
            del b_files[j]
        else:
            pairs.append((f, ''))  # delete

    for f in b_files:
        pairs.append(('', f))  # add

    return pairs


def find_moves(pairs):
    """Separate the file move pairs from other file pairs"""
    # If a file is just moved, then the added file and the deleted file
    # will both put their idx into the same key of the dictionary
    add_delete_pairs = defaultdict(lambda: [None, None])
    for idx, (a, b) in enumerate(pairs):
        if b and not a:  # add
            add_delete_pairs[util.contentHash(b)][1] = idx
        elif a and not b:  # delete

# Path: webdiff/localfilediff.py
# class LocalFileDiff(object):
#     def __init__(self, a_root, a_path, b_root, b_path, is_move):
#         """A before/after file pair on local disk
# 
#         Args:
#             a_path, b_path: full paths to the files on disk. Either (but not
#                 both) may be empty.
#             a_root, b_root: Paths to the root of diff.
#             is_move: Is this a pure move between the two files?
#         """
#         assert (a_path != '') or (b_path != '')
#         self.a_path = a_path
#         self.b_path = b_path
#         self.a_root = a_root
#         self.b_root = b_root
#         self.is_move = is_move
# 
#     @property
#     def a(self):
#         if self.a_path == '':
#             return ''
#         return os.path.relpath(self.a_path, self.a_root)
# 
#     @property
#     def b(self):
#         if self.b_path == '':
#             return ''
#         return os.path.relpath(self.b_path, self.b_root)
# 
#     @property
#     def type(self):
#         if self.a_path == '':
#             return 'add'
#         elif self.b_path == '':
#             return 'delete'
#         elif self.is_move:
#             return 'move'
#         return 'change'
# 
#     def __repr__(self):
#         return '%s/%s (%s)' % (self.a, self.b, self.type)
# 
# Path: webdiff/util.py
# class ImageMagickNotAvailableError(Exception):
# class ImageMagickError(Exception):
# def memoize(obj):
#     def memoizer(*args, **kwargs):
# def contentHash(path):
# def are_files_identical(path1, path2):
# def image_metadata(path):
# def is_imagemagick_available():
# def generate_pdiff_image(before_path, after_path):
# def generate_dilated_pdiff_image(diff_path):
# def get_pdiff_bbox(diff_path):
#     PIPE = subprocess.PIPE


# Path: webdiff/dirdiff.py
from collections import defaultdict
from webdiff.localfilediff import LocalFileDiff
from webdiff import util
import copy
import os



def pair_files(a_files, b_files):
    '''Paths must be relative to the diff root for each side.'''
    pairs = []
    for f in a_files[:]:
        if f in b_files:
            i = a_files.index(f)
            j = b_files.index(f)
            pairs.append((f, f))
            del a_files[i]
            del b_files[j]
        else:
            pairs.append((f, ''))  # delete

    for f in b_files:
        pairs.append(('', f))  # add

    return pairs


def find_moves(pairs):
    """Separate the file move pairs from other file pairs"""
    # If a file is just moved, then the added file and the deleted file
    # will both put their idx into the same key of the dictionary
    add_delete_pairs = defaultdict(lambda: [None, None])
    for idx, (a, b) in enumerate(pairs):
        if b and not a:  # add
            add_delete_pairs[util.contentHash(b)][1] = idx
        elif a and not b:  # delete

            add_delete_pairs[util.contentHash(a)][0] = idx