{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}

{{ section }}
# Utility functions for zoxide.
#

# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd
{%- if cfg!(windows) %}
    command cygpath -w (builtin pwd -P)
{%- else if resolve_symlinks %}
    builtin pwd -P
{%- else %}
    builtin pwd -L
{%- endif %}
end

# A copy of fish's internal cd function. This makes it possible to use
# `alias cd=z` without causing an infinite loop.
if ! builtin functions -q __zoxide_cd_internal
    if builtin functions -q cd
        builtin functions -c cd __zoxide_cd_internal
    else
        alias __zoxide_cd_internal='builtin cd'
    end
end

# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd
{%- if cfg!(windows) %}
    __zoxide_cd_internal (cygpath -u $argv)
{%- else %}
    __zoxide_cd_internal $argv
{%- endif %}
{%- if echo %}
    and __zoxide_pwd
{%- endif %}
end

{{ section }}
# Hook configuration for zoxide.
#

{% if hook == InitHook::None -%}
{{ not_configured }}

{%- else -%}
# Initialize hook to add new entries to the database.
{%- if hook == InitHook::Prompt %}
function __zoxide_hook --on-event fish_prompt
{%- else if hook == InitHook::Pwd %}
function __zoxide_hook --on-variable PWD
{%- endif %}
    test -z "$fish_private_mode"
    and command zoxide add -- (__zoxide_pwd)
end

{%- endif %}

{{ section }}
# When using zoxide with --no-cmd, alias these internal functions as desired.
#

set __zoxide_z_prefix 'z!'

# Jump to a directory using only keywords.
function __zoxide_z
    set -l argc (count $argv)
    set -l completion_regex '^'(string escape --style=regex $__zoxide_z_prefix)'(.*)$'

    if test $argc -eq 0
        __zoxide_cd $HOME
    else if test "$argv" = -
        __zoxide_cd -
    else if test $argc -eq 1 -a -d $argv[1]
        __zoxide_cd $argv[1]
    else if set -l result (string match --groups-only --regex $completion_regex $argv[-1])
        __zoxide_cd $result
    else
        set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
        and __zoxide_cd $result
    end
end

# Completions for `z`.
function __zoxide_z_complete
    set -l tokens (commandline --current-process --tokenize)
    set -l curr_tokens (commandline --cut-at-cursor --current-process --tokenize)

    if test (count $tokens) -le 2 -a (count $curr_tokens) -eq 1
        # If there are < 2 arguments, use `cd` completions.
        __fish_complete_directories "$tokens[2]" ''
    else if test (count $tokens) -eq (count $curr_tokens)
        # If the last argument is empty, use interactive selection.
        set -l query $tokens[2..-1]
        set -l result (zoxide query --exclude (__zoxide_pwd) -i -- $query)
        and echo $__zoxide_z_prefix$result
        commandline --function repaint
    end
end

# Jump to a directory using interactive search.
function __zoxide_zi
    set -l result (command zoxide query -i -- $argv)
    and __zoxide_cd $result
end

{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#

{%- match cmd %}
{%- when Some with (cmd) %}

abbr --erase {{cmd}} &>/dev/null
complete --command {{cmd}} --erase
function {{cmd}}
    __zoxide_z $argv
end
complete --command {{cmd}} --no-files --arguments '(__zoxide_z_complete)'

abbr --erase {{cmd}}i &>/dev/null
complete --command {{cmd}}i --erase
function {{cmd}}i
    __zoxide_zi $argv
end

{%- when None %}

{{ not_configured }}

{%- endmatch %}

{{ section }}
# To initialize zoxide, add this to your configuration (usually
# ~/.config/fish/config.fish):
#
#   zoxide init fish | source
#
# Note: zoxide only supports fish v3.4.0 and above.
