/*
 * Copyright Elasticsearch B.V. and contributors
 * SPDX-License-Identifier: Apache-2.0
 */

// This file was automatically generated by elastic/elastic-client-generator-js
// DO NOT MODIFY IT BY HAND. Instead, modify the source open api file,
// and elastic/elastic-client-generator-js to regenerate this file again.

import {
  Transport,
  TransportRequestMetadata,
  TransportRequestOptions,
  TransportRequestOptionsWithMeta,
  TransportRequestOptionsWithOutMeta,
  TransportResult
} from '@elastic/transport'
import * as T from '../types'

interface That {
  transport: Transport
}

const acceptedParams: Record<string, { path: string[], body: string[], query: string[] }> = {
  reindex_rethrottle: {
    path: [
      'task_id'
    ],
    body: [],
    query: [
      'requests_per_second',
      'group_by'
    ]
  }
}

/**
  * Throttle a reindex operation. Change the maximum number of documents to index per second for a particular reindex operation. For example, to unthrottle to unlimited documents per second: ``` POST _reindex/r1A2WoRbTwKZ516z6NEs5A:36619/_rethrottle?requests_per_second=-1 ``` Rethrottling that speeds up the query takes effect immediately. Rethrottling that slows down the query will take effect after completing the current batch of documents. This behavior prevents scroll timeouts. This API follows reindex tasks across node-shutdown relocations, so callers can keep using the original task ID throughout the lifetime of the operation. The relocated task ID is also accepted and is followed transparently. In either case, returned task IDs and timings reflect the original task, not its relocated successor. The rethrottle may not have been applied to any tasks if either `node_failures` or `task_failures` are non-empty, or if the response contains no successfully rethrottled tasks — that is, no entries under `nodes` (returned with the default `group_by=nodes` in stack) or under `tasks` (returned in serverless, or in stack with `group_by=none` or `group_by=parents`).
  * @see {@link https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-reindex | Elasticsearch API documentation}
  */
export default async function ReindexRethrottleApi (this: That, params: T.ReindexRethrottleRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.ReindexRethrottleResponse>
export default async function ReindexRethrottleApi (this: That, params: T.ReindexRethrottleRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.ReindexRethrottleResponse, unknown>>
export default async function ReindexRethrottleApi (this: That, params: T.ReindexRethrottleRequest, options?: TransportRequestOptions): Promise<T.ReindexRethrottleResponse>
export default async function ReindexRethrottleApi (this: That, params: T.ReindexRethrottleRequest, options?: TransportRequestOptions): Promise<any> {
  const {
    path: acceptedPath
  } = acceptedParams.reindex_rethrottle

  const userQuery = params?.querystring
  const querystring: Record<string, any> = userQuery != null ? { ...userQuery } : {}

  let body: Record<string, any> | string | undefined
  const userBody = params?.body
  if (userBody != null) {
    if (typeof userBody === 'string') {
      body = userBody
    } else {
      body = { ...userBody }
    }
  }

  for (const key in params) {
    if (acceptedPath.includes(key)) {
      continue
    } else if (key !== 'body' && key !== 'querystring') {
      // @ts-expect-error
      querystring[key] = params[key]
    }
  }

  const method = 'POST'
  const path = `/_reindex/${encodeURIComponent(params.task_id.toString())}/_rethrottle`
  const meta: TransportRequestMetadata = {
    name: 'reindex_rethrottle',
    pathParts: {
      task_id: params.task_id
    },
    acceptedParams: {
      path: ['task_id'],
      body: [],
      query: ['requests_per_second', 'group_by']
    }
  }
  return await this.transport.request({ path, method, querystring, body, meta }, options)
}
