# shellcheck shell=dash
# shellcheck disable=SC2039,3043

# x gh avatar add root x-cmd-root
# x gh repo collaborator add -r x-cmd/x-cmd root

___x_cmd_gh_avatar(){
    param:scope  ___x_cmd_github
    param:subcmd ___x_cmd_gh_avatar                                             \
        "ls"           "List all address books for the current session record"  \
        "add|put"      "Add or Edit the key and value in the address book"      \
        get            "Get the value of the address book record by key"
    param:subcmd:try
    param:run

    ___x_cmd_gh_avatar _param_help_doc
    return 1
}

# Section: List
# shellcheck disable=SC2016
___x_cmd_gh_avatar_ls(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    --raw    "output format as key=value"
'
    param:run
    if [ -n "$raw" ]; then
        x sdict print ___x_cmd_gh_avatar___user_addressbook
    else
        x sdict print ___x_cmd_gh_avatar___user_addressbook | ___x_cmd_gx_avatar_sdict_to_csv | x csv static_tab
    fi
}
# EndSection

# Section: Add
___x_cmd_gh_avatar_add(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1    "address book key"      <>
    #2    "address book value"    <>
'
    param:run

    [ "$#" -ne 0 ] || M='accepts 2 arg(s) (x gh avatar add|put <key> <value>), received empty' arg:ret:64
    while [ $# -ne 0 ]; do
        [ -n "$1" ] || M='accepts the frist arg (user avatar key), received empty' arg:ret:64
        [ -n "$2" ] || M="The frist arg is $1. accepts the second arg (user avatar value), But received empty" arg:ret:64
        x sdict put ___x_cmd_gh_avatar___user_addressbook  "$1" "$2"
        shift 2
    done
}
# EndSection

# Section: Get
___x_cmd_gh_avatar_get(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1    "address book key"      <>
'
    param:run

    [ -n "$1" ] || M='accepts arg (user avatar key), received empty' arg:ret:64
    x sdict get ___x_cmd_gh_avatar___user_addressbook  "$1"
}
# EndSection

# Section: Transform Username
# gh:error "Can't get avatar value by \"${_avt_name#*/}\" key. Please use \`x gh avatar ls\` to check the avatar key and value is exist."
___x_cmd_gh____transform_avt_name(){
    [ -n "$_avt_name" ] || M='accepts _avt_name var (username), received empty' arg:ret:64
    local _tmp_avt_name=""
    if _tmp_avt_name=$(___x_cmd_gh_avatar_get "${_avt_name}"); then
        _avt_name="$_tmp_avt_name"
    fi
    return 0
}
# EndSection

___x_cmd_gh_avatar____full_search_asset_id(){
    local release_id

    if [ -n "$tag" ]; then
        release_id="$(___x_cmd_gh_repo_release_ls -r "$repo" | ___x_cmd_cmds awk -v tag="$tag" '$3==tag{ print $1}')"
    else
        release_id="$(___x_cmd_gh_repo_release_latest -r "$repo" --json | ___x_cmd jo .id)"
    fi

    [ -n "$release_id" ] || return 1

    NO_CACHE=1 ___x_cmd_gh_repo_release_attachment_ls -r "$repo" --release_id "$release_id"  2>/dev/null \
      | ___x_cmd_cmds awk -v asset="$asset" '$2==asset{ print $1 }'
}
