{{ define "title" }}pulls &middot; {{ .RepoInfo.FullName }}{{ end }}

{{ define "extrameta" }}
    {{ $title := "pulls"}}
    {{ $url := printf "https://tangled.org/%s/pulls" .RepoInfo.FullName }}

    {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }}
{{ end }}

{{ define "repoContent" }}
  {{ $active := .FilterState }}
  {{ $open :=
     (dict
       "Key" "open"
       "Value" "open"
       "Icon" "git-pull-request"
       "Meta" (string .RepoInfo.Stats.PullCount.Open)) }}
  {{ $merged :=
     (dict
       "Key" "merged"
       "Value" "merged"
       "Icon" "git-merge"
       "Meta" (string .RepoInfo.Stats.PullCount.Merged)) }}
  {{ $closed :=
     (dict
       "Key" "closed"
       "Value" "closed"
       "Icon" "ban"
       "Meta" (string .RepoInfo.Stats.PullCount.Closed)) }}
  {{ $values := list $open $merged $closed }}
  <div class="grid gap-2 grid-cols-[auto_1fr_auto] grid-row-2">
    <form id="search-form" class="flex relative col-span-3 sm:col-span-1 sm:col-start-2" method="GET">
      <div class="flex-1 flex relative">
        <input
          id="search-q"
          class="flex-1 py-1 pl-2 pr-10 mr-[-1px] rounded-r-none peer"
          type="text"
          name="q"
          value="{{ .FilterQuery }}"
          placeholder="search pulls..."
        >
        <a
          {{ if $active }}href="?q=state:{{ $active }}"{{ else }}href="?"{{ end }}
          class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hidden peer-[:not(:placeholder-shown)]:block"
        >
          {{ i "x" "w-4 h-4" }}
        </a>
      </div>
      <button
        type="submit"
        class="p-2 text-gray-400 border rounded-r border-gray-300 dark:border-gray-600"
      >
        {{ i "search" "w-4 h-4" }}
      </button>
    </form>
    <div class="sm:row-start-1">
      {{ template "fragments/tabSelector" (dict "Name" "state" "Values" $values "Active" $active "Include" "#search-q" "Form" "search-form") }}
    </div>
    <a
      href="/{{ .RepoInfo.FullName }}/pulls/new"
      class="col-start-3 btn-create text-sm flex items-center gap-2 no-underline hover:no-underline hover:text-white"
    >
      {{ i "git-pull-request-create" "w-4 h-4" }}
      <span>new</span>
    </a>
  </div>
  <div class="error" id="pulls"></div>
{{ end }}

{{ define "repoAfter" }}
    <div class="flex flex-col gap-2 mt-2">
        {{ range $stack := .Stacks }}
          {{ $topPR := index $stack 0 }}
          <div class="rounded bg-white dark:bg-gray-800">
            <div class="px-6 py-4 z-5">
                <div class="pb-2">
                    <a href="/{{ $.RepoInfo.FullName }}/pulls/{{ $topPR.PullId }}" class="dark:text-white">
                        {{ $topPR.Title | description }}
                        <span class="text-gray-500 dark:text-gray-400">#{{ $topPR.PullId }}</span>
                    </a>
                </div>
                <div class="text-sm text-gray-500 dark:text-gray-400 flex flex-wrap items-center gap-1">
                    {{ template "repo/pulls/fragments/pullState" $topPR.State }}
                    <span class="ml-1 flex items-center gap-1">
                        {{ template "user/fragments/picLink" (list $topPR.OwnerDid "size-6" (index $.VouchRelationships $topPR.OwnerDid)) }}
                    <a href="/{{ resolve $topPR.OwnerDid }}">{{ resolve $topPR.OwnerDid }}</a>
                    </span>

                    <span class="before:content-['·']">
                        {{ template "repo/fragments/time" $topPR.Created }}
                    </span>

                    <span class="before:content-['·']">
                      {{ $commentCount := $topPR.TotalComments }}
                      {{ $commentCount }} comment{{ if ne $commentCount 1 }}s{{ end }}
                    </span>

                    <span class="before:content-['·']">
                      round
                      <span class="font-mono">
                        #{{ $topPR.LastRoundNumber }}
                      </span>
                    </span>

                    {{ $pipeline := index $.Pipelines $topPR.LatestSha }}
                    {{ if and $pipeline $pipeline.Id }}
                      <span class="before:content-['·']"></span>
                      {{ template "repo/pipelines/fragments/pipelineSymbol" (dict "Pipeline" $pipeline "ShortSummary" true) }}
                    {{ end }}

                    {{ $state := $topPR.Labels }}
                    {{ range $k, $d := $.LabelDefs }}
                      {{ range $v, $s := $state.GetValSet $d.AtUri.String }}
                        {{ template "labels/fragments/label" (dict "def" $d "val" $v "withPrefix" true) }}
                      {{ end }}
                    {{ end }}
                </div>
              </div>
              {{ if gt (len $stack) 1 }}
                <details class="group">
                  <summary class="px-6 pb-4 text-xs list-none cursor-pointer hover:text-gray-500 hover:dark:text-gray-400">
                    <span class="flex items-center gap-2">
                      <span class="group-open:hidden">
                        {{ i "chevrons-up-down" "size-3" }}
                      </span>
                      <span class="hidden group-open:flex">
                        {{ i "chevrons-down-up" "size-3" }}
                      </span>
                      {{ $rest := sub (len $stack) 1 }}
                      expand {{ $rest }} pull{{if ne $rest 1 }}s{{end}} in this stack
                    </span>
                  </summary>
                  {{ template "stackedPullList" (list (slice $stack 1) $) }}
                </details>
              {{ end }}
            </div>
        {{ end }}
    </div>
    {{if gt .PullCount .Page.Limit }}
      {{ template "fragments/pagination" (dict
          "Page" .Page
          "TotalCount" .PullCount
          "BasePath" (printf "/%s/pulls" .RepoInfo.FullName)
          "QueryParams" (queryParams "q" .FilterQuery)
      ) }}
    {{ end }}
{{ end }}

{{ define "stackedPullList" }}
  {{ $list := index . 0 }}
  {{ $root := index . 1 }}
  <div class="grid grid-cols-1 rounded-b border-b border-t border-gray-200 dark:border-gray-900 divide-y divide-gray-200 dark:divide-gray-900">
    {{ range $pull := $list }}
      {{ $pipeline := index $root.Pipelines $pull.LatestSha }}
      <a href="/{{ $root.RepoInfo.FullName }}/pulls/{{ $pull.PullId }}" class="no-underline hover:no-underline hover:bg-gray-100/25 hover:dark:bg-gray-700/25">
        <div class="flex gap-2 items-center px-6">
          <div class="flex-grow min-w-0 w-full py-2">
            {{ template "repo/pulls/fragments/summarizedPullHeader" (list $pull $pipeline) }}
          </div>
        </div>
      </a>
    {{ end }}
  </div>
{{ end }}
