# shellcheck    shell=sh disable=SC3043

# Section 4: param default management
# param_default put <scope> <key> <value>
# eg. param_default put gitee/$O repo x-bash
# eg. param_default put gitee/$O user edwinjhlee
# eg. param_default put gitee/$O access public

xrc dict

PARAM_DEFAULT_VAR_PREFIX=___X_CMD_X_BASH_PARAM_DEFAULT___

param_default() {
    local op="$1"
    local app_name=${2:?Provide Object}
    shift 2

    local object_name=${app_name#*/}
    if [ "$object_name" = "$app_name" ]; then
        object_name=
    fi
    app_name=${app_name%%/*}

    local O="${PARAM_DEFAULT_VAR_PREFIX}${app_name}"

    case "$op" in # yml seemed to be a better
        load)
                    local json_path="${1:?Provide path in function param_default}"
                        ___x_cmd_dict @"${O}" load_json "$(___x_cmd_cmds_cat "$json_path")"
                    ;;
        dump)       if [ -n "$object_name" ]; then
                        ___x_cmd_dict @"${O}" scope "${object_name}" | ___x_cmd_dict_pjson
                    else
                        ___x_cmd_dict @"${O}" json
                    fi ;;
        dump_raw)   if [ -n "$object_name" ]; then
                        ___x_cmd_dict @"${O}" scope "${object_name}" | ___x_cmd_dict @"${O}" dump
                    else
                        ___x_cmd_dict @"${O}" dump
                    fi ;;
        clear)      ___x_cmd_dict @"${O}" free ;;
        get)        ___x_cmd_dict @"${O}" get ${object_name:+"${object_name}"} "${1:?Provide [key] name in function 'param_default get'}" ;;
        put | set )
                    ___x_cmd_dict @"${O}" put ${object_name:+"${object_name}"} "${1:?Provide key name in function param_default}" "${2:?Provide [value] in function 'param_default put'}" ;;
        remove)
                    if [ -n "$object_name" ]; then
                        ___x_cmd_dict @"${O}" remove "${object_name:+"${object_name}"}$___X_CMD_PARAM_ARG_SEP${1:?Provide key name in function param_default}"
                    else
                        ___x_cmd_dict @"${O}" remove "${1:?Provide key name in function param_default}"
                    fi ;;
        export)     export "${O?}" ;;
        *)
        ;;
    esac
}




