/*
 * 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[] }> = {
  cancel_reindex: {
    path: [
      'task_id'
    ],
    body: [],
    query: [
      'wait_for_completion'
    ]
  }
}

/**
  * Cancel an ongoing reindex task. If `wait_for_completion` is `true` (the default), the response contains the final task state after cancellation. If `wait_for_completion` is `false`, the response contains only `acknowledged: true`. This API follows reindex tasks across node-shutdown relocations, so callers can keep using the original task ID throughout the lifetime of the operation. Returned task IDs and timings reflect the original task, not its relocated successor. Relocated task IDs are also supported. They are followed transparently and return the task ID and timings of the original task. When the task ID cannot be cancelled (unknown ID, non-reindex task, sliced child, finished task, or node left with no stored result), the API returns the following response with a 404 status code: ``` { "error": { "type": "resource_not_found_exception", "reason": "reindex task [r1A2WoRbTwKZ516z6NEs5A:36619] either not found or completed" }, "status": 404 } ``` During a brief handoff window of a node-shutdown relocation, you may receive the response below with a 503 status code. Retry with the same task ID; the retry follows the relocated task transparently. ``` { "error": { "type": "status_exception", "reason": "cannot cancel task [36619] because it is being relocated" }, "status": 503 } ```
  * @see {@link https://www.elastic.co/docs/api/doc/elasticsearch#TODO | Elasticsearch API documentation}
  */
export default async function CancelReindexApi (this: That, params: T.CancelReindexRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.CancelReindexResponse>
export default async function CancelReindexApi (this: That, params: T.CancelReindexRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.CancelReindexResponse, unknown>>
export default async function CancelReindexApi (this: That, params: T.CancelReindexRequest, options?: TransportRequestOptions): Promise<T.CancelReindexResponse>
export default async function CancelReindexApi (this: That, params: T.CancelReindexRequest, options?: TransportRequestOptions): Promise<any> {
  const {
    path: acceptedPath
  } = acceptedParams.cancel_reindex

  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())}/_cancel`
  const meta: TransportRequestMetadata = {
    name: 'cancel_reindex',
    pathParts: {
      task_id: params.task_id
    },
    acceptedParams: {
      path: ['task_id'],
      body: [],
      query: ['wait_for_completion']
    }
  }
  return await this.transport.request({ path, method, querystring, body, meta }, options)
}
