{{ define "repo/fragments/searchResults" }}
<div id="repo-files" class="space-y-4 mb-6">
  {{ if .ErrorMsg }}
    <div class="flex items-center gap-2 my-2 bg-red-50 dark:bg-red-900/30 border border-red-300 dark:border-red-700 rounded px-3 py-2 text-red-600 dark:text-red-300 text-sm">
      {{ i "circle-alert" "size-4" }}
      <span>{{ .ErrorMsg }}</span>
    </div>
  {{ else if not .Query }}
    {{ template "searchEmptyState" }}
  {{ else }}
    {{ range .Results }}
      {{ template "resultCard" . }}
    {{ else }}
      <div class="text-base text-gray-500 flex items-center justify-center italic p-12 border border-gray-200 dark:border-gray-700 rounded">
        <span>No results found.</span>
      </div>
    {{ end }}
  {{ end }}
</div>
{{ end }}

{{ define "searchEmptyState" }}
  {{ $code := "px-1 py-0.5 bg-gray-100 dark:bg-gray-700 rounded text-xs font-mono" }}
  <div class="flex flex-col items-center text-center gap-3 p-12 border border-gray-200 dark:border-gray-700 rounded">
    <span class="text-gray-400 dark:text-gray-500">{{ i "search-code" "size-8" }}</span>
    <p class="text-base text-gray-500 dark:text-gray-400">Enter a search term above to find files or code in this repository.</p>
    <p class="text-sm text-gray-500 dark:text-gray-400 max-w-md">
      Type any text to match file contents, or narrow your search with
      <code class="{{ $code }}">lang:go</code>,
      <code class="{{ $code }}">file:readme</code>, or
      <code class="{{ $code }}">case:yes</code>.
    </p>
  </div>
{{ end }}

{{ define "resultCard" }}
  {{ $owner := resolve .Repo.Did }}
  {{ $slug  := .Repo.Slug }}
  {{ if .File }}
    {{/* filename match */}}
    <div class="px-3.5 py-2 text-sm border border-gray-200 dark:border-gray-700 rounded-sm flex items-center justify-between">
      <div class="flex items-center gap-2 min-w-0">
        {{ i "file" "size-4 flex-shrink-0" }}
        <a href="/{{ $owner }}/{{ $slug }}/blob/{{ .Commit }}/{{ .FilePath }}" class="truncate">
          {{- range .File.NameSpans -}}
            {{- if .Match -}}<span class="chunk-match-hl">{{ .Text }}</span>
            {{- else -}}{{ .Text }}{{- end -}}
          {{- end -}}
        </a>
      </div>
      <div class="flex items-center gap-3.5">
        {{ with .Language }}
          {{ template "repo/fragments/colorBall" (dict "color" (langColor .)) }}
          <span>{{ . }}</span>
        {{ end }}
      </div>
    </div>
  {{ else if .Chunks }}
    {{/* chunk match */}}
    <div class="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-sm divide-y divide-gray-200 dark:divide-gray-700">
      <div class="px-3.5 py-2 text-sm flex items-center justify-between">
        <div class="flex items-center gap-2 min-w-0">
          {{ i "file" "size-4 flex-shrink-0" }}
          <a href="/{{ $owner }}/{{ $slug }}/blob/{{ .Commit }}/{{ .FilePath }}" class="truncate">{{ .FilePath }}</a>
        </div>
        <div class="flex items-center gap-3.5">
          {{ with .Language }}
            {{ template "repo/fragments/colorBall" (dict "color" (langColor .)) }}
            <span>{{ . }}</span>
          {{ end }}
        </div>
      </div>
      {{ range $i, $chunk := .Chunks }}
        {{ if lt $i 3 }}
          {{ if gt $i 0 }}
            <div class="bg-gray-50 dark:bg-gray-700 text-center">
              <span class="text-sm text-gray-500 dark:text-gray-400 select-none">···</span>
            </div>
          {{ end }}
          {{ template "search/fragments/chunkBody" (dict "Owner" $owner "Slug" $slug "Commit" $.Commit "FilePath" $.FilePath "Chunk" $chunk) }}
        {{ end }}
      {{ end }}
      {{ if gt (len .Chunks) 3 }}
        <details class="group flex flex-col divide-y divide-gray-200 dark:divide-gray-700">
          <summary class="bg-gray-50 dark:bg-gray-700 text-gray-500 dark:text-gray-400 px-1 py-0.5 cursor-pointer list-none group-open:order-last group-open:border-t border-gray-200 dark:border-gray-700">
            <div class="flex group-open:hidden">
              {{ i "chevron-down" "size-3 m-1" }}
              <span class="text-sm select-none">Show {{ (slice .Chunks 3).MatchCount }} more matches</span>
            </div>
            <div class="hidden group-open:flex">
              {{ i "chevron-up" "size-3 m-1" }}
              <span class="text-sm select-none">Show less</span>
            </div>
          </summary>
          {{ range $i, $chunk := .Chunks }}
            {{ if ge $i 3 }}
              <div class="bg-gray-50 dark:bg-gray-700 text-center">
                <span class="text-sm text-gray-500 dark:text-gray-400 select-none">···</span>
              </div>
              {{ template "search/fragments/chunkBody" (dict "Owner" $owner "Slug" $slug "Commit" $.Commit "FilePath" $.FilePath "Chunk" $chunk) }}
            {{ end }}
          {{ end }}
        </details>
      {{ end }}
    </div>
  {{ end }}
{{ end }}
