# shellcheck shell=sh   disable=SC3043,2154

___x_cmd_cf_worker_domain(){
    param:scope     ___x_cmd_cf
    param:subcmd    ___x_cmd_cf_worker_domain       \
        ls              "List worker domain"                \
        info            "Get worker domain information"     \
        attach          "Attach to Domain"                  \
        detach          "Detach from Domain"


    param:subcmd:try
    param:run

    ___x_cmd_cf_worker_domain  _param_help_doc
    return 1
}

# Section: domain ls
# https://developers.cf.com/api/operations/worker-domain-list-domains
___x_cmd_cf_worker_domain_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"       <>=""
    --zone_name         "The name of the zone"                                                  <>=""
    --service           "Worker service associated with the zone and hostname"                  <>=""
    --hostname          "Hostname of the Worker Domain"                                         <>=""
    --environment       "Worker environment associated with the zone and hostname"              <>=""
    --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}/workers/domains"
    else
        ___x_cmd_cf_curl get "/accounts/${account_id}/workers/domains" | {
        x jo .result | x jo 2c .id .content .type | x csv header --add Id Content Type | x csv static_tab
        }
    fi
}
# EndSection

# Section: domain info
# https://developers.cf.com/api/operations/worker-domain-get-a-domain
___x_cmd_cf_worker_domain_info(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "The Id of the Worker Domain"              <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default
    ___x_cmd_cf_curl get "/accounts/${account_id}/workers/domains/$1"
}
# EndSection

# Section: worker attach
# https://developers.cf.com/api/operations/worker-domain-attach-to-domain
___x_cmd_cf_worker_domain_attach(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    --zone_id           "The id of the zone,If not provided, the default zone id is used"   <>:String
    --service           "Worker service associated with the zone and hostname"              <>:String
    --hostname          "Hostname of the Worker Domain"                                     <>:String
    --environment       "Worker environment 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 )"
    ___x_cmd_cf_curl put "/accounts/${account_id}/workers/domains" gen_cf_json
}
# EndSection

# Section: worker detach
# https://developers.cf.com/api/operations/worker-domain-detach-from-domain
___x_cmd_cf_worker_domain_detach(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "The Id of the Worker Domain"              <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default
    
    ___x_cmd_cf_curl delete "/accounts/${account_id}/workers/domains/$1"
}
# EndSection