/*
 * 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[] }> = {
  search_shards: {
    path: [
      'index'
    ],
    body: [],
    query: [
      'allow_no_indices',
      'expand_wildcards',
      'ignore_unavailable',
      'local',
      'master_timeout',
      'preference',
      'routing',
      '_slice'
    ]
  }
}

/**
  * Get the search shards. Get the indices and shards that a search request would be run against. This information can be useful for working out issues or planning optimizations with routing and shard preferences. When filtered aliases are used, the filter is returned as part of the `indices` section. If the Elasticsearch security features are enabled, you must have the `view_index_metadata` or `manage` index privilege for the target data stream, index, or alias.
  * @see {@link https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-search-shards | Elasticsearch API documentation}
  */
export default async function SearchShardsApi (this: That, params?: T.SearchShardsRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.SearchShardsResponse>
export default async function SearchShardsApi (this: That, params?: T.SearchShardsRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.SearchShardsResponse, unknown>>
export default async function SearchShardsApi (this: That, params?: T.SearchShardsRequest, options?: TransportRequestOptions): Promise<T.SearchShardsResponse>
export default async function SearchShardsApi (this: That, params?: T.SearchShardsRequest, options?: TransportRequestOptions): Promise<any> {
  const {
    path: acceptedPath
  } = acceptedParams.search_shards

  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 }
    }
  }

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

  let method = ''
  let path = ''
  if (params.index != null) {
    method = body != null ? 'POST' : 'GET'
    path = `/${encodeURIComponent(params.index.toString())}/_search_shards`
  } else {
    method = body != null ? 'POST' : 'GET'
    path = '/_search_shards'
  }
  const meta: TransportRequestMetadata = {
    name: 'search_shards',
    pathParts: {
      index: params.index
    },
    acceptedParams: {
      path: ['index'],
      body: [],
      query: ['allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local', 'master_timeout', 'preference', 'routing', '_slice']
    }
  }
  return await this.transport.request({ path, method, querystring, body, meta }, options)
}
