{{- /*
  Recursive function to render tree from structured data
  @param {Array} items - Array of tree items
  @param {Int} level - The expand level of the tree (expand all: -1, collapse all: 0)
  @param {Int} [depth=0] - Current depth level
  @param {Boolean} [folder_slash=false] - Whether to append a trailing "/" to folder names
  @param {Array} [ignore_list] - List of file or folder names to ignore
  @param {Array} [highlight_list] - List of file or folder names to highlight

  File tree item structure:
    name: string     - The name of the file or folder
    type: string     - Type of item, either "dir" (directory) or "file"
    children?: array - (Optional) Array of child items, only valid for directories
*/ -}}
{{- define "render-file-tree" -}}
  {{- $items := .items -}}
  {{- $level := .level -}}
  {{- $depth := .depth | default 0 -}}
  {{- $folderSlash := .folder_slash | default false -}}
  {{- $ignoreList := .ignore_list | default slice -}}
  {{- $highlightList := .highlight_list | default slice -}}

  <ul class="file-tree" data-level="{{ $depth }}">
    {{- /* Sort items: directories first, then files */ -}}
    {{- $dirItems := slice -}}
    {{- $fileItems := slice -}}
    {{- range $items -}}
      {{- if in $ignoreList .name -}}
        {{- continue -}}
      {{- end -}}
      {{- if eq .type "dir" -}}
        {{- $dirItems = $dirItems | append . -}}
      {{- else -}}
        {{- $fileItems = $fileItems | append . -}}
      {{- end -}}
    {{- end -}}

    {{- range (append $fileItems $dirItems) -}}
      {{- $isDir := eq .type "dir" -}}
      {{- $isCollapsed := $isDir | and (ge $depth $level) | and (ge $level 0) -}}
      {{- $isHighlighted := in $highlightList .name -}}
      {{- $label := .name -}}
      {{- if and $isDir $folderSlash -}}
        {{- $label = printf "%s/" $label -}}
      {{- end -}}

      <li class="file-tree-item{{ if $isDir }} file-tree-folder{{ end }}{{ if $isCollapsed }} is-collapsed{{ end }}">
        <span class="file-tree-label{{ if $isDir }} file-tree-toggle{{ end }}{{ if $isHighlighted }} is-highlighted{{ end }}">
          {{- $icon := "file-tree-icon fa-regular " -}}
          {{- if $isDir -}}
            {{- $icon = add $icon "fa-folder-open" -}}
          {{- else -}}
            {{- $icon = add $icon "fa-file-lines" -}}
          {{- end -}}
          {{- dict "Class" $icon | partial "plugin/icon.html" -}}
          <span class="file-tree-label__inner">{{ $label }}</span>
        </span>

        {{- if and $isDir .children -}}
          {{- $options := dict
            "items" .children
            "depth" (add $depth 1)
            "level" $level
            "folder_slash" $folderSlash
            "ignore_list" $ignoreList
            "highlight_list" $highlightList
          -}}
          {{- template "render-file-tree" $options -}}
        {{- end -}}
      </li>
    {{- end -}}
  </ul>
{{- end -}}

{{- if .items -}}
  {{- $ignoreList := .ignore_list | default slice -}}
  {{- $notEmpty := false -}}
  {{- range .items -}}
    {{- if not (in $ignoreList .name) -}}
      {{- $notEmpty = true -}}
      {{- break -}}
    {{- end -}}
  {{- end -}}
  {{- if $notEmpty -}}
    <div class="file-tree-wrapper">
      {{- template "render-file-tree" . -}}
    </div>
  {{- end -}}
{{- end -}}
