# shellcheck shell=sh disable=SC3043
# Section 7: param plugin

PARAM_SUBCMD_VAR_PREFIX=___X_CMD_X_BASH_PARAM_SUBCMD___

param_plugin(){
    local op="$1";      shift
    case "$op" in
        add)        ___x_cmd_param_plugin_define "$@"     ;;
        del)        ___x_cmd_param_plugin_del "$@"        ;;
        unset)      ___x_cmd_param_plugin_unset "$@"      ;;
        pprint)     ___x_cmd_param_plugin_pprint "$@"     ;;
        *)          printf "%s" "param_plugin: Unknown operation '$op'" ;;
    esac
}

___x_cmd_param_plugin_define() {
    # FIXME: To use no app name to add the subcmd plugin.
    local plugin_id="$___X_CMD_PARAM_PLUGIN_ID"
    local subcmd="$1"
    local desc="$2"
    [ -n "$subcmd" ] || M="Provide subcmd" N="param" log:ret:64
    [ -n "$desc" ] || M="Provide description" N="param" log:ret:64

    local varname="${PARAM_SUBCMD_VAR_PREFIX}${plugin_id}"
    local result="$subcmd $desc"
    eval "$varname=\"\${$varname}\${___X_CMD_PARAM_ARG_SEP}\${result}\""
}

# Maybe using dict?
___x_cmd_param_plugin_del() {
    # FIXME: To use no app name to add the subcmd plugin.
    local plugin_id="$___X_CMD_PARAM_PLUGIN_ID"
    local subcmd="$1"
    [ -n "$subcmd" ] || M="Provide subcmd" N="param" log:ret:64

    # TODO: Not provided
}

___x_cmd_param_plugin_unset() {
    local plugin_id="$___X_CMD_PARAM_PLUGIN_ID"
    local varname="${PARAM_SUBCMD_VAR_PREFIX}${plugin_id}"
    eval "$varname="
}

___x_cmd_param_plugin_pprint() {
    ___x_cmd_param___plugin_print "$@" | tr "$___X_CMD_PARAM_ARG_SEP" "\n"
}

### EndSection
