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

# Code generated by zoxide. DO NOT EDIT.

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

# Default prompt for Nushell.
def __zoxide_prompt [] {
    let git = $'(do -i {git rev-parse --abbrev-ref HEAD} | str trim -rc (char newline))'
    let git = (if ($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 %}
{{ not_configured }}

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

# Initialize hook.
let-env PROMPT_STRING = (
    let prompt = (if ($nu.env | select PROMPT_STRING | empty?) {
        if ($nu.config | select prompt | empty?) { '__zoxide_prompt' } { $nu.config.prompt }
    } { $nu.env.PROMPT_STRING });
    if ($prompt | str contains '__zoxide_hook') { $prompt } { $'__zoxide_hook;($prompt)' }
)

{%- when InitHook::Pwd %}
$'zoxide: PWD hooks are not supported on Nushell.(char nl)Use (char sq)zoxide init nushell --hook prompt(char sq) 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 ($rest | length) > 1 {
            $'zoxide: can only jump directories on filesystem(char nl)'
        } {
            cd $rest
{%- if echo %}
            pwd
{%- endif %}
        }
    } {
        let arg0 = ($rest | append '~' | first 1);
        if ($rest | length) <= 1 && ($arg0 == '-' || ($arg0 | path expand | path exists)) {
            cd $arg0
        } {
            cd $'(zoxide query --exclude (pwd) -- $rest | str trim -rc (char newline))'
        }
{%- 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 -rc (char newline))'
{%- 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, add this to your configuration (find it by running
# `config path` in Nushell):
#
#   startup = ['zoxide init nushell --hook prompt | save ~/.zoxide.nu', 'source ~/.zoxide.nu']
#
# Note: zoxide only supports Nushell v0.36.0 and above.
