# Path: isomorphic/template/backend.py
# class JsTemplate(object):
#     def __init__(self, template_path):
#         self.template_path = template_path
# 
#     def render(self, context=None, request=None):
#         context = context.copy()
#         request_data = {}
# 
#         if request is not None:
#             context['csrf_token'] = get_token(request)
# 
#             request_data['path'] = request.path
#             request_data['absolute_uri'] = request.build_absolute_uri()
# 
#         client = TemplateClient()
#         return client.render_template(self.template_path, context, request_data)
# 
# Path: isomorphic/template/loaders.py
# class JsLoader(Loader):
#     def load_template_source(self, template_name, template_dirs=None):
#         tried = []
#         for filepath in self.get_template_sources(template_name, template_dirs):
#             if exists(filepath):
#                 return filepath, None
#             else:
#                 tried.append(filepath)
# 
#         if tried:
#             error_msg = "Tried %s" % tried
#         else:
#             error_msg = ("Your template directories configuration is empty. "
#                          "Change it to point to at least one template directory.")
#         raise TemplateDoesNotExist(error_msg)
# 
#     def load_template(self, template_name, template_dirs=None):
#         """
#         Only the file path is returned
#         """
#         filepath, display_name = self.load_template_source(template_name, template_dirs)
#         return filepath, template_name


from django import template
from django.conf import settings
from ..template.backend import JsTemplate
from ..template.loaders import JsLoader

register = template.Library()
loader = JsLoader(engine=None)


class JsMissingTemplateDirException(Exception):
    pass


def _get_template_dirs():
    for t in settings.TEMPLATES:
        if t['BACKEND'] == 'isomorphic.template.backend.JsTemplates':
            return t['DIRS']
    return None


def _get_template_path(template_name):
    template_dirs = _get_template_dirs()
    if not template_dirs:
        return None
    template_path, _ = loader.load_template(template_name, template_dirs=template_dirs)
    return template_path


@register.simple_tag(takes_context=True)
def include_js(context, template_name, **kwargs):
    request = context.get('request')
    template_path = _get_template_path(template_name)
    if not template_path:
        raise JsMissingTemplateDirException('No template directory')

# Path: isomorphic/template/backend.py
# class JsTemplate(object):
#     def __init__(self, template_path):
#         self.template_path = template_path
# 
#     def render(self, context=None, request=None):
#         context = context.copy()
#         request_data = {}
# 
#         if request is not None:
#             context['csrf_token'] = get_token(request)
# 
#             request_data['path'] = request.path
#             request_data['absolute_uri'] = request.build_absolute_uri()
# 
#         client = TemplateClient()
#         return client.render_template(self.template_path, context, request_data)
# 
# Path: isomorphic/template/loaders.py
# class JsLoader(Loader):
#     def load_template_source(self, template_name, template_dirs=None):
#         tried = []
#         for filepath in self.get_template_sources(template_name, template_dirs):
#             if exists(filepath):
#                 return filepath, None
#             else:
#                 tried.append(filepath)
# 
#         if tried:
#             error_msg = "Tried %s" % tried
#         else:
#             error_msg = ("Your template directories configuration is empty. "
#                          "Change it to point to at least one template directory.")
#         raise TemplateDoesNotExist(error_msg)
# 
#     def load_template(self, template_name, template_dirs=None):
#         """
#         Only the file path is returned
#         """
#         filepath, display_name = self.load_template_source(template_name, template_dirs)
#         return filepath, template_name


# Path: isomorphic/templatetags/djangojs.py
from django import template
from django.conf import settings
from ..template.backend import JsTemplate
from ..template.loaders import JsLoader


register = template.Library()
loader = JsLoader(engine=None)


class JsMissingTemplateDirException(Exception):
    pass


def _get_template_dirs():
    for t in settings.TEMPLATES:
        if t['BACKEND'] == 'isomorphic.template.backend.JsTemplates':
            return t['DIRS']
    return None


def _get_template_path(template_name):
    template_dirs = _get_template_dirs()
    if not template_dirs:
        return None
    template_path, _ = loader.load_template(template_name, template_dirs=template_dirs)
    return template_path


@register.simple_tag(takes_context=True)
def include_js(context, template_name, **kwargs):
    request = context.get('request')
    template_path = _get_template_path(template_name)
    if not template_path:
        raise JsMissingTemplateDirException('No template directory')

    template = JsTemplate(template_path)