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

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

# Default prompt for Nushell.
def __zoxide_prompt [] {
    let git = $"(do -i {git rev-parse --abbrev-ref HEAD} | str trim)"
    let git = (if (echo $git | str length) == 0 {
        ""
    } {
        build-string (char lparen) (ansi cb) $git (ansi reset) (char rparen)
    })
    build-string (ansi gb) (pwd) (ansi reset) $git "> "
}

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

# Hook to add new entries to the database.
{%- match hook %}
{%- when InitHook::None %}
def __zoxide_hook [] {}

{%- when InitHook::Prompt %}
def __zoxide_hook [] {
    shells | where active == $true && name == filesystem | get path | each { zoxide add -- $it }
}

{%- when InitHook::Pwd %}
def __zoxide_hook [] {}
$"zoxide: PWD hooks are not supported on Nushell.(char nl)Use 'zoxide init nushell --hook prompt' instead.(char nl)"
{%- endmatch %}

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

# Jump to a directory using only keywords.
def __zoxide_z [...rest:string] {
    if (shells | where active == $true | get name) != filesystem {
        if (echo $rest | length) > 1 {
            $"zoxide: can only jump directories on filesystem(char nl)"
        } {
            cd (echo $rest)
{%- if echo %}
            pwd
{%- endif %}
        }
    } {
        let arg0 = (echo $rest | append '~' | first 1);
        if (echo $rest | length) <= 1 && ($arg0 == '-' || (echo $arg0 | path expand | path exists)) {
            cd $arg0
        } {
            cd (zoxide query --exclude (pwd) -- $rest | str trim)
        }
{%- if echo %}
        pwd
{%- endif %}
    }
}

# Jump to a directory using interactive search.
def __zoxide_zi  [...rest:string] {
    if (shells | where active == $true | get name) != filesystem {
        $"zoxide: can only jump directories on filesystem(char nl)"
    } {
        cd (zoxide query -i -- $rest | str trim)
{%- if echo %}
        pwd
{%- endif %}
    }
}

{{ section }}
# Convenient aliases for zoxide. Disable these using --no-aliases.
#

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

alias {{cmd}} = __zoxide_z
alias {{cmd}}i = __zoxide_zi

{%- when None %}

{{ not_configured }}

{%- endmatch %}

{{ section }}
# To initialize zoxide, first create a Nushell script:
#
#   zoxide init nushell --hook prompt | save ~/.zoxide.nu
#
# Add this to your configuration (usually ~/.config/nu/config.toml):
#
#   prompt = "__zoxide_hook;__zoxide_prompt"
#   startup = ["zoxide init nushell --hook prompt | save ~/.zoxide.nu", "source ~/.zoxide.nu"]
#
# You can replace __zoxide_prompt with a custom prompt.
