<%!
from staticfiles.storage import staticfiles_storage
from pipeline_mako import compressed_css, compressed_js
from django.utils.translation import get_language_bidi
%>

<%def name='url(file, raw=False)'><%
try:
    url = staticfiles_storage.url(file)
except:
    url = file
%>${url}${"?raw" if raw else ""}</%def>

<%def name='css(group, raw=False)'>
  <%
      rtl_group = '{}-rtl'.format(group)

      if get_language_bidi() and rtl_group in settings.PIPELINE_CSS:
        group = rtl_group
  %>

  % if settings.FEATURES['USE_DJANGO_PIPELINE']:
    ${compressed_css(group, raw=raw)}
  % else:
    % for filename in settings.PIPELINE_CSS[group]['source_filenames']:
      <link rel="stylesheet" href="${staticfiles_storage.url(filename.replace('.scss', '.css'))}${"?raw" if raw else ""}" type="text/css" media="all" / >
    % endfor
  %endif
</%def>

<%def name='js(group)'>
  % if settings.FEATURES['USE_DJANGO_PIPELINE']:
    ${compressed_js(group)}
  % else:
    % for filename in settings.PIPELINE_JS[group]['source_filenames']:
      <script type="text/javascript" src="${staticfiles_storage.url(filename.replace('.coffee', '.js'))}"></script>
    % endfor
  %endif
</%def>

## A language-direction indicator, suitable for use in class="" attributes,
## for example:
##
##      <body class="${dir_rtl()}">
##
<%def name="dir_rtl()"><%
    return 'rtl' if get_language_bidi() else 'ltr'
%></%def>

<%def name="include(path)"><%
from django.template.loaders.filesystem import _loader
source, template_path = _loader.load_template_source(path)
%>${source}</%def>
