# Path: logic/employee.py
# def employees_matching_prefix(prefix):
#     """Returns a list of (full name, username) tuples for users that match the given prefix."""
#     if not prefix:
#         return []
# 
#     user_tuples = set()
# 
#     search_query = search.Query(
#         query_string=prefix,
#         options=search.QueryOptions(
#             limit=15))
#     results = search.Index(name=INDEX_NAME).search(search_query)
#     for r in results:
#         username, full_name = None, None
#         for f in r.fields:
#             if f.name == 'full_name':
#                 full_name = f.value
#             elif f.name == 'username':
#                 username = f.value
#             else:
#                 continue
#         if username is not None and full_name is not None:
#             photo_url = Employee.query(Employee.username == username).get().get_photo_url()
#             user_tuples.add((full_name, username, photo_url))
# 
#     user_tuples = list(user_tuples)
#     user_tuples.sort()
#     return user_tuples
# 
# Path: util/render.py
# def make_json_response(data, *args):
#     response = make_response(json.dumps(data), *args)
#     response.headers['Content-Type'] = 'application/json'
#     return response


from logic.employee import employees_matching_prefix
from util.render import make_json_response
# -*- coding: utf-8 -*-


def autocomplete(request):
    """This is the implementation of the autocomplete API endpoint. It's shared between
    the api view and web view that is called from Javascript, only the authorization
    checks are different.
    """

# Path: logic/employee.py
# def employees_matching_prefix(prefix):
#     """Returns a list of (full name, username) tuples for users that match the given prefix."""
#     if not prefix:
#         return []
# 
#     user_tuples = set()
# 
#     search_query = search.Query(
#         query_string=prefix,
#         options=search.QueryOptions(
#             limit=15))
#     results = search.Index(name=INDEX_NAME).search(search_query)
#     for r in results:
#         username, full_name = None, None
#         for f in r.fields:
#             if f.name == 'full_name':
#                 full_name = f.value
#             elif f.name == 'username':
#                 username = f.value
#             else:
#                 continue
#         if username is not None and full_name is not None:
#             photo_url = Employee.query(Employee.username == username).get().get_photo_url()
#             user_tuples.add((full_name, username, photo_url))
# 
#     user_tuples = list(user_tuples)
#     user_tuples.sort()
#     return user_tuples
# 
# Path: util/render.py
# def make_json_response(data, *args):
#     response = make_response(json.dumps(data), *args)
#     response.headers['Content-Type'] = 'application/json'
#     return response


# Path: views/common.py
from logic.employee import employees_matching_prefix
from util.render import make_json_response

# -*- coding: utf-8 -*-


def autocomplete(request):
    """This is the implementation of the autocomplete API endpoint. It's shared between
    the api view and web view that is called from Javascript, only the authorization
    checks are different.
    """

    matches = employees_matching_prefix(request.args.get('term', None))