# shellcheck shell=sh disable=SC3043,2002,SC2154

___x_cmd_gh_repo_apply(){
    param:void
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then _____x_cmd_gh_help repo apply; return;
    fi

    [ -n "$1" ] || M='accepts 1 arg(apply file path), received empty' arg:ret:64
    local filepath="$1"; shift
    local _errexit="${___X_CMD_UNSEENCHAR_001}${___X_CMD_UNSEENCHAR_002}${___X_CMD_UNSEENCHAR_003}"

    local code; if code="$(
        {
            ___x_cmd_gh_repo_apply___parse_data | {
                x jo env . content=. owner_repo=.repo  apply_collaborator=.collaborator
                [ -n "$content" ]    || { gh:error "Not found apply configure data"; printf "%s\n" "$_errexit"; return 1; }
                [ -n "$owner_repo" ] || { gh:error "Not found apply configure 'repo' main key"; printf "%s\n" "$_errexit"; return 1; }

                ___x_cmd_gh_repo_apply__replace "$@"
                printf "%s\n" "$content"
                gh:info "Getting repo info ..."
                ___x_cmd_gh_repo_apply__collaborator_info
                ___x_cmd_gh_repo_apply__repo_info
            }
        } | {
                x cawk \
                -f "$___X_CMD_ROOT_MOD/awk/lib/sh.awk" \
                -f "$___X_CMD_ROOT_MOD/awk/lib/j/json.awk" \
                -f "$___X_CMD_ROOT_MOD/awk/lib/j/jiter.awk" \
                -f "$___X_CMD_ROOT_MOD/awk/lib/j/jcp.awk" \
                -f "$___X_CMD_ROOT_MOD/gh/lib/awk/repo/collaborator.awk"  \
                -f "$___X_CMD_ROOT_MOD/gh/lib/awk/repo.awk" \
                -f "$___X_CMD_ROOT_MOD/gh/lib/awk/replace.awk" \
                -f "$___X_CMD_ROOT_MOD/gh/lib/repo/apply/apply.awk"
        }
    )"; then
        gh:debug "apply code - $code"
        [ -n "$code" ] || {
            printf "%s\n" ""; gh:info "Repo apply no changes required"
            return 0
        }
        gh:info "Handling repo by info ..."
        if [ -n "$___X_CMD_GH_DRY_RUN" ]; then      printf "%s\n" "$code";
        else                                        eval "$code";
        fi
        # echo "$code"
    else
        code=$?
        gh:error "Exit=$code. Fail to apply $filepath" >/dev/stderr
    fi
}

___x_cmd_gh_repo_apply___parse_data(){
    case "$filepath" in
        -)              ___x_cmd_cmds_cat | x y2j ;;
        *.json)         ___x_cmd_cmds_cat "$filepath" ;;
        *.yml)          (
                            ___x_cmd_inner_cd "$(dirname "$filepath")" || return 1
                            printf "%s\n" "$(pwd)/$(basename "$filepath")"
                        ) | x cawk \
                        -f "$___X_CMD_ROOT_MOD/awk/lib/re.awk"  \
                        -f "$___X_CMD_ROOT_MOD/awk/lib/j/json.awk"  \
                        -f "$___X_CMD_ROOT_MOD/awk/lib/j/jiter.awk" \
                        -f "$___X_CMD_ROOT_MOD/awk/lib/j/jcp.awk" \
                        -f "$___X_CMD_ROOT_MOD/yml/lib/yutil.awk"   \
                        -f "$___X_CMD_ROOT_MOD/yml/lib/yparse.value.awk"  \
                        -f "$___X_CMD_ROOT_MOD/yml/lib/yparse.json.awk"  \
                        -f "$___X_CMD_ROOT_MOD/yml/lib/yparse.awk"  \
                        -f "$___X_CMD_ROOT_MOD/gh/lib/awk/xrc.awk";;
        *)              <"$filepath" x y2j ;;
    esac
}

# shellcheck disable=SC2154
___x_cmd_gh_repo_apply__collaborator_info(){
    [ -n "$apply_collaborator" ] || { printf "\n"; return 0; }

    NO_CACHE=1 x gh repo collaborator ls --json --repo "$owner_repo" | x ja t "\t" ; printf "\n"
    # ___x_cmd_cmds_cat "$(x wsroot)"/.x-cmd/repo_member.json | x ja t "\t" ; printf "\n"
}

___x_cmd_gh_repo_apply__repo_info(){
    NO_CACHE=1  x gh repo info --json "$owner_repo" | x ja t "\t" ; printf "\n"
    # ___x_cmd_cmds_cat "$(x wsroot)"/.x-cmd/repo_info.json | x ja t "\t" ; printf "\n"
}

___x_cmd_gh_repo_apply__replace(){
    if [ -f "$1" ]; then   ___x_cmd_cmds_cat "$1" | x y2j | x ja t "\t" ; printf "\n"
    else    local IFS=","; printf "replace:%s\n" "$*"
    fi
}