# shellcheck shell=dash

___x_cmd log init nano

___x_cmd_nano___main(){
    [ "$#" -gt 0 ] ||   {
        ___x_cmd_nano___exec
        return
    }

    local op="$1";      shift
    case "$op" in
        --open)         ___x_cmd_nano___open ;;
        -h|--help)      ___x_cmd help -m nano; return 0 ;;
        *)              ___x_cmd_nano___exec "$op" "$@"
    esac
}

___x_cmd_nano___exec(){
    if ! ___x_cmd_hascmd nano; then
        ___x_cmd cosmo use nano
    elif ___x_cmd os is darwin && { ___x_cmd_cmds nano -h 2>&1 | ___x_cmd_cmds grep -q Pico; } 2>/dev/null; then
        nano:info "Pico lacks some important features compared to nano. Automatically using GNU nano instead."
        ___x_cmd cosmo use nano
        command hash -r
    fi

    ___x_cmd_nano___exec(){
        ___x_cmd_cmds nano "$@"
    } && ___x_cmd_nano___exec "$@"
}

___x_cmd_nano___open(){
    local id
    local cmd
    ___x_cmd ui select id,cmd  "Next"  \
        "x nano --help"     \
        "x open https://www.nano-editor.org                            # official"              \
        "x open https://git.savannah.gnu.org/git/nano.git/             # git"                   \
        "x open https://www.nano-editor.org/docs.php                   # online docs"           \
        "x open https://www.nano-editor.org/dist/latest/nano.html      # online man for nano"   \
        "x open https://www.nano-editor.org/dist/latest/nanorc.5.html  # online man for nanorc" \
        "return 0" || return

    [ -n "$cmd" ] || return
    eval "$cmd"
}
