<%# locals: () -%>
<div class="box">
  <%= form_with url: '/search', method: :get do |f| %>
    <div class="boxline">
        <%= f.text_field "q", { :value => @search.q, :size => 40 }.
        merge(@search.q.present? ? {} : { :autofocus => "autofocus" }) %>
      <input type="submit" value="Search">
    </div>

    <div class="boxline">
      <label class="required">Search:</label>

      <%= f.radio_button "what", "stories", :checked => @search.what == :stories %>
        <label for="what_stories" class="normal">Stories</label>

      &nbsp;

      <%= f.radio_button "what", "comments", :checked => @search.what == :comments %>
        <label for="what_comments" class="normal">Comments</label>

      <br>

      <label class="required">Order By:</label>

      <%= f.radio_button "order", "newest", :checked => @search.order == :newest %>
        <label for="order_newest" class="normal">Newest</label>

      &nbsp;

      <%= f.radio_button "order", "relevance", :checked => @search.order == :relevance %>
        <label for="order_relevance" class="normal">Relevance</label>

      &nbsp;

      <%= f.radio_button "order", "score", :checked => @search.order == :score %>
        <label for="order_score" class="normal">Score</label>
    </div>
  <% end %>
</div>

<% if @search.results_count > -1 %>
  <div class="box searchresults">
    <details <%= "open" if @search.results_count == 0 %>>
      <summary>
        <span class="heading"><%= @search.results_count %> result<%= @search.results_count == 1 ? "" : "s" %> for </span>
        <span class="searchq"><%= @search.q %></span>
      </summary>

      <% if @search.invalid_because %>
        <div class="flash-error"><%= @search.invalid_because %></div>
      <% end %>

      <p>
      Search query parsed to:
      </p>
      <dl class="search_parse">
        <%
          @search.parse_tree.each do |node|
            type, value = node.first

            case type
          when :commenter %>
          <dt><span class="searchq">commenter:<%= value %></span></dt>
          <dd>Comment by: <%= link_to value, user_path(value) %></dd>
        <% when :domain %>
          <dt><span class="searchq">domain:<%= value %></span></dt>
          <dd>Domain: <%= link_to value, domain_path(value) %></dd>
        <% when :submitter %>
          <dt><span class="searchq">submitter:<%= value %></span></dt>
          <dd>Story submitted by: <%= link_to value, user_path(value) %></dd>
        <% when :tag %>
          <dt><span class="searchq">tag:<%= value %></span></dt>
          <dd>Tag: <%= link_to value, tag_path(value) %></dd>
        <% when :title %>
          <dt><span class="searchq">title:<%= @search.flatten_title(value) %></span></dt>
          <dd>Title: <%= @search.flatten_title(value) %></dd>
        <% when :url %>
          <dt><span class="searchq">url:<%= value %></span></dt>
          <dd>URL: <%= value %></dd>
        <% when :user %>
          <dt><span class="searchq">@<%= value %></span></dt>
          <dd><%= @search.searched_model.name %> by: <%= link_to value, user_path(value) %></dd>
        <% when :negated %>
        <% when :quoted %>
          <dt><span class="searchq">"<%= value.pluck(:term).join(" ") %>"</span></dt>
          <dd>
            Quote: <span class="searchq">"<%= @search.strip_operators(value.pluck(:term).join(" ")) %>"</span>
            <% if !value.to_s.include?(' ') %>
              <br>Quoting a single word is redundant, terms are exact.
            <% end %>
            <% if value.pluck(:term).join(" ").to_s != @search.strip_operators(value.pluck(:term).join(" ")) %>
              <br>Punctuation that might have been taken as a <a href="https://mariadb.com/kb/ru/full-text-index-overview/#in-boolean-mode">SQL search operator</a> was stripped.
            <% end %>
          </dd>
        <% when :term %>
          <dt><span class="searchq"><%= value %></span></dt>
          <dd>
            Term: <span class="searchq"><%= @search.strip_short_terms(@search.strip_operators(value)) %></span>
            <% if value.to_s != @search.strip_operators(value) %>
              <br>Punctuation that might have been taken as a <a href="https://mariadb.com/kb/ru/full-text-index-overview/#in-boolean-mode">SQL search operator</a> was stripped.
            <% end %>
            <% if value.to_s != @search.strip_short_terms(value) %>
              <br>1 and 2 character terms are not indexed.
              <% if value.to_s.downcase =~ /\b(d\b|go\b|ml\b|c([+][+]|\b)?)/ %>
                <% lang = $& %>
                <br>For the
                <% if value.to_s.downcase =~ /\bgo\b/ %>
                  terribly-named
                <% end %>
                language, use <span class="searchq">tag:<%= lang %></span>
              <% end %>
              <% if value.to_s.downcase =~ /\b(ai|ml)\b/ %>
                <br>Use <span class="searchq">tag:ai</span> for artificial intelligence/machine learning
              <% end %>
            <% end %>
          </dd>
        <% when :catchall %>
          <dt><span class="searchq"><%= value %></span></dt>
          <dd>
            Catchall: <span class="searchq"><%= @search.strip_operators(value) %></span>
            If you were trying to use a search operator, it wasn't recognized.
          </dd>
        <% else %>
          <dt>oops</dt>
          <dd>Implementer forgot to handle a search case, please file a bug with your search terms.</dd>
        <% end %>
      <% end %>
      </dl>
    </details>
  </div>

  <% if @search.what == :stories %>
    <ol class="stories list">
      <% @search.results.each do |story| %>
<%#heinous_inline_partial(stories/_listdetail.html.erb)%>
<%# Do not edit, the content before /heinous_inline_partial comes from the named partial %>
<%# locals: (story:, single_story: false) -%>
<%
  # partial inputs:
  single_story ||= false
  # single_story: at the top of a single story page, eg /s/abc123, this
  # controls rendering additional info like list of merged stories, preview,
  # users hiding, and flag summary
%>
<%#
  The h-entry, u-report-of, u-url and h-card css classes are necessary
  for the webmentions support. http://microformats.org/wiki/h-entry

  This has apparently useless <span> tags explained in a comment in CSS
  around .negative_1 in CSS (introduced same commit as this comment).
%>
<li id="story_<%= story.short_id %>" data-shortid="<%= story.short_id %>"
class="story <%= story.current_upvoted? ? "upvoted" : "" %>
<%= story.current_flagged? ? "flagged" : "" %>
<%= story.negativity_class %>
<%= story.is_hidden_by_cur_user ? "hidden" : "" %>
<%= story.is_saved_by_cur_user ? "saved" : "" %>
<%= story.is_deleted? ? "deleted" : "" %>">
<div class="story_liner h-entry">
  <div class="voters">
    <% if @user %>
      <a class="upvoter" title="Add upvote"></a>
    <% else %>
      <%= link_to "", login_path, :class => "upvoter" %>
    <% end %>
    <div class="score"><%= story.show_score_to_user?(@user) ? story.score : '~' %></div>
  </div>
  <div class="details">
    <span role="heading" aria-level="1" class="link h-cite u-repost-of">
      <% if story.merged_story_id %>
        <span class="merge"></span>
      <% end %>
      <% if story.can_be_seen_by_user?(@user) %>
        <a class="u-url" href="<%= story.url_or_comments_path %>" rel="ugc <%= story.send_referrer? ? '' : 'noreferrer' %>"><%= story.title %></a>
      <% end %>
      <% if story.is_gone? %>
        [Story removed by <%= story.is_moderated? ? "moderator" : "submitter" %>]
      <% end %>
    </span>
    <% if story.stories_count > 0 %>
      <span class="merge"></span><%= story.stories_count + 1 %>
    <% end %>
    <% if story.markeddown_description.present? %>
      <a class="description_present" title="<%= truncate(story.description,
        :length => 500) %>" href="<%= story.comments_path %>">&#x2636;</a>
    <% end %>
    <% if story.can_be_seen_by_user?(@user) %>
      <span class="tags">
        <% story.tags.each do |tag| %>
          <%= tag_link(tag) %>
        <% end %>
      </span>
      <% if story.origin.present? %>
        <%= link_to story.origin.identifier, origin_path(story.origin), class: 'domain' %>
      <% elsif story.domain.present? %>
        <%= link_to story.domain.domain, domain_path(story.domain), class: 'domain' %>
      <% end %>

      <% if single_story %>
        <% story.merged_stories.not_deleted(nil).includes(:domain, :user, :tags).each do |ms| %>
          <br>
          <span class="merge"></span>
          <span class="link">
            <a href="<%= ms.url_or_comments_path %>" rel="ugc <%= ms.send_referrer? ? '' : 'noreferrer' %>"><%= ms.title %></a>
          </span>
          <span class="tags">
            <% ms.tags.each do |tag| %>
              <a href="<%= tag_path(tag) %>"
                class="<%= tag.css_class %>"
                title="<%= tag.description %>"><%= tag.tag %></a>
            <% end %>
          </span>
          <% if ms.domain_id.present? %>
            <a class="domain" href="<%= domain_path(ms.domain) %>"><%= ms.domain.domain %></a>
          <% end %>
          <span class="byline">
            <span><%= ms.user_is_author? ? "authored by" : "via" %></span>
            <% if (@user && @user.show_avatars?) || !@user %>
              <%= link_to avatar_img(ms.user, 16), ms.user %>
            <% end %>
            <%= styled_user_link ms.user, story, class: ['u-author', 'h-card'] %>

            <%= how_long_ago_label(ms.created_at) %>
            <% if ms.is_editable_by_user?(@user) %>
              <span> | </span>
              <a href="<%= edit_story_path(ms.short_id) %>">edit</a>
            <% end %>
            <% if @user&.is_moderator? %>
              <span> | </span>
              <a href="<%= edit_mod_story_path(ms.short_id) %>">mod edit</a>
            <% end %>
          </span>
        <% end %>
      <% end %>
    <% end %>

    <% if !single_story && @user && @user.show_story_previews? %>
      <% if (sc = story.description_or_story_text(500)).present? %>
        <div class="story_content">
          <%= sc %>
        </div>
      <% end %>
    <% end %>

    <div class="byline">
      <% if (@user && @user.show_avatars?) || !@user %>
        <%= link_to avatar_img(story.user, 16), user_path(story.user) %>
      <% end %>
      <% if story.previewing %>
        <% if story.user_is_author? %>
          <span> authored by </span>
        <% else %>
          <span> via </span>
        <% end %>

        <%= styled_user_link story.user, story, class: ['u-author', 'h-card'] %>
        <span> just now </span>
      <% else %>
        <% if story.user_is_author? %>
          <span> authored by </span>
        <% else %>
          <span> via </span>
        <% end %>
        <%= styled_user_link story.user, story, class: ['u-author', 'h-card'] %>

        <%= how_long_ago_label(story.created_at) %>

        <% if story.is_editable_by_user?(@user) %>
          <span> | </span>
          <a href="<%= edit_story_path(story.short_id) %>" class="<%= story.has_suggestions? ? "story_has_suggestions" : "" %>">edit</a>
        <% end %>
        <% if @user&.is_moderator? %>
          <span> | </span>
          <a href="<%= edit_mod_story_path(story.short_id) %>" class="<%= story.has_suggestions? ? "story_has_suggestions" : "" %>">mod edit</a>
        <% end %>
        <% if story.can_have_suggestions_from_user?(@user) %>
          <span> | </span><%= link_to "suggest", new_story_suggestion_path(story.short_id),
            :class => "suggester" %>
        <% end %>
        <% if !story.is_gone? && @user %>
          <% if @user && story.current_flagged? %>
            <span> | </span><a class="flagger">unflag (<%=
              Vote::STORY_REASONS[story.current_vote[:reason]].to_s.downcase %>)</a>
          <% elsif @user && @user.can_flag?(story) %>
            <span> | </span>
            <span class="dropdown_parent">
              <a class="flagger">flag</a>
            </span>
          <% end %>
          <% if story.is_hidden_by_cur_user %>
            <span> | </span><%= link_to "unhide", story_unhide_path(story.short_id),
              :class => "hider" %>
          <% else %>
            <span> | </span><%= link_to "hide", story_hide_path(story.short_id),
              :class => "hider" %>
          <% end %>
          <% if single_story && story.disownable_by_user?(@user) %>
          |
          <%= button_to("disown", story_disown_path(story.short_id), form_class: "disowner-form", params: { type: 'story' }) %>
          <% end %>
          <% if single_story && story.hider_count > 0 %>
            (hidden by <%= pluralize(story.hider_count, "user") %>)
          <% end %>
          <% if !story.is_saved_by_cur_user %>
            <span> | </span><%= link_to "save", story_save_path(story.short_id),
                                       :class => "saver" %>
          <% end %>
        <% end %>
        <% if story.is_saved_by_cur_user %>
          <span> | </span><%= link_to "unsave", story_unsave_path(story.short_id),
                                      :class => "saver" %>
        <% end %>
        <% if story.url.present? && (!story.is_gone? || @user.try(:is_moderator?))  %>
          <span> | </span>
          <span class="dropdown_parent">
            <input id="archive_<%= story.short_id %>" class="archive_button" type="checkbox">
            <label for="archive_<%= story.short_id %>">caches</label>
            <div class="archive-dropdown">
              <a href="<%= story.archiveorg_url %>">Archive.org</a>
              <a href="<%= story.archivetoday_url %>">Archive.today</a>
              <a href="<%= story.ghost_url %>">Ghostarchive</a>
            </div>
          </span>
        <% end %>
        <% if !story.is_gone? || @user.try(:is_moderator?) %>
          <span class="comments_label">
            <span> | </span>
            <a role="heading" aria-level="2" href="<%= story.comments_path %>">
            <% if story.comments_count == 0 %>
              no comments</a>
            <% else %>
              <%= story.comments_count %> <%= 'comment'.pluralize(story.comments_count) %></a>
            <% end %>
          </span>
        <% end %>

        <% if single_story &&
        ((story.flags > 0 && @user && @user.is_moderator?) ||
        (story.flags >= 3 || story.score <= 0)) %>
          <span> | <%= story.vote_summary_for(@user).downcase %> </span>
        <% end %>
      <% end %>
    </div>
  </div>
</div>
<a href="<%= story.comments_path %>" class="mobile_comments <%= story.comments_count == 0 ? "zero" : "" %>" style="display: none;">
  <span><%= story.comments_count %></span>
</a>
</li>

<%#/heinous_inline_partial(stories/_listdetail.html.erb)%>
      <% end %>
    </ol>
  <% elsif @search.what == :comments %>
    <ol class="comments comments1">
      <% @search.results.each do |res| %>
        <li><%= render "comments/comment", :comment => res, :show_story => true %></li>
      <% end %>
    </ol>
  <% end %>

  <% if @search.results_count > @search.per_page %>
    <div class="page_link_buttons">
      Page:

      <% page_numbers_for_pagination(@search.page_count, @search.page).each do |p| %>
        <% if p.is_a?(Integer) %>
          <%= link_to p, search_path(@search.to_param.merge(page: p)), class: (@search.page == p ? 'cur' : '') %>
        <% else %>
          <span>...</span>
        <% end %>
      <% end %>
    </div>
  <% elsif @search.results_count == 1 %>
    <div class="page_link_buttons">
      <p>Hapax legomenon</p>
    </div>
  <% end %>
<% else %>
  <div class="help">
    Search hints:
    <ul>
      <li>Search terms are required. Use quote marks if you want <span class="searchq">"words together"</span></li>
      <li>Terms are exact, so <span class="searchq">code</span> doesn't match <span class="searchq">coder</span> or <span class="searchq">coding</span>. You can approximate stemming with <span class="searchq">cod*</span></li>
      <li>Search titles with <span class="searchq">title:kernel</span> or <span class="searchq">title:"linux kernel"</span></li>
      <li>Search by domain with <span class="searchq">domain:example.com</span></li>
      <li>Search by story submitter with <span class="searchq">submitter:alice</span> or by comment author with <span class="searchq">commenter:bob</span></li>
      <li>Search by <%= link_to 'tag', tags_path %> with <span class="searchq">tag:meta</span></li>
    </ul>
  </div>
<% end %>
