# shellcheck shell=sh   disable=SC3043,2154
xrc:mod:lib cf \
            worker/kv \
            worker/cron \
            worker/route \
            worker/domain \
            worker/subdomain

___x_cmd_cf_worker(){
    param:scope     ___x_cmd_cf
    param:subcmd    ___x_cmd_cf_worker                  \
        ls              "List worker"                           \
        kv              "Kv namespaces manage"                  \
        cron            "Cron Triggers manage"                  \
        info            "Get script metadata and config"        \
        route           "Worker route manage"                   \
        create          "Create a Worker"                       \
        domain          "Worker domain manage"                  \
        update          "Update Usage Model"                    \
        delete          "Delete worker"                         \
        download        "Get script content"                    \
        subdomain       "Worker subdomain manage"


    param:subcmd:try
    param:run

    ___x_cmd_cf_worker  _param_help_doc
    return 1
}

# Section: worker ls
# https://developers.cf.com/api/operations/worker-script-list-workers
___x_cmd_cf_worker_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}/workers/scripts"
    else
        ___x_cmd_cf_curl get "/accounts/${account_id}/workers/scripts" | {
        x jo .result | x jo 2c .id .usage_model | x csv header --add WorkerName UsageModel | x csv app
        }
    fi
}
# EndSection

# Section: worker info
# x cf worker info basic-bundle
# https://developers.cf.com/api/operations/worker-script-get-settings
___x_cmd_cf_worker_info(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "Name of the script(worker)"            <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    ___x_cmd_cf_curl get "/accounts/${account_id}/workers/scripts/$1/settings"
}
# EndSection

# Section: worker update
# x cf worker update --usage_model unbound xx-ts
# https://developers.cf.com/api/operations/worker-script-update-usage-model
___x_cmd_cf_worker_update(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "Name of the script(worker)"                    <>:String
    --usage_model       "Specifies the usage model for the Worker"      <>:String   = bundled unbound
    --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/scripts/$1/usage-model" gen_cf_json
}
# EndSection

# Section: worker create
# x cf worker create --script_name "xx-ts" --body 'addEventListener('\''fetch'\'', (event) => event.respondWith(new Response('\''OK'\'')))'
# https://developers.cf.com/api/operations/worker-script-upload-worker-module
___x_cmd_cf_worker_create(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    --script_name       "Name of the script(worker)"    <>:String
    --body              "Raw javascript content"        <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default
    local cf_content_type="Content-Type: application/javascript"
    cf:debug  "body: $body"
    ___x_cmd_cf_curl put "/accounts/${account_id}/workers/scripts/${script_name}" body
}
# EndSection

# Section: worker delete
# https://developers.cf.com/api/operations/worker-script-delete-worker
___x_cmd_cf_worker_delete(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "Name of the script(worker)"            <>:String
    --force             "If set to true, delete will not be stopped by associated service binding, durable object, or other binding"    <>:bole=""
    --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}/workers/scripts/$1"
}
# EndSection

# Section: worker download
# https://developers.cf.com/api/operations/worker-script-download-worker
___x_cmd_cf_worker_download(){
    param:scope     ___x_cmd_cf
    param:dsl       '
option:
    #1                  "Name of the script(worker)"            <>:String
    --json|-j           "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    ___x_cmd_cf_curl get "/accounts/${account_id}/workers/scripts/$1"
}
# EndSection