# shellcheck disable=SC2154
# adding a suffix like ` (trimmed)` or ` | echo-trim-padding --stdin` to the file name disables syntax highlighting unfortunately
if true; then
  __read() {
    if [ "${FILE}" = '/dev/stdin' ]; then
      # STDIN
      if [ "${option_trim}" = 'yes' ]; then
        echo-trim-padding --stdin | bat --file-name "${NAME}" "${bat_options}" - || return $?
      else
        bat --file-name "${NAME}" "${bat_options}" - || return $?
      fi
    else
      # FILE
      if [ "${option_trim}" = 'yes' ]; then
        __wrap cat "${FILE}" | echo-trim-padding --stdin | bat --file-name "${NAME}" "${bat_options}" - || return $?
      else
        __wrap cat "${FILE}" | bat --file-name "${NAME}" "${bat_options}" - || return $?
      fi
    fi
  }
else
  cat_options=""
  if [ "${option_hidden}" = 'yes' ]; then
    #  -e      Display non-printing characters (see the -v option), and display a dollar sign (‘$’) at the end of each line.
    # -t      Display non-printing characters (see the -v option), and display tab characters as ‘^I’.
    # -v      Display non-printing characters so they are visible.  Control characters print as ‘^X’ for control-X; the delete character (octal 0177) prints as ‘^?’.  Non-ASCII characters (with the high bit set) are printed as ‘M-’ (for meta) followed by the character for the low 7 bits.
    cat_options="${cat_options}-etv" # use -t to replace tabs as it may be desired in they are in vscode which transforms tabs to spaces, so useful to know it is actually a tab
  fi
  if [ "${option_numbers}" = 'yes' ]; then
    # -b      Number the non-blank output lines, starting at 1.
    # -n      Number the output lines, starting at 1.
    cat_options="${cat_options}-n" # using -b does not count non-blank lines, use -n alone to count all lines
  fi
  __read() {
    if [ "${FILE}" = '/dev/stdin' ]; then
      # STDIN
      if [ "${option_trim}" = 'yes' ]; then
        __cat() {
          echo-trim-padding --stdin | cat "${cat_options}" - || return $?
        }
      else
        __cat() {
          cat "${cat_options}" - || return $?
        }
      fi
    else
      # FILE
      if [ "${option_trim}" = 'yes' ]; then
        __cat() {
          __wrap cat -- "${FILE}" | echo-trim-padding --stdin | cat "${cat_options}" - || return $?
        }
      else
        __cat() {
          __wrap cat "${cat_options}" -- "${FILE}" || return $?
        }
      fi
    fi
    # this doesn't handle files without trailing newline well, and it doesn't color well
    # if [ $option_header = 'yes' ]; then
    # 	__print_style --element="$FILE" || return $?
    # fi
    # __cat || return $?
    # if [ $option_header = 'yes' ]; then
    # 	__print_style --/element="$FILE" || return $?
    # fi
    if [ "${option_header}" = 'yes' ]; then
      # local content
      content="$(__cat)" || return $?
      if [ -n "${content}" ]; then
        __print_style --element="${NAME}" --newline --code="${content}" --newline --/element="${NAME}" || return $?
      else
        # self close, as no data (consistent with eval-helper)
        __print_style --element/="${NAME}" || return $?
      fi
    else
      __cat || return $?
    fi
  }
fi

# STDIN
__on_stdin() {
  FILE='/dev/stdin'
  NAME="${option_name:-"${FILE}"}"
  __read || return $?
}
