# shellcheck shell=sh   disable=SC3043,2154

___x_cmd_cf_worker_kv(){
    param:scope     ___x_cmd_cf
    param:subcmd    ___x_cmd_cf_worker_kv                               \
        ls              "List kvs namespaces for a account"                     \
        key             "key-value pair manage"                                 \
        rename          "Update a namespace name"                               \
        delete          "Deletes the namespace corresponding to the given ID"   \
        create          "Creates a kv that maps a URL pattern to a Worker"


    param:subcmd:try
    param:run

    ___x_cmd_cf_worker_kv  _param_help_doc
    return 1
}

# Section: kv ls
# https://developers.cf.com/api/operations/workers-kv-namespace-list-namespaces
___x_cmd_cf_worker_kv_ls(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    --json|-j       "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_cf_curl get "/accounts/${account_id}/storage/kv/namespaces"
    else
        ___x_cmd_cf_curl get "/accounts/${account_id}/storage/kv/namespaces" | {
        x jo .result | x jo 2c .id .title | x csv header --add Id Name | x csv app
        }
    fi
}
# EndSection

# Section: kv create
# https://developers.cf.com/api/operations/workers-kv-namespace-create-a-namespace
___x_cmd_cf_worker_kv_create(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    --title             "A human-readable string name for a Namespace"                  <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    local gen_cf_json=""
    gen_cf_json="$(param:option2json )"
    ___x_cmd_cf_curl post "/accounts/${account_id}/storage/kv/namespaces" gen_cf_json
}
# EndSection

# Section: kv delete
# https://developers.cf.com/api/operations/workers-kv-namespace-remove-a-namespace
___x_cmd_cf_worker_kv_delete(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "The Id of the Worker kv"                   <>:String
    --json|-j           "output in json format"
    --yes|-y            "Ignore remove prompt interception"
'
    param:run
    ___x_cmd_cf_param_default

    ___x_cmd_cf_curl delete "/accounts/${account_id}/storage/kv/namespaces/$1"
}
# EndSection

# Section: kv update
# https://developers.cf.com/api/operations/workers-kv-namespace-rename-a-namespace
___x_cmd_cf_worker_kv_rename(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "The Id of the Worker kv"                               <>:String
    --title             "A human-readable string name for a Namespace"          <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    local gen_cf_json=""
    gen_cf_json="$(param:option2json )"
    ___x_cmd_cf_curl put "/accounts/${account_id}/storage/kv/namespaces/$1" gen_cf_json
}
# EndSection

___x_cmd_cf_worker_kv_key(){
    param:scope     ___x_cmd_cf
    param:subcmd    ___x_cmd_cf_worker_kv_key       \
        ls              "Lists a namespace keys"            \
        delete          "Deletes the namespace corresponding to the given ID"

    param:subcmd:try
    param:run

    ___x_cmd_cf_worker_kv_key  _param_help_doc
    return 1
}

# Section: key ls
# https://developers.cf.com/api/operations/workers-kv-namespace-list-a-namespace'-s-keys
___x_cmd_cf_worker_kv_key_ls(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    --kv_id         "The Id of the Worker kv"                                           <>:String
    --limit         "The number of keys to return"                                      <>:Number="1000"
    --prefix        "A string prefix used to filter down which keys will be returned"   <>=""
    --cursor        "Opaque token indicating the position from which to continue when requesting the next set of records if the amount of list results was limited by the limit parameter"    <>=""
    --json|-j       "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    if [ -n "$json" ] || [ -n "$ENFORCE_JSON" ]; then
        ___x_cmd_cf_curl get "/accounts/${account_id}/storage/kv/namespaces/${kv_id}/keys"
    else
        ___x_cmd_cf_curl get "/accounts/${account_id}/storage/kv/namespaces/${kv_id}/keys" | {
        x jo .result | x jo 2c .name | x csv header --add Name | x csv static_tab
        }
    fi
}
# EndSection

# Section: key delete
# https://developers.cf.com/api/operations/workers-kv-namespace-delete-key-value-pair
___x_cmd_cf_worker_kv_key_delete(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "The name of the key to delete"             <>:String
    --kv_id             "The Id of the Worker kv"                   <>:String
    --yes|-y            "Ignore remove prompt interception"
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    ___x_cmd_cf_curl delete "/accounts/${account_id}/storage/kv/namespaces/${kv_id}/values/$1"
}
# EndSection