
# Section: print pprint grep filter

___x_cmd_dict___pgrep(){
    AWK_CODE="( keyline ~ key )" ___x_cmd_dict___premove "$@"
}

___x_cmd_dict___pdrop() {
    AWK_CODE="! (keyline ~ key)" ___x_cmd_dict___premove "$@"
}

# drop_by_idx, drop_by_regex, drop_by_glob
___x_cmd_dict___premove() {
    [ $# -eq 0 ] && printf "%s\n" "Accept more than one argument." >&2

    local IFS="${DICT_KEYS_SEP}"
    local AWK_CODE
    AWK_CODE=${AWK_CODE:-"keyline != key"}
    {
        printf "%s${DICT_SEP}" "$*"
        ___x_cmd_cmds_cat
    } | ___x_cmd_cmds_awk -v RS="${DICT_SEP}" '
        NR==1 { key = $0;   ORS=RS;   len=0   }
        NR>=2 {
            if (NR % 2 == 0) {  # key
                keyline = $0
            } else {
                if ('"$AWK_CODE"')  print keyline ORS $0
                else                len = len - 1
            }
        }
        END   {
            if (len < 0) {
                printf("%s", (keyline + len))
                exit 0
            } else {
                printf("%s", keyline)
                exit 1
            }
        }
    ' -
}

___x_cmd_dict___pput(){
    {
        local s
        local IFS="${DICT_KEYS_SEP}"
        s="$*"
        printf "%s${DICT_SEP}%s${DICT_SEP}" "${s%${DICT_KEYS_SEP}*}" "${s##*${DICT_KEYS_SEP}}";
        ___x_cmd_cmds_cat
    } | ___x_cmd_cmds_awk -v RS="${DICT_SEP}" '

        BEGIN { sw = 0; ORS = RS;   }
        NR==1 { key = $0            }
        NR==2 { val = $0            }
        NR>=3 {
            if (NR % 2 == 1) { # key
                keyline = $0
            } else {
                if (keyline == key) {
                    print keyline RS val
                    valline = ""
                    sw = 1
                } else {
                    print keyline RS $0
                }
            }
        }
        END   {
            if (sw == 1)    printf("%s", keyline)
            else {
                print key RS val
                printf("%s", (keyline+1))
            }
        }
    ' -
}

___x_cmd_dict___pget(){
    local IFS="${DICT_KEYS_SEP}"
    {
        printf "%s${DICT_SEP}" "$*"
        ___x_cmd_cmds_cat
    } | ___x_cmd_cmds_awk -v RS="${DICT_SEP}" '
        NR==1 {
            tgt = $0;   tgt_len = length(tgt);  exit_code = 1
        }
        NR>=2 {
            if (NR % 2 == 0) {  # key
                keyline = $0
            } else {
                if (keyline == tgt) {
                    print $0
                    exit_code = 0
                    exit
                }
            }
        }
        END {
            exit exit_code
        }
    ' -
}

___x_cmd_dict___pfilter(){
    local s
    local AWK_CODE=""

    case $# in
        0)  exit 4;;
        1)
            AWK_CODE="keyline == tgt"
            s="$1" ;;
        *)
            local IFS="${DICT_KEYS_SEP}"
            s="$*";
            AWK_CODE="substr(keyline, 1, tgt_len) == tgt" ;;
    esac

    {
        printf "%s${DICT_SEP}" "$s"
        ___x_cmd_cmds_cat
    } | ___x_cmd_cmds_awk -v RS="${DICT_SEP}" '
        NR==1 {
            tgt = $0;   tgt_len = length(tgt);  exit_code = 1
        }
        NR>=2 {
            if (NR % 2 == 0) {  # key
                keyline = $0
            } else {
                if ('"${AWK_CODE}"') {
                    print $0
                    exit_code = 0
                    exit
                }
            }
        }
        END {
            exit exit_code
        }
    ' -
}

# ___x_cmd_dict_scope <key1> <key2> <key3>
___x_cmd_dict___pscope(){
    {
        local IFS="${DICT_KEYS_SEP}"
        printf "%s${DICT_KEYS_SEP}${DICT_SEP}" "$*"
        ___x_cmd_cmds_cat
    } | ___x_cmd_cmds_awk -v RS="${DICT_SEP}" '
        NR==1 {
            key = $0;   ORS=RS;   len=0
            key_len = length(key)
        }
        NR>=2 {
            if (NR % 2 == 0) {  # key
                keyline = $0
            } else {
                if (substr(keyline, 1, key_len) == key) {
                    print substr(keyline, key_len+1) ORS $0
                    len = len + 1
                }
            }
        }
        END {
            printf("%s", len)
        }
    ' -
}

_dict_map_awk(){
    # TODO: modify_key(key, value)
    # TODO: modify_value(key, value)
    awk -v RS="${DICT_SEP}" "$1""


    " -
}
# EndSection


# ___x_cmd_dict_dump
___x_cmd_dict_pjson(){

    ___x_cmd_cmds_cat | ___x_cmd_cmds_awk -v RS="${DICT_SEP}" '
        BEGIN { len = $0;   result = "{";     quote = "\002";}
        NR>=1 {
            if (NR % 2 == 1) { # key
                key = $0
            } else {
                if (NR == 2)    result = result "\n  "  quote key quote ": " quote $0 quote
                else            result = result ",\n  " quote key quote ": " quote $0 quote
            }
        }
        END   {
            result = result "\n}"

            # gsub("\001", "\\n", result)
            gsub("\"", "\\\"", result)
            gsub("\002", "\"", result)
            gsub("\v", "\\v", result)
            gsub("\b", "\\b", result)
            gsub("\t", "\\t", result)
            gsub("\r", "\\r", result)
            printf("%s", result)
        }
    ' -
}

___x_cmd_dict_pjsonparse(){
    awk -v op=flat-leaf -f "$___X_CMD_ROOT_MOD/json/lib/v0_walk.awk" \
        | awk 'BEGIN{ORS="\003"} { start=index($0, "\"")
          tmp=index($0, "--->")
          print substr($0,start+1,tmp-start-3) "\003" substr($0,tmp+start+3,length($0)-tmp-6)
        } END{ print NR }'
}



___x_cmd_dict_pprint(){
    {
        printf "${ITEM_SEP:-"\\n"}${DICT_SEP}${KV_SEP:-"="}${DICT_SEP}"
        ___x_cmd_cmds_cat
    } | ___x_cmd_cmds_awk -v RS="${DICT_SEP}" '
            NR==1 {     ORS = $0            }
            NR==2 {     KV_SEP  = $0        }
            NR>=3 {
                if (NR % 2 == 1)    key = $0
                else                print  key KV_SEP $0
            }
        ' -
}

