<% if (site.categories.length){ %>
  <div class="nexmoe-widget-wrap">
    <h3 class="nexmoe-widget-title"><%= __('categories') %></h3>
    <div class="nexmoe-widget">

      <ul class="category-list">

        <%
          /*
           * 获 取 全 部 分 类
           * 并提取出payload
           */

          let categories = Array();
          let categories_counter = 0;

          site.categories.sort('name').each(function(item){
               categories[categories_counter] = {
                 name: item.name,
                 path: item.path,
                 length: item.length
               };

              categories_counter++;
          });
        %>


        <%
          /*
           * 筛选 删掉重复的 将重复的数量放到唯一的哪一个
           *
           */
          let counter;
          let isDisplayed = false;
          let displayed = Array();
          let final_categories = Array();

          //console.log("init categories.length: " + categories.length);

          for (counter = 0; counter < categories.length; counter++){

            ///console.log("starting displayed.length: " + displayed.length + " | finding: " + categories[counter].name);
            for (let i = 0; i < displayed.length; i++){
              if (displayed[i].name === categories[counter].name){
                //console.log("blocked same name: " + categories[counter].name);

                for (let b = 0; b < final_categories.length; b++){
                  if (displayed[i].name === final_categories[b].name){
                    final_categories[b].length++;
                  }
                }

                isDisplayed = true;
                break;
              }else{
                isDisplayed = false;
              }
            }

            if (isDisplayed == false){
              final_categories[final_categories.length] = {
                name: categories[counter].name,
                path: categories[counter].path,
                length: categories[counter].length
              }

              displayed[displayed.length] = {
                name: categories[counter].name,
                path: categories[counter].path,
                length: categories[counter].length
              }
            }

          }
        %>

        <%
        for (let u = 0; u < final_categories.length; u++){
        %>

        <li class="category-list-item">
          <a class="category-list-link" href="<%- config.root %><%- final_categories[u].path %>"><%= final_categories[u].name %></a>
          <span class="category-list-count"><%= final_categories[u].length %></span>
        </li>

        <% } %>
      </ul>

    </div>
  </div>
<% } %>
