# shellcheck shell=sh disable=SC3043,SC2154

# https://developers.cf.com/r2/api/s3/api/#object-level-operations
___x_cmd_cf_r2_object(){
    param:scope     ___x_cmd_cf
    param:subcmd    ___x_cmd_cf_r2_object           \
        ls                  "List object"                   \
        info                "Get object info"               \
        delete              "Delete object"                 \
        create              "Create object"
        # multipart_upload    "multipart upload manage"


    param:subcmd:try
    param:run

    ___x_cmd_cf_r2_object _param_help_doc
    return 1
}

# Section: ListObject
___x_cmd_cf_r2_object_ls(){
    param:scope     ___x_cmd_cf
    param:dsl       '
options:
    --bucket                 "bucket name"                                                                                                   <>:String
    --prefix                 "Limits the response to keys that begin with the specified prefix"                                              <>=""
    --max_keys               "A delimiter is a character that you use to group keys"                                                         <>:Number=""
    --encoding_type          "The encoding type used to encode the object key in the response"                                               <>=""
    --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}/r2/buckets/${bucket}/objects" prefix 
    else
        ___x_cmd_cf_curl get "/accounts/${account_id}/r2/buckets/${bucket}/objects" prefix | {
        x jo .result | x jo 2c .key .etag | x csv header --add Name Etag | x csv static_tab
        }
    fi
}
# EndSection


# Section: GetObject
___x_cmd_cf_r2_object_info(){
    param:scope     ___x_cmd_cf
    param:dsl       '
options:
    --bucket                                "bucket name"                                                                               <>:String
    --object                                "object name"                                                                               <>:String
    --if_match                              "Return the object only if its entity tag (ETag) is the same as the one specified"          <>=""
    --if_none_match                         "Return the object only if its entity tag (ETag) is different from the one specified"       <>=""
    --if_modified_since                     "Return the object only if it has been modified since the specified time"                   <>=""
    --if_unmodified_since                   "Return the object only if it has not been modified since the specified time"               <>=""
    Range|--range                           "Downloads the specified range bytes of an object"                                          <>=""
    partNumber|--part_number                "Part number of the object being read"                                                      <>=""
    --json|-j                               "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    ___x_cmd_cf_curl get "/accounts/${account_id}/r2/buckets/${bucket}/objects/${object}"
}
# EndSection

# Section: PutObject
___x_cmd_cf_r2_object_create(){
    param:scope     ___x_cmd_cf
    param:dsl       '
options:
    #1                               "target upload file path"                                                                   <>
    --bucket                         "bucket name"                                                                               <>:String
    --object                         "object name"                                                                               <>:String
    --expires                        "The date and time at which the object is no longer cacheable"                              <>=""
    --cache_control                  "Can be used to specify caching behavior along the request/reply chain"                     <>=""
    --content_disposition            "Specifies presentational information for the object"                                       <>=""
    --content_encoding               "Declare the encoding of the object"                                                        <>=""
    --json|-j                        "output in json format"
'
    param:run
    ___x_cmd_cf_param_default

    [ -n "$1" ] || M='accepts 1 arg (target upload file path), received empty' arg:ret:64
    local _file_path="$1"

    ___x_cmd_cf_curl put "/accounts/${account_id}/r2/buckets/${bucket}/objects/${object}"
}
# EndSection

# Section: DeleteObject
___x_cmd_cf_r2_object_delete(){
    param:scope     ___x_cmd_cf
    param:dsl       '
options:
    --bucket            "bucket name"                            <>:String
    --object            "object name"                            <>: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}/r2/buckets/${bucket}/objects/${object}"
}
# EndSection

# # TODO: endpoint not found
# ___x_cmd_cf_r2_object_multipart_upload(){
#     param:scope     ___x_cmd_cf
#     param:subcmd    ___x_cmd_cf_r2_object_multipart_upload  \
#         ls                "List multipart upload"                   \
#         get               "Get the Parts"                           \
#         abort             "abort multipart upload"                  \
#         upload            "Uploads a part in a multipart upload"    \
#         create            "Create multipart upload"                 \
#         complete          "complete multipart upload"

#     param:subcmd:try
#     param:run

#     ___x_cmd_cf_r2_object_multipart_upload _param_help_doc
#     return 1
# }

# # Section: CreateMultipartUpload
# ___x_cmd_cf_r2_object_multipart_upload_create(){
#     param:scope     ___x_cmd_cf
#     param:dsl       '
# options:
#     --bucket                      "bucket name"                                                                   <>:String
#     --object                      "object name"                                                                   <>:String
#     --expires                     "The date and time at which the object is no longer cacheable"                  <>=""
#     --content_type                "A standard MIME type describing the format of the object data"                 <>=""
#     --cache_control               "Specifies caching behavior along the request/reply chain"                      <>=""
#     --content_encoding            "Declare the encoding of the object"                                            <>=""
#     --content_language            "The language the content is in"                                                <>=""
#     --content_disposition         "Specifies presentational information for the object"                           <>=""
#     --json|-j                     "output in json format"
# '
#     param:run
#     ___x_cmd_cf_param_default

#     ___x_cmd_cf_curl post "/accounts/${account_id}/r2/buckets/${bucket}/objects/${object}/uploads"
# }
# # EndSection


# # Section: ListMultipartUploads
# ___x_cmd_cf_r2_object_multipart_upload_ls(){
#     param:scope     ___x_cmd_cf
#     param:dsl       '
# options:
#     --bucket                        "bucket name"                                                                                                        <>:String
#     --prefix                        "Lists in-progress uploads only for those keys that begin with the specified prefix"                                 <>=""
#     --delimiter                     "The character used to group Object names"                                                                           <>=""
#     --encoding_type                 "Requests Amazon S3 to encode the object keys in the response and specifies the encoding method to use"              <>=""
#     --max_uploads                   "Sets the maximum number of multipart uploads, from 1 to 1,000, to return in the response body"                      <>=""
#     --key_marker                    "Together with upload-id-marker, this parameter specifies the multipart upload after which listing should begin"     <>=""
#     --upload_id_marker              "Together with key-marker, specifies the multipart upload after which listing should begin"                          <>=""
#     --json|-j                       "output in json format"
# '
#     param:run
#     ___x_cmd_cf_param_default

#     ___x_cmd_cf_curl get "/accounts/${account_id}/r2/buckets/${bucket}/uploads"
# }
# # EndSection

# # Section: UploadPart
# # https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPart.html
# ___x_cmd_cf_r2_object_multipart_upload_upload(){
#     param:scope     ___x_cmd_cf
#     param:dsl       '
# options:
#     #1                                  "target upload file path"                                                                       <>
#     --bucket                            "bucket name"                                                                                   <>:String
#     --object                            "object name"                                                                                   <>:String
#     --upload_id                         "Upload ID identifying the multipart upload whose part is being uploaded"                       <>:String
#     --part_number                       "Part number of part being uploaded. This is a positive integer between 1 and 10,000"           <>:Number
#     --x_amz_checksum_crc32              "This header can be used as a data integrity check to verify that the data received is the same data that was originally sent"   <>=""
#     --x_amz_checksum_sha256             "This header can be used as a data integrity check to verify that the data received is the same data that was originally sent"   <>=""
#     --json|-j                           "output in json format"
# '
#     param:run

#     [ -n "$1" ] || M='accepts 1 arg (target upload file path), received empty' arg:ret:64
#     local _file_path="$1"
#     x api aws s3 put "/$object" "partNumber=$part_number" "uploadId=$upload_id"
#     ___x_cmd_cf_curl put "/accounts/${account_id}/r2/buckets/${bucket}/objects/${object}/uploads"
# }
# # EndSection

# # Section: ListParts
# # https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListParts.html
# ___x_cmd_cf_r2_object_multipart_upload_get(){
#     param:scope     ___x_cmd_cf
#     param:dsl       '
# options:
#     --bucket                     "bucket name"                                                                                                <>:String
#     --object                     "object name"                                                                                                <>:String
#     --upload_id                  "Upload ID identifying the multipart upload whose parts are being listed"                                    <>:String
#     --max_parts                  "Sets the maximum number of parts to return"                                                                 <>:Number=""
#     --part_number_marker         "Specifies the part after which listing should begin. Only parts with higher part numbers will be listed"    <>:Number=""
#     --json|-j                    "output in json format"
# '
#     param:run
#     ___x_cmd_cf_param_default

#     ___x_cmd_cf_curl get "/accounts/${account_id}/r2/buckets/${bucket}/objects/${object}/uploadId/${upload_id}"
# }
# # EndSection

# # Section: AbortMultipartUpload
# # https://docs.aws.amazon.com/AmazonS3/latest/API/API_AbortMultipartUpload.html
# ___x_cmd_cf_r2_object_multipart_upload_abort(){
#     param:scope     ___x_cmd_cf
#     param:dsl       '
# options:
#     --bucket                           "bucket name"                                        <>:String
#     --region_id                        "Available regions"                                  <>:RegionId
#     --object                           "object name"                                        <>:String
#     --upload_id                        "The upload Id"                                      <>:String
#     --x_amz_expected_bucket_owner      "The account ID of the expected bucket owner"        <>=""
# '
#     param:run
#     ___x_cmd_cf_param_default

#     ___x_cmd_cf_curl delete "/accounts/${account_id}/r2/buckets/${bucket}/objects/${object}/uploadId/${upload_id}"
# }
# # EndSection

# # Section: CompleteMultipartUpload
# # https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html
# ___x_cmd_cf_r2_object_multipart_upload_complete(){
#     param:scope     ___x_cmd_cf
#     param:dsl       '
# options:
#     --region                           "Assign region"                                                                    <>="us-east-1"
#     --bucket                           "bucket name"                                                                      <>:String
#     --object                           "object name"                                                                      <>:String
#     --etag                             "After the part is successfully uploaded, the ETag value returned by OSS"          <>:String
#     --upload_id                        "The upload Id"                                                                    <>:String
#     --part_number                      "Part Number"                                                                      <>:Number
#     --x_amz_checksum_crc32             "This header can be used as a data integrity check to verify that the data received is the same data that was originally sent"   <>=""
#     --x_amz_checksum_sha256            "This header can be used as a data integrity check to verify that the data received is the same data that was originally sent"   <>=""
#     --x_amz_expected_bucket_owner      "The account ID of the expected bucket owner"                                                                                    <>=""
# '
#     param:run
# }
# # EndSection
