# frozen_string_literal: true

module HomeHelper
  def width(project, required, max)
    if required == 'most_recent_projects'
      1
    else
      count = project_count(project, required)
      max = 1 if max.to_i.zero?
      [1, count.to_i * 60 / max].max
    end
  end

  def project_count(item, required)
    case required
    when 'most_popular_projects'
      item.user_count
    when 'most_active_projects'
      item.best_analysis.thirty_day_summary.commits_count if item.best_analysis.present?
    when 'most_active_contributors'
      item.best_account_analysis.account_analysis_fact&.thirty_day_commits
    when 'most_recent_projects'
      item.created_at.strftime('%b %d, %Y')
    end
  end

  def set_link(project)
    if project.is_a?(Account)
      link_to(image_tag(avatar_img_path(project), height: 32, width: 32), account_path(project), class: 'top_ten_icon')
    else
      link_to(capture_haml { project_icon(project) }, project_path(project), class: 'top_ten_icon')
    end
  end

  def set_path(project)
    path = project.is_a?(Account) ? account_path(project) : project_path(project)
    link_to(h(project.name), path)
  end
  # :nocov:

  def home_top_lists
    Rails.cache.fetch device? ? 'homepage_top_lists_device' : 'homepage_top_lists' do
      render partial: 'top_lists'
    end
  end
  # :nocov:
end
