'use strict';

module.exports = function listCategoriesHelper(categories, options) {
  if (!options && (!categories || !categories.hasOwnProperty('length'))) {
    options = categories;
    categories = this.site.categories;
  }

  if (!categories || !categories.length) return '';
  options = options || {};

  var style = options.hasOwnProperty('style') ? options.style : 'list';
  var showCount = options.hasOwnProperty('show_count') ? options.show_count : true;
  var className = options.class || 'category';
  var depth = options.depth ? parseInt(options.depth, 10) : 0;
  var orderby = options.orderby || 'name';
  var order = options.order || 1;
  var transform = options.transform;
  var separator = options.hasOwnProperty('separator') ? options.separator : ', ';
  var showCurrent = options.show_current || false;
  var childrenIndicator = options.hasOwnProperty('children_indicator') ? options.children_indicator : false;
  var result = '';
  var self = this;

  function urlForCategory(category) {
    return self.url_for(self.page.lang + '/' + category.path.replace(self.config.default.category_dir, self.config.category_dir));
  }

  function prepareQuery(parent) {
    var query = {};

    if (parent) {
      query.parent = parent;
    } else {
      query.parent = {$exists: false};
    }

    return categories.find(query).sort(orderby, order).filter(function(cat) {
      return cat.length && cat.posts.filter(function(post) {
        return post.lang == self.page.lang;
      }).length;
    });
  }

  function hierarchicalList(level, parent) {
    var result = '';

    prepareQuery(parent).forEach(function(cat, i) {
      var child;
      if (!depth || level + 1 < depth) {
        child = hierarchicalList(level + 1, cat._id);
      }

      var isCurrent = false;
      if (showCurrent && self.page) {
        for (var j = 0; j < cat.length; j++) {
          var post = cat.posts.data[j];
          if (post && post._id === self.page._id) {
            isCurrent = true;
            break;
          }
        }

        // special case: category page
        if (!isCurrent && self.page.base) {
          if (self.page.base.indexOf(cat.path) === 0) {
            isCurrent = true;
          }
        }
      }

      var additionalClassName = '';
      if (child && childrenIndicator) {
        additionalClassName = ' ' + childrenIndicator;
      }

      result += '<li class="' + className + '-list-item' + additionalClassName + '">';

      result += '<a class="' + className + '-list-link' + (isCurrent ? ' current' : '') + '" href="' + urlForCategory(cat) + '">';
      result += transform ? transform(cat.name) : cat.name;
      result += '</a>';

      if (showCount) {
        result += '<span class="' + className + '-list-count">' + cat.posts.filter(function(post) {
          return post.lang == self.page.lang;
        }).length + '</span>';
      }

      if (child) {
        result += '<ul class="' + className + '-list-child">' + child + '</ul>';
      }

      result += '</li>';
    });

    return result;
  }

  function flatList(level, parent) {
    var result = '';

    prepareQuery(parent).forEach(function(cat, i) {
      if (i || level) result += separator;

      result += '<a class="' + className + '-link" href="' + urlForCategory(cat) + '">';
      result += transform ? transform(cat.name) : cat.name;

      if (showCount) {
        result += '<span class="' + className + '-count">' + cat.posts.filter(function(post) {
          return post.lang == self.page.lang;
        }).length + '</span>';
      }

      result += '</a>';

      if (!depth || level + 1 < depth) {
        result += flatList(level + 1, cat._id);
      }
    });

    return result;
  }

  if (style === 'list') {
    result += '<ul class="' + className + '-list">' + hierarchicalList(0) + '</ul>';
  } else {
    result += flatList(0);
  }

  return result;
};

@@ -2164,17 +2164,17 @@
 th) === 
-0
+3
 ) %7B%0A    

Fix category page current link