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

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

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

    local code=""; if code="$(
        {
            case "$filepath" in
                -)              ___x_cmd_cmds_cat | x y2j ;;
                *.json)         ___x_cmd_cmds_cat "$filepath" ;;
                *)              <"$filepath" x y2j ;;
            esac | {
                x jo env . content=. org=.org  membership_admin=.membership.admin membership_member=.membership.member
                [ -n "$org" ] || { gh:error "Not found apply configure 'org' main key" >/dev/stderr; return 1; }

                ___x_cmd_gh_org_apply__replace_args "$@"
                printf "%s\n" "$content"
                gh:info "Getting org info ..."
                ___x_cmd_gh_org_apply__org_info
                ___x_cmd_gh_org_apply__membership_admin_info
                ___x_cmd_gh_org_apply__membership_member_info
            }
        } | {
            ___x_cmd_cmds_awk -f "$___X_CMD_ROOT_MOD/awk/lib/core.awk" \
                -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/gh/lib/awk/org/membership.awk" \
                -f "$___X_CMD_ROOT_MOD/gh/lib/awk/org.awk" \
                -f "$___X_CMD_ROOT_MOD/gh/lib/awk/replace.awk" \
                -f "$___X_CMD_ROOT_MOD/gh/lib/org/apply.awk"
        }
    )"; then
        gh:debug "apply code - $code"
        [ -n "$code" ] || {
            printf "%s\n" ""; gh:info "Org apply no changes required"
            return 0
        }
        gh:info "Handling Org 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"
    fi
}

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

    NO_CACHE=1 x gh org membership ls --json --org "$org" --role admin | x ja t "\t" ; printf "\n"
    # cat "$(x wsroot)"/.x-cmd/org-member-admin.json | x ja t "\t" ; printf "\n"
}

___x_cmd_gh_org_apply__membership_member_info(){
    [ -n "$membership_member" ] || { printf "\n"; return 0; }

    NO_CACHE=1 x gh org membership ls --json --org "$org" --role member | x ja t "\t" ; printf "\n"
    # cat "$(x wsroot)"/.x-cmd/org-member-member.json | x ja t "\t" ; printf "\n"
}

___x_cmd_gh_org_apply__org_info(){
    NO_CACHE=1  x gh org info --json "$org" | x ja t "\t" ; printf "\n"
    # cat "$(x wsroot)"/.x-cmd/org-info.json | x ja t "\t" ; printf "\n"
}

___x_cmd_gh_org_apply__replace_args(){
    if [ $# -gt 0 ]; then
        local IFS=","; printf "replace:%s\n" "$*"
        return
    fi
    # cat "$(x wsroot)"/.x-cmd/lteam-member.yml | x y2j | x ja t "\t" ; printf "\n"
}
