{%- 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 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
    __zoxide_cd_internal $argv
{%- 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-aliases, alias these internal functions as
# desired.
#

# Jump to a directory using only keywords.
function __zoxide_z
    set argc (count $argv)
    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
        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 trigger '**'
    set -l trigger_length (string length $trigger)

    set -l line (commandline -op)
    set -l interactive 0
    if test (string sub -s "-$trigger_length" $line[-1]) = $trigger
        set line[-1] (string sub -e "-$trigger_length" $line[-1])
        set interactive 1
    end

    set -l query $line[2..-1]
    if test $interactive -eq 0 -a (count (commandline -cop)) -le 1
        __fish_complete_directories "$query" ''
        return
    end
    set -l result (_ZO_FZF_OPTS='{{ crate::shell::FZF_COMPLETE_OPTS }}' zoxide query -i -- $query)
    set -l exit $status
    if test $exit -ne 0
        test $exit -eq 130
        and commandline -p "$line"
        return
    end

    set -l cmd $line[1]
    commandline -p "$cmd "(string escape $result)
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 }}
# Convenient aliases for zoxide. Disable these using --no-aliases.
#

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

# Remove definitions.
function __zoxide_unset
    set --erase $argv >/dev/null 2>&1
    abbr --erase $argv >/dev/null 2>&1
    builtin functions --erase $argv >/dev/null 2>&1
end

__zoxide_unset {{cmd}}
alias {{cmd}}=__zoxide_z
complete -e {{cmd}}
complete -c {{cmd}} -f -a '(__zoxide_z_complete)'

__zoxide_unset {{cmd}}i
alias {{cmd}}i=__zoxide_zi

{%- when None %}

{{ not_configured }}

{%- endmatch %}

{{ section }}
# To initialize zoxide, add this to your configuration (usually
# ~/.config/fish/config.fish):
#
# zoxide init fish | source
