function initializeFetchFollowedArticles() {
  if ( document.getElementById("featured-story-marker") && checkUserLoggedIn() ) {
    algoliaFollowedArticles();
  }
}
  
function insertArticle(article,parent,insertPlace) {
  if (article){
    var newItem = document.createElement("DIV");
    newItem.innerHTML = buildArticleHTML(article)
    parent.insertBefore(newItem, insertPlace);
    initializeReadingListIcons();
  }
}

function insertInitialArticles(user) {
  var el = document.getElementById("home-articles-object");
  if (!el) {return}
  el.innerHTML = "";
  insertNewArticles(user);
  insertTopArticles(user);
  el.outerHTML = "";
}

function insertNewArticles(user){
  var el = document.getElementById("new-articles-object");
  var articlesJSON = JSON.parse(el.dataset.articles);
  var insertPlace = document.getElementById("article-index-hidden-div");
  if (insertPlace) {
      articlesJSON.forEach(function(article){
      var articlePoints = 0
      var containsUserID = findOne([article.user_id], user.followed_user_ids || [])
      var containsOrganizationID = findOne([article.organization_id], user.followed_organization_ids || [])
      var intersectedTags = intersect_arrays(user.followed_tag_names, article.cached_tag_list_array)
      var followedPoints = 1
      JSON.parse(user.followed_tags).map(function(tag) {
        if (intersectedTags.includes(tag.name)) {
          followedPoints = followedPoints + tag.points
        }
      })
      articlePoints = articlePoints + (followedPoints*2) + article.positive_reactions_count
      if (containsUserID || article.user_id === user.id) {
        articlePoints = articlePoints + 16
      }
      if (containsOrganizationID) {
        articlePoints = articlePoints + 16
      }
      article['points'] = articlePoints
    });
    var sortedArticles = articlesJSON.sort(function(a, b) {
      return b.points - a.points;
    });
    sortedArticles.forEach(function(article){
      var parent = insertPlace.parentNode;
      if ( article.points > 15 && !document.getElementById("article-link-"+article.id) ) {
        insertArticle(article,parent,insertPlace);
      }
    });
  }
}

function insertTopArticles(user){
  var el = document.getElementById("home-articles-object");
  var articlesJSON = JSON.parse(el.dataset.articles)
  var insertPlace = document.getElementById("article-index-hidden-div");
  if (insertPlace) {
      articlesJSON.forEach(function(article){
      var articlePoints = 0
      var containsUserID = findOne([article.user_id], user.followed_user_ids || [])
      var containsOrganizationID = findOne([article.organization_id], user.followed_organization_ids || [])
      var intersectedTags = intersect_arrays(user.followed_tag_names, article.cached_tag_list_array)
      var followedPoints = 1
      JSON.parse(user.followed_tags).map(function(tag) {
        if (intersectedTags.includes(tag.name)) {
          followedPoints = followedPoints + tag.points
        }
      })
      articlePoints = articlePoints + followedPoints
      if (containsUserID) {
        articlePoints = articlePoints + 1
      }
      if (containsOrganizationID) {
        articlePoints = articlePoints + 1
      }
      article['points'] = articlePoints
    });
    var sortedArticles = articlesJSON.sort(function(a, b) {
      return b.points - a.points;
    });
    sortedArticles.forEach(function(article){
      var parent = insertPlace.parentNode;
      if ( article.points > 0 && !document.getElementById("article-link-"+article.id) ) {
        insertArticle(article,parent,insertPlace);
      }
    });
  }
}

function algoliaFollowedArticles(){
    var user = userData();
    if (user && user.followed_tag_names && user.followed_tag_names.length > 0) {
      insertInitialArticles(user)
      var followedUsersArray = [];
      if (user.followed_user_ids){
        followedUsersArray = user.followed_user_ids.map(function(id){return 'user_'+id});
      }
      var publicSearchKey = '<%= ALGOLIASEARCH_PUBLIC_SEARCH_ONLY_KEY %>'
      var client = algoliasearch('<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>', publicSearchKey);
      var index = client.initIndex('ordered_articles_<%= Rails.env %>');
      
      var searchObj = {
          hitsPerPage: 20,
          page: "0",
          attributesToHighlight: [],
          tagFilters: [user.followed_tag_names.concat(followedUsersArray)],
      }
      
      var homeEl = document.getElementById("index-container");
      if (homeEl.dataset.feed === "base-feed") {
        articlesIndex = client.initIndex('ordered_articles_<%= Rails.env %>');
      } else if (homeEl.dataset.feed === "latest") {
        articlesIndex = client.initIndex('ordered_articles_by_published_at_<%= Rails.env %>');
      } else {
        searchObj["filters"] = ('published_at_int > ' + homeEl.dataset.articlesSince);
        articlesIndex = client.initIndex('ordered_articles_by_positive_reactions_count_<%= Rails.env %>');
      }
      articlesIndex.search("*", searchObj)
      .then(function searchDone(content) {
        var insertPlace = document.getElementById("article-index-hidden-div");
        if (insertPlace) {
          var parent = insertPlace.parentNode;
          content.hits.forEach(function(article){
            if ( !document.getElementById("article-link-"+article.id) ) {
              insertArticle(article,parent,insertPlace);
            }
          });
        }
        var el = document.getElementById("home-articles-object")
        if (el){el.outerHTML = ''}
      });
    }
}

function findOne(haystack, arr) {
    return arr.some(function (v) {
        return haystack.indexOf(v) >= 0;
    });
};


function intersect_arrays(a, b) {
    var sorted_a = a.concat().sort();
    var sorted_b = b.concat().sort();
    var common = [];
    var a_i = 0;
    var b_i = 0;

    while (a_i < a.length
           && b_i < b.length)
    {
        if (sorted_a[a_i] === sorted_b[b_i]) {
            common.push(sorted_a[a_i]);
            a_i++;
            b_i++;
        }
        else if(sorted_a[a_i] < sorted_b[b_i]) {
            a_i++;
        }
        else {
            b_i++;
        }
    }
    return common;
}