# shellcheck shell=dash     disable=SC2086,SC2089,SC2090,SC3003

xrc:mod:lib git             \
    origin/_index           \
    quick/_index            \
    ext/_index              \
    submodule/_index        \
    subtree/_index          \
    x/_index                \
    part/_index             \
    tree/_index             \
    sp/_index               \
    meta/_index


if ___x_cmd_is_suitable_advise_env; then
    xrc advise
    ___x_cmd_advise __load_xcmd_advise_util_file git
fi

___x_cmd_git___main(){
    local ___X_CMD_GIT_MAIN_OPTS=''; while true; do
        case "$1" in
            --version|--help|--man-path|--info-path)
                ___x_cmd___git_origin "$1"
                return
                ;;
            -p|--paginate|-P|--no-pager|\
            --no-replace-objects|--bare|\
            --git-dir=*|--work-tree=*|--namespace=*|--exec-path=*)
                x cmdstr ___X_CMD_GIT_MAIN_OPTS "$1";  shift
                ;;
            -C|-c)
                x cmdstr ___X_CMD_GIT_MAIN_OPTS "$1" "$2"; shift 2
                ;;
            -h) ___x_cmd_git___help || return
                ;;
            *)  break;;
        esac
    done

    local op="$1";  [ -z "$op" ] || shift
    # TODO: using workspace facility
    case "$op" in
        "")                 ___x_cmd_git___help || return;;

        x)                  ___x_cmd_git_x          "$@" ;;
        b|branch)           ___x_cmd_git_branch     "$@" ;;
        cfg|config)         ___x_cmd_git_config     "$@" ;;

        c|collaborator)     ___x_cmd_git_collaborator     "$@" ;;
        i|issue)            ___x_cmd_git_issue      "$@" ;;
        pr)                 ___x_cmd_git_pr         "$@" ;;
        info)               ___x_cmd_git_info       "$@" ;;

        p|pull)             ___x_cmd_git_pull       "$@" ;;
        P|push)             ___x_cmd_git_push       "$@" ;;
        pp|pupu)            ___x_cmd_git_pupu       "$@" ;;

        part)               ___x_cmd_git_part       "$@" ;;
        tr|tree)            ___x_cmd_git_tree       "$@" ;;

        log)                ___x_cmd_git_log        "$@" ;;
        si|submit)          ___x_cmd_git_submit     "$@" ;;
        ci|commit)          ___x_cmd_git_commit     "$@" ;;
        co|checkout)        ___x_cmd_git_checkout   "$@" ;;

        cl|clone)           ___x_cmd_git_clone      "$@" ;;
        a)                  ___x_cmd_git___a        "$@" ;;
        ac)                 ___x_cmd_git___ac       "$@" ;;
        acpp)               ___x_cmd_git___acpp     "$@" ;;
        d|diff)             ___x_cmd_git___diff     "$@" ;;

        ss)                 ___x_cmd_git___ss       "$@" ;;
        s|status)           ___x_cmd_git___s        "$@" ;;

        sm|submodule)       ___x_cmd_git_submodule  "$@" ;;
        st|subtree)         ___x_cmd_git_subtree    "$@" ;;

        sp)                 ___x_cmd_git_sp         "$@" ;;

        pullr)              ___x_cmd_git_pullr      "$@" ;;
        pushr)              ___x_cmd_git_pushr      "$@" ;;

        sy|sync)            ___x_cmd_git_sync       "$@" ;;

        newtree)            ___x_cmd_git___newtree  "$@" ;;

        last|unadd|\
        is|meta|\
        rootout|track|update|backup|all|lz)
                            ___x_cmd_git_"${op}"        "$@" ;;

        *)                  ___x_cmd___git_origin "$op" "$@" ;;
    esac
}


# TODO: consider using the x pixi exec git ...
___x_cmd_git___cmd(){
    if ___x_cmd hascmd git; then
        ___x_cmd_git___cmd(){
            ___x_cmd_cmds git "$@"
        }
    else
        ___x_cmd_git___cmd(){
            ___x_cmd jj git "$@"
        }
    fi

    ___x_cmd_git___cmd "$@"
}

___x_cmd___git_origin(){
    eval set -- ___x_cmd_git___cmd $___X_CMD_GIT_MAIN_OPTS "\"\$@\""
    "$@"
}

# shellcheck disable=2120
___x_cmd_git___help(){
    x help -m git "$@" 1>&2
    return 1
}

___x_cmd_git___a(){
    if [ $# -eq 0 ]; then
        ___x_cmd___git_origin add .
    else
        ___x_cmd___git_origin add "$@"
    fi
}

___x_cmd_git___ac(){
    ___x_cmd_git___a . && ___x_cmd___git_origin commit -m "${*:-...}"
}

___x_cmd_git___acpp(){
    ___x_cmd_git___ac "$@"
    x git pull && x git push
}

___x_cmd_git___acs(){
    ___x_cmd_git___ac "$@" && ___x_cmd_git_sync
}

___x_cmd_git___s(){
    ___x_cmd___git_origin status "$@"
}

___x_cmd_git___ss(){
    ___x_cmd___git_origin status -s "$@"
}

___x_cmd_git___diff(){
    ___x_cmd___git_origin diff "$@"
}

___x_cmd_git___newtree(){
    (
        # TODO: cd into .git containing folder
        local name="$1"
        ___x_cmd___git_origin checkout --orphan "$name"
        ___x_cmd___git_origin rm -rf .
        ___x_cmd___git_origin commit --allow-empty -m "x git newtree $name"
    )
}


___x_cmd_git_lz(){
    ___x_cmd lazygit "$@"
}
