<%# locals: (scripts:, include_script_sets: true, by_sites: nil, include_search: false, sort_options: nil, libraries: false, rel: nil, skip_all_sites_link: false)  %>

<%
sort_options ||= %w[daily_installs total_installs ratings created updated name]
default_sort = @set&.default_sort || (params[:q].present? ? 'relevance' : (libraries ? 'created' : ScriptListings::DEFAULT_SORT))

current_options = params.to_unsafe_h.slice(:sort, :site, :set, :language, :filter_locale, :by).map {|k, v| [k.to_sym, v] }.to_h.merge(default_sort: default_sort)
current_options[:rel] = rel if rel
%>

<div id="script-list-option-groups" class="list-option-groups">

  <% if include_search %>
    <form class="sidebar-search">
      <% ([:filter_locale, :sort, :site, :language] + advanced_search_params.keys).each do |p| %>
        <% if params[p].is_a?(Array) %>
          <% params[p].each do |v| %>
            <input type="hidden" name="<%= p %>[]" value="<%= v %>">
          <% end %>
        <% elsif params[p].present? %>
          <input type="hidden" name="<%= p %>" value="<%= params[p] %>">
        <% end %>
      <% end %>
      <%= render partial: 'shared/locale_override_field' %>
      <input type="search" name="q" value="<%= params[:q] %>" placeholder="<%= t('scripts.search_placeholder') %>"><input class="search-submit" type="submit" value="🔎">
    </form>
  <% end %>

  <div id="script-list-sort" class="list-option-group"><%= t('scripts.listing_sort_label') %>
    <ul>
      <% sort_options.each do |sort_option| %>
        <%= script_list_link(t("scripts.listing_sort_#{sort_option}"), **current_options.merge(sort: default_sort == sort_option ? nil : sort_option)) %>
      <% end %>
    </ul>
  </div>

  <% if @ad_method&.ga? %>
    <div class="ad adsense-ad" style="max-height: 200px">
      <!-- Script list -->
      <ins class="adsbygoogle"
           style="display:inline-block;width:180px;height:150px"
           data-ad-client="ca-pub-2247465624637408"
           data-ad-slot="4443918292"></ins>
      <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
      </script>
    </div>
  <% end %>

  <% if !by_sites.nil? %>
    <div id="script-list-filter" class="list-option-group"><%= t('scripts.listing_filter_by_site_label') %>
      <ul>
        <%= script_list_link(t('scripts.listing_filter_by_site_disable'), **current_options.merge(site: nil)) %>
        <% current_site_is_top = false
        (0..9).each do |i|
          break if by_sites[i].nil?
          dt = by_sites[i][0]
          if dt.nil?
            current_site_is_top = true if params[:site] == '*' %>
            <%= script_list_link(t('scripts.listing_filter_by_site_all_sites'), **current_options.merge(site: '*')) %>
          <% else
            current_site_is_top = true if dt == params[:site] %>
            <%= script_list_link(dt, **current_options.merge(site: dt)) %>
          <% end %>
        <% end
        # Include the current site if it was not displayed as one of the top ones
        if !current_site_is_top and !params[:site].nil? and !scripts.empty? %>
          <%= script_list_link(params[:site], **current_options) %>
        <% end %>
        <li><%= link_to(t('scripts.listing_filter_by_site_more_sites'), site_list_scripts_path) unless skip_all_sites_link %></li>
      </ul>
    </div>
  <% end %>

  <% unless libraries %>
    <div id="script-language-filter" class="list-option-group"><%= t('scripts.listing_filter_by_language_label') %>
      <ul>
        <% default_language_option = controller.is_a?(UsersController) ? 'all' : 'js' %>
        <% { t('scripts.listing_language_all') => 'all', t('scripts.listing_language_js') => 'js', t('scripts.listing_language_css') => 'css' }.each do |language_option, key| %>
          <%= script_list_link(language_option, **current_options.merge(language: default_language_option == key ? nil : key)) %>
        <% end %>
      </ul>
    </div>
  <% end %>

  <% if (current_user || params[:by].presence) && controller_name != 'users' %>
    <div id="script-list-author" class="list-option-group"><%= t('scripts.listing_filter_by_authorship_label') %>
      <ul>
        <%= script_list_link(t('scripts.listing_author.all'), **current_options.merge(by: nil)) %>
        <% if current_user %>
          <%= script_list_link(t('scripts.listing_author.me'), **current_options.merge(by: current_user.id.to_s)) %>
        <% end %>
        <% if current_user.nil? || current_user.id != params[:by].to_i %>
          <% by = User.find_by(id: params[:by]) %>
          <% if by %>
            <%= script_list_link(t('scripts.listing_author.user', author_name: by.name), **current_options.merge(by: by.id.to_s)) %>
          <% end %>
        <% end %>
      </ul>
    </div>
  <% end %>

  <% # This doesn't work for search
  if include_script_sets && (action_name != 'index' || params[:q].blank?)  && user_signed_in? && !current_user.script_sets.empty? %>
    <div id="script-list-set" class="list-option-group">Script set:
      <ul>
        <%= script_list_link('All', **current_options.merge(set: nil)) %>
        <% ScriptSet.favorites_first(current_user.script_sets).each do |set| %>
          <%= script_list_link(set.display_name, **current_options.merge(set: set.id.to_s)) %>
        <% end %>
      </ul>
    </div>
  <% end %>

  <% if advanced_search_applied? %>
    <div id="script-list-advanced-search" class="list-option-group">
      <ul>
        <li class="list-option list-current"><%= link_to t('scripts.listing_advanced_search_applied'), search_path(**all_search_params) %></li>
        <li class="list-option"><%= link_to t('scripts.listing_advanced_search_clear'), current_script_listing_path(**regular_search_params.except(:site)) %></li>
    </div>
  <% end %>
</div>
