{{ define "repo/pulls/fragments/pullActions" }}
  {{ $lastIdx := sub (len .Pull.Submissions) 1 }}
  {{ $roundNumber := .RoundNumber }}
  {{ $stack := .Stack }}

  {{ $totalPulls := sub 0 1 }}
  {{ $below := sub 0 1 }}
  {{ $stackCount := "" }}
  {{ if (gt (len .Stack) 1) }}
    {{ $totalPulls = len $stack }}
    {{ $below = $stack.Below .Pull }}
    {{ $mergeable := len $below.Mergeable }}
    {{ $stackCount = printf "%d/%d" $mergeable $totalPulls }}
  {{ end }}

  {{ $isPushAllowed := .RepoInfo.Roles.IsPushAllowed }}
  {{ $isMerged := .Pull.State.IsMerged }}
  {{ $isClosed := .Pull.State.IsClosed }}
  {{ $isOpen := .Pull.State.IsOpen }}
  {{ $isConflicted := and .MergeCheck (or .MergeCheck.Error .MergeCheck.IsConflicted) }}
  {{ $isPullAuthor := and .LoggedInUser (eq .LoggedInUser.Did .Pull.OwnerDid) }}
  {{ $isLastRound := eq $roundNumber $lastIdx }}
  {{ $isSameRepoBranch := .Pull.IsBranchBased }}
  {{ $isUpToDate := .ResubmitCheck.No }}
  <div id="actions-{{$roundNumber}}" class="flex flex-wrap gap-2 relative p-2">
    <button 
      hx-get="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/round/{{ $roundNumber }}/comment"
      hx-target="#actions-{{$roundNumber}}"
      hx-swap="outerHtml"
      class="btn-flat p-2 flex items-center gap-2 no-underline hover:no-underline group">
        {{ i "message-square-plus" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        comment
    </button>
    {{ if .BranchDeleteStatus }}
      <button 
        hx-delete="/{{ resolve .BranchDeleteStatus.Repo.Did }}/{{ .BranchDeleteStatus.Repo.Slug }}/branches"
        hx-vals='{"branch": "{{ .BranchDeleteStatus.Branch }}" }'
        hx-swap="none"
        class="btn-flat p-2 flex items-center gap-2 no-underline hover:no-underline group text-red-500 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300">
          {{ i "git-branch" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
          {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
          delete branch
      </button>
    {{ end }}
    {{ if and $isPushAllowed $isOpen $isLastRound }}
      {{ $disabled := "" }}
      {{ if $isConflicted }}
        {{ $disabled = "disabled" }}
      {{ end }}
      <button 
        hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/merge"
        hx-swap="none"
        hx-confirm="Are you sure you want to merge pull #{{ .Pull.PullId }} into the `{{ .Pull.TargetBranch }}` branch?"
        class="btn-flat p-2 flex items-center gap-2 group" {{ $disabled }}>
        {{ i "git-merge" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        merge{{if $stackCount}} {{$stackCount}}{{end}}
      </button>
    {{ end }}

    {{ if and $isPullAuthor $isOpen $isLastRound }}
      {{ $disabled := "" }}
      {{ if $isUpToDate }}
        {{ $disabled = "disabled" }}
      {{ end }}
      <button id="resubmitBtn"
          {{ if not .Pull.IsPatchBased }}
            hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/resubmit"
          {{ else }}
            hx-get="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/resubmit"
            hx-target="#actions-{{$roundNumber}}"
            hx-swap="outerHtml"
          {{ end }}

          hx-disabled-elt="#resubmitBtn"
          class="btn-flat p-2 flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed group" {{ $disabled }}

          {{ if $disabled }}
          title="Update this branch to resubmit this pull request"
          {{ else }}
          title="Resubmit this pull request"
          {{ end }}
          >
          {{ i "rotate-ccw" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
          {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
          resubmit
      </button>
    {{ end }}

    {{ if and (or $isPullAuthor $isPushAllowed) $isOpen $isLastRound }}
    <button 
      hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/close"
      hx-swap="none"
      class="btn-flat p-2 flex items-center gap-2 group">
        {{ i "ban" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        close
    </button>
    {{ end }}

    {{ if and (or $isPullAuthor $isPushAllowed) $isClosed $isLastRound }}
    <button 
      hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/reopen"
      hx-swap="none"
      class="btn-flat p-2 flex items-center gap-2 group">
        {{ i "refresh-ccw-dot" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        reopen
    </button>
    {{ end }}
  </div>
{{ end }}


