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

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

___x_cmd_gt_avatar(){
    param:scope  ___x_cmd_gt
    param:subcmd ___x_cmd_gt_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_gt_avatar _param_help_doc
    return 1
}

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

# Section: Put
___x_cmd_gt_avatar_add(){
    param:scope     ___x_cmd_gt
    param:dsl       '
options:
    #1    "address book key"      <>
    #2    "address book value"    <>
'
    param:run

    [ "$#" -ne 0 ] || M='accepts 2 arg(s) (x gt 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_gt_avatar___user_addressbook  "$1" "$2"
        shift 2
    done
}
# EndSection

# Section: Get
___x_cmd_gt_avatar_get(){
    param:scope     ___x_cmd_gt
    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_gt_avatar___user_addressbook  "$1"
}
# EndSection

# Section: Transform Username
___x_cmd_gt____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_gt_avatar_get "${_avt_name}"); then
        _avt_name="$_tmp_avt_name"
    fi
    return 0
}
# EndSection