# shellcheck shell=sh   disable=SC3043,2154

___x_cmd_cf_worker_route(){
    param:scope     ___x_cmd_cf
    param:subcmd    ___x_cmd_cf_worker_route        \
        ls              "List routes for a zone"            \
        info            "Get worker route information"      \
        update          "Update route"                      \
        delete          "Delete route"                      \
        create          "Creates a route that maps a URL pattern to a Worker"


    param:subcmd:try
    param:run

    ___x_cmd_cf_worker_route  _param_help_doc
    return 1
}

# Section: route ls
# https://developers.cf.com/api/operations/worker-routes-list-routes
___x_cmd_cf_worker_route_ls(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    --zone_id       "The id of the zone,If not provided, the default zone id is used"       <>=""
    --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 "/zones/${zone_id}/workers/routes"
    else
        ___x_cmd_cf_curl get "/zones/${zone_id}/workers/routes" | {
        x jo .result | x jo 2c .id .pattern .script | x csv header --add Id Pattern Worker | x csv static_tab
        }
    fi
}
# EndSection

# Section: route info
# https://developers.cf.com/api/operations/worker-routes-get-route
___x_cmd_cf_worker_route_info(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                 "The Id of the Worker route"                                             <>:String
    --zone_id          "The id of the zone,If not provided, the default zone id is used"        <>=""
    --json|-j          "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    ___x_cmd_cf_curl get "/zones/${zone_id}/workers/routes/$1"
}
# EndSection

# Section: route create
# https://developers.cf.com/api/operations/worker-routes-create-route
___x_cmd_cf_worker_route_create(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    --zone_id           "The id of the zone,If not provided, the default zone id is used"        <>=""
    --script            "Name of the script(worker)"                                             <>:String
    --pattern           "Worker service associated with the zone and hostname"                   <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    local gen_cf_json=""
    gen_cf_json="$(param:option2json -zone_id)"
    ___x_cmd_cf_curl post "/zones/${zone_id}/workers/routes" gen_cf_json
}
# EndSection

# Section: route delete
# https://developers.cf.com/api/operations/worker-routes-delete-route
___x_cmd_cf_worker_route_delete(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1              "The Id of the Worker route"                                            <>:String
    --zone_id       "The id of the zone,If not provided, the default zone id is used"       <>=""
    --json|-j       "output in json format"
    --yes|-y        "Ignore remove prompt interception"
'
    param:run
    ___x_cmd_cf_param_default
    
    ___x_cmd_cf_curl delete "/zones/${zone_id}/workers/routes/$1"
}
# EndSection

# Section: route update
# https://developers.cf.com/api/operations/worker-routes-update-route
___x_cmd_cf_worker_route_update(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "The Id of the Worker route"                                            <>:String
    --zone_id           "The id of the zone,If not provided, the default zone id is used"       <>=""
    --script            "Name of the script(worker)"                                            <>:String
    --pattern           "Worker service associated with the zone and hostname"                  <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    local gen_cf_json=""
    gen_cf_json="$(param:option2json -zone_id)"
    ___x_cmd_cf_curl put "/zones/${zone_id}/workers/routes/$1" gen_cf_json
}
# EndSection