{{ define "repo/pulls/fragments/diff" }}
  {{ $split := .Split }}
  <div class="space-y-4">
    {{ range .Files }}
      {{ $ins := 0 }}
      {{ $del := 0 }}
      {{ range .Hunks }}
        {{ if $split }}
          {{ range .Rows }}
            {{ if eq .Left.Kind "del" }}{{ $del = add $del 1 }}{{ end }}
            {{ if eq .Right.Kind "add" }}{{ $ins = add $ins 1 }}{{ end }}
          {{ end }}
        {{ else }}
          {{ range .Lines }}
            {{ if eq .Op "-" }}{{ $del = add $del 1 }}{{ end }}
            {{ if eq .Op "+" }}{{ $ins = add $ins 1 }}{{ end }}
          {{ end }}
        {{ end }}
      {{ end }}

      <div class="bg-white dark:bg-gray-800 drop-shadow-sm rounded border border-gray-200 dark:border-gray-700 overflow-clip">
        <details class="group w-full" open>
          <summary class="list-none cursor-pointer sticky top-12 z-10 group-open:border-b border-gray-200 dark:border-gray-700">
            <div class="flex justify-between bg-white dark:bg-gray-800">
              <div class="p-2 flex gap-2 items-center">
                <span class="group-open:hidden inline">{{ i "chevron-right" "w-4 h-4" }}</span>
                <span class="hidden group-open:inline">{{ i "chevron-down" "w-4 h-4" }}</span>
                {{ template "repo/fragments/diffStatPill" (dict "Insertions" $ins "Deletions" $del) }}
                <span>{{ .Path }}</span>
              </div>
              <div></div>
            </div>
          </summary>

          {{ if .Note }}
            <div class="p-4 text-center text-gray-400 dark:text-gray-500 text-sm">{{ .Note }}</div>
          {{ else if $split }}
            {{ template "pullDiffSplitTable" . }}
          {{ else }}
            {{ template "pullDiffUnifiedTable" . }}
          {{ end }}
        </details>
      </div>
    {{ end }}
  </div>
{{ end }}

{{/* pullDiffUnifiedTable renders a DiffFile's hunks as a 3-column unified table. */}}
{{ define "pullDiffUnifiedTable" }}
  <table class="diff w-full">
    <colgroup>
      <col width="40" />
      <col width="40" />
      <col />
    </colgroup>
    <tbody>
      {{- range .Hunks -}}
        <tr class="splitter"><td colspan="3">&middot;&middot;&middot;</td></tr>
        {{- range .Lines -}}
          {{- $cls := "" -}}
          {{- if eq .Op "+" -}}{{- $cls = "added" -}}{{- else if eq .Op "-" -}}{{- $cls = "deleted" -}}{{- end -}}
          <tr{{ with $cls }} class="{{ . }}"{{ end }}>
            <td class="num">{{ if ge .Old 0 }}{{ .Old }}{{ end }}</td>
            <td class="num">{{ if ge .New 0 }}{{ .New }}{{ end }}</td>
            <td class="content" data-diff-marker="{{ .Op }}">{{ .Content }}</td>
          </tr>
        {{- end -}}
      {{- end -}}
    </tbody>
  </table>
{{ end }}

{{/* pullDiffSplitTable renders a DiffFile's hunks as a 4-column side-by-side table. */}}
{{ define "pullDiffSplitTable" }}
  <table class="diff w-full">
    <colgroup>
      <col width="40" />
      <col />
      <col width="40" />
      <col />
    </colgroup>
    <tbody>
      {{- range .Hunks -}}
        <tr class="splitter"><td colspan="4">&middot;&middot;&middot;</td></tr>
        {{- range .Rows -}}
          <tr>
            {{- template "pullDiffSplitCell" .Left -}}
            {{- template "pullDiffSplitCell" .Right -}}
          </tr>
        {{- end -}}
      {{- end -}}
    </tbody>
  </table>
{{ end }}

{{/* pullDiffSplitCell renders the num + content <td>s for one side of a split row.
     Dot is a DiffCell (Kind ctx|del|add|empty, Num, Content). */}}
{{ define "pullDiffSplitCell" }}
  {{- $cls := "" -}}
  {{- $mark := "" -}}
  {{- if eq .Kind "del" -}}{{- $cls = "deleted" -}}{{- $mark = "-" -}}
  {{- else if eq .Kind "add" -}}{{- $cls = "added" -}}{{- $mark = "+" -}}
  {{- else if eq .Kind "empty" -}}{{- $cls = "empty" -}}{{- end -}}
  <td class="{{ with $cls }}{{ . }} {{ end }}num">{{ if ge .Num 0 }}{{ .Num }}{{ end }}</td>
  <td class="{{ with $cls }}{{ . }} {{ end }}content" data-diff-marker="{{ $mark }}">{{ .Content }}</td>
{{ end }}
