<%- title t('discussions.title') %>

<div class="sidebarred">
  <div class="sidebarred-main-content discussion-list-main-content">
    <div class="open-sidebar sidebar-collapsed">
      ☰
    </div>

    <div class="discussion-list-header">
      <h2><%= t('discussions.title') %></h2>
      <% if current_user && @filter_result.read_status != 'read' && @discussions.any? && !params[:q].present? %>
        <div class="discussion-actions">
          <%= link_to t('discussions.mark_all_read'), mark_all_read_discussions_path(category: @filter_result.category, by_user: @filter_result.by_user&.id, related_to_me: @filter_result.related_to_me, read: @filter_result.read_status), data: { turbo: true, turbo_method: :post } %>
        </div>
      <% end %>
    </div>

    <% if @discussions.empty? %>
      <p><%= t('discussions.no_discussions') %></p>
    <% else %>
      <div class="discussion-list <%= 'discussion-list-logged-in' if current_user %>">
        <%= render collection: @comments_to_discussions || @discussions, partial: 'discussions/discussion', locals: { show_script: true, discussion_ids_read: @discussion_ids_read } %>
      </div>
      <%= render_pagination %>
    <% end %>
  </div>

  <div class="sidebar collapsed">
    <div class="close-sidebar">
      <div class="sidebar-title"><%= t('common.listing_options_title') %></div>
      <div>☰</div>
    </div>

    <div class="list-option-groups">
      <p>
        <%= link_to t('discussions.new_button'), new_discussion_path(category: (@filter_result.category unless [DiscussionCategory::SCRIPT_DISCUSSIONS_KEY, DiscussionCategory::NO_SCRIPTS_KEY].include?(@filter_result.category))), class: 'list-option-button' %>
      </p>

      <form class="sidebar-search">
        <% [:sort, :me, :user, :category, :read, :show_locale, :visibility].select {|p| params[p] }.each do |p| %>
          <input type="hidden" name="<%= p %>" value="<%= params[p] %>">
        <% end %>
        <%= render partial: 'shared/locale_override_field' %>
        <input type="search" name="q" value="<%= params[:q] %>" placeholder="<%= t('discussions.search_placeholder') %>"><input class="search-submit" type="submit" value="🔎">
      </form>

      <div id="discussion-list-sort" class="list-option-group"><%= t('scripts.listing_sort_label') %>
        <ul>
          <% if params[:q].present? %>
            <%= discussion_list_link(t('users.listing_relevance'), sort: nil) %>
          <% end %>
          <% if params[:q].present? %>
            <%= discussion_list_link(t('discussions.sort.comment_created'), sort: DiscussionsController::DISCUSSION_SORT_COMMENT_CREATED) %>
          <% end %>
          <%= discussion_list_link(t('discussions.sort.discussion_last_comment'), sort: params[:q].present? ? DiscussionsController::DISCUSSION_SORT_LAST_COMMENT : nil) %>
          <%= discussion_list_link(t('discussions.sort.discussion_created'), sort: DiscussionsController::DISCUSSION_SORT_DISCUSSION_CREATED) %>
        </ul>
      </div>


      <% if current_user %>
        <div class="list-option-group">
          <%= t('discussions.user_filter.label') %>
          <ul>
            <%= discussion_list_link(t('discussions.user_filter.all'), me: nil) %>
            <%= discussion_list_link(t('discussions.user_filter.discussion_by_me'), me: 'started') %>
            <%= discussion_list_link(t('discussions.user_filter.comment_by_me'), me: 'comment') %>
            <%= discussion_list_link(t('discussions.user_filter.on_my_scripts'), me: 'script') %>
            <% if current_user.discussion_subscriptions.any? %>
              <%= discussion_list_link(t('discussions.user_filter.subscribed'), me: 'subscribed') %>
            <% end %>
          </ul>
        </div>
      <% end %>

      <% if @filter_result.by_user %>
        <div class="list-option-group">
          <ul>
            <%= discussion_list_link(t('discussions.user_filter.all'), user: nil) %>
            <%= discussion_list_link(t('discussions.other_user_filter.comments_by', user: @filter_result.by_user.name), user: @filter_result.by_user) %>
          </ul>
        </div>
      <% end %>

      <div class="list-option-group">
        <%= t('discussions.category_filter.label') %>
        <ul>
          <%= discussion_list_link(t('discussions.category_filter.all'), category: nil) %>
          <% DiscussionCategory.visible_to_user(current_user).pluck(:category_key).each do |dc| %>
            <%= discussion_list_link(t("discussions.categories.#{dc}.name"), category: dc) %>
          <% end %>
          <%= discussion_list_link(t('discussions.categories.no-scripts.name'), category: DiscussionCategory::NO_SCRIPTS_KEY) %>
        </ul>
      </div>

      <% if current_user && !params[:q].present? %>
        <div class="list-option-group">
          <%= t('discussions.read_filter.label') %>
          <ul>
            <%= discussion_list_link(t('discussions.read_filter.all'), read: nil) %>
            <%= discussion_list_link(t('discussions.read_filter.read'), read: 'read') %>
            <%= discussion_list_link(t('discussions.read_filter.unread'), read: 'unread') %>
          </ul>
        </div>
      <% end %>

      <div class="list-option-group">
        <%= t('discussions.locale_filter.label') %>
        <ul>
          <%= discussion_list_link(t('discussions.locale_filter.all'), show_locale: nil) %>
          <%= discussion_list_link(request_locale.best_name(in_locale: request_locale), show_locale: request_locale.code) %>
          <% showing_request_locale = @filter_result.locale && @filter_result.locale == request_locale %>
          <li class="list-option <%= 'list-current' if @filter_result.locale && !showing_request_locale %>">
            <select id="discussion-locale">
              <option></option>
              <% @possible_locales.each do |l| %>
                <option data-url="<%= current_path_with_params(show_locale: l.code, page: nil) %>" value="<%= l.code %>" <%= 'selected' if !showing_request_locale && @filter_result.locale && @filter_result.locale.id == l.id %>><%= l.display_text(in_locale: request_locale) %></option>
              <% end %>
            </select>
          </li>
        </ul>
      </div>

      <% if current_user&.moderator? && !params[:q].present? %>
        <div class="list-option-group">
          Visibility
          <ul>
            <%= discussion_list_link('Public', visibility: nil) %>
            <%= discussion_list_link('Private', visibility: 'private') %>
            <%= discussion_list_link('All', visibility: 'all') %>
          </ul>
        </div>
      <% end %>

    </div>
  </div>
</div>
