let _omp_executable: string = (echo ::OMP::)

let _omp_executable_is_path = (
    ($_omp_executable | str contains "/")
    or ($_omp_executable | str contains "\\")
    or ($_omp_executable | str starts-with ".")
    or ($_omp_executable | str starts-with "~")
)

if not (($_omp_executable_is_path and ($_omp_executable | path exists)) or (which $_omp_executable | is-not-empty)) { return }

if ($env.config? | is-not-empty) {
    $env.config = ($env.config | upsert render_right_prompt_on_last_line true)
}

$env.POWERLINE_COMMAND = 'oh-my-posh'
$env.PROMPT_INDICATOR = ""
$env.POSH_SESSION_ID = "::SESSION_ID::"
# pinned to the session's configuration so it can be recovered when the session cache is lost
$env.POSH_CONFIG = ::CONFIG::
$env.POSH_SHELL = "nu"
$env.POSH_SHELL_VERSION = (version | get version)

$env.VIRTUAL_ENV_DISABLE_PROMPT = 1
$env.PYENV_VIRTUALENV_DISABLE_PROMPT = 1

def --wrapped _omp_get_prompt [
    type: string,
    ...args: string
] {
    # We have to do this because the initial value of `$env.CMD_DURATION_MS` is always `0823`, which is an official setting.
    # See https://github.com/nushell/nushell/discussions/6402#discussioncomment-3466687.
    let execution_time = match $env.CMD_DURATION_MS {
        '0823' => -1
        $ms => { $ms | into int }
    }

    # `$env.POSH_EXECUTED` is set once per prompt cycle in `$env.PROMPT_COMMAND`, based on
    # whether history actually grew. Falls back to the execution-time sentinel when history
    # is disabled, which only detects a freshly started shell.
    let no_status = if $nu.history-enabled {
        not ($env.POSH_EXECUTED? | default false)
    } else {
        $execution_time < 0
    }

    (
        ^$_omp_executable print $type
            --save-cache
            --shell=nu
            $"--shell-version=($env.POSH_SHELL_VERSION)"
            $"--status=($env.LAST_EXIT_CODE)"
            $"--no-status=($no_status)"
            $"--execution-time=($execution_time)"
            $"--terminal-width=((term size).columns)"
            $"--job-count=(job list | length)"
            ...$args
    )
}

$env.PROMPT_MULTILINE_INDICATOR = (
    ^$_omp_executable print secondary
        --shell=nu
        $"--shell-version=($env.POSH_SHELL_VERSION)"
)

$env.PROMPT_COMMAND = {||
    let hist = if $nu.history-enabled { history } else { [] }
    let hist_len = ($hist | length)

    # hack: sets cursor line to 1 on clear; not bulletproof, just a start
    let clear = $nu.history-enabled and (
        ($hist | is-empty)
        or ($hist | last | get command?) == "clear"
    )

    if ($env.SET_POSHCONTEXT? | is-not-empty) {
        do --env $env.SET_POSHCONTEXT
    }

    # a command was executed this prompt cycle only if history actually grew;
    # an empty Enter (or history disabled) leaves the length unchanged
    $env.POSH_EXECUTED = ($nu.history-enabled and ($hist_len > ($env.POSH_LAST_HISTORY_LEN? | default 0)))
    $env.POSH_LAST_HISTORY_LEN = $hist_len

    _omp_get_prompt primary $"--cleared=($clear)"
}

$env.PROMPT_COMMAND_RIGHT = {|| _omp_get_prompt right }
