/*
 * 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'
import { kAcceptedParams } from '../../symbols'

interface That {
  transport: Transport
  [kAcceptedParams]: Record<string, { path: string[], body: string[], query: string[] }>
}

const commonQueryParams = ['error_trace', 'filter_path', 'human', 'pretty']

export default class Profiling {
  transport: Transport
  [kAcceptedParams]: Record<string, { path: string[], body: string[], query: string[] }>
  constructor (transport: Transport) {
    this.transport = transport
    this[kAcceptedParams] = {
      'profiling.flamegraph': {
        path: [],
        body: [
          'conditions'
        ],
        query: []
      },
      'profiling.stacktraces': {
        path: [],
        body: [
          'conditions'
        ],
        query: []
      },
      'profiling.status': {
        path: [],
        body: [],
        query: [
          'master_timeout',
          'timeout',
          'wait_for_resources_created'
        ]
      },
      'profiling.topn_functions': {
        path: [],
        body: [
          'conditions'
        ],
        query: []
      }
    }
  }

  /**
    * Returns basic information about the status of Universal Profiling.
    * @see {@link https://www.elastic.co/guide/en/observability/master/universal-profiling.html | Elasticsearch API documentation}
    */
  async flamegraph (this: That, params: T.ProfilingFlamegraphRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.ProfilingFlamegraphResponse>
  async flamegraph (this: That, params: T.ProfilingFlamegraphRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.ProfilingFlamegraphResponse, unknown>>
  async flamegraph (this: That, params: T.ProfilingFlamegraphRequest, options?: TransportRequestOptions): Promise<T.ProfilingFlamegraphResponse>
  async flamegraph (this: That, params: T.ProfilingFlamegraphRequest, options?: TransportRequestOptions): Promise<any> {
    const {
      path: acceptedPath,
      body: acceptedBody,
      query: acceptedQuery
    } = this[kAcceptedParams]['profiling.flamegraph']

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

    let body: any = params.body ?? undefined
    for (const key in params) {
      if (acceptedBody.includes(key)) {
        // @ts-expect-error
        body = params[key]
      } else if (acceptedPath.includes(key)) {
        continue
      } else if (key !== 'body' && key !== 'querystring') {
        if (acceptedQuery.includes(key) || commonQueryParams.includes(key)) {
          // @ts-expect-error
          querystring[key] = params[key]
        } else {
          body = body ?? {}
          // @ts-expect-error
          body[key] = params[key]
        }
      }
    }

    const method = 'POST'
    const path = '/_profiling/flamegraph'
    const meta: TransportRequestMetadata = {
      name: 'profiling.flamegraph',
      acceptedParams: {
        path: [],
        body: ['conditions'],
        query: []
      }
    }
    return await this.transport.request({ path, method, querystring, body, meta }, options)
  }

  /**
    * Extracts raw stacktrace information from Universal Profiling.
    * @see {@link https://www.elastic.co/guide/en/observability/master/universal-profiling.html | Elasticsearch API documentation}
    */
  async stacktraces (this: That, params: T.ProfilingStacktracesRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.ProfilingStacktracesResponse>
  async stacktraces (this: That, params: T.ProfilingStacktracesRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.ProfilingStacktracesResponse, unknown>>
  async stacktraces (this: That, params: T.ProfilingStacktracesRequest, options?: TransportRequestOptions): Promise<T.ProfilingStacktracesResponse>
  async stacktraces (this: That, params: T.ProfilingStacktracesRequest, options?: TransportRequestOptions): Promise<any> {
    const {
      path: acceptedPath,
      body: acceptedBody,
      query: acceptedQuery
    } = this[kAcceptedParams]['profiling.stacktraces']

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

    let body: any = params.body ?? undefined
    for (const key in params) {
      if (acceptedBody.includes(key)) {
        // @ts-expect-error
        body = params[key]
      } else if (acceptedPath.includes(key)) {
        continue
      } else if (key !== 'body' && key !== 'querystring') {
        if (acceptedQuery.includes(key) || commonQueryParams.includes(key)) {
          // @ts-expect-error
          querystring[key] = params[key]
        } else {
          body = body ?? {}
          // @ts-expect-error
          body[key] = params[key]
        }
      }
    }

    const method = 'POST'
    const path = '/_profiling/stacktraces'
    const meta: TransportRequestMetadata = {
      name: 'profiling.stacktraces',
      acceptedParams: {
        path: [],
        body: ['conditions'],
        query: []
      }
    }
    return await this.transport.request({ path, method, querystring, body, meta }, options)
  }

  /**
    * Returns basic information about the status of Universal Profiling.
    * @see {@link https://www.elastic.co/guide/en/observability/master/universal-profiling.html | Elasticsearch API documentation}
    */
  async status (this: That, params?: T.ProfilingStatusRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.ProfilingStatusResponse>
  async status (this: That, params?: T.ProfilingStatusRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.ProfilingStatusResponse, unknown>>
  async status (this: That, params?: T.ProfilingStatusRequest, options?: TransportRequestOptions): Promise<T.ProfilingStatusResponse>
  async status (this: That, params?: T.ProfilingStatusRequest, options?: TransportRequestOptions): Promise<any> {
    const {
      path: acceptedPath
    } = this[kAcceptedParams]['profiling.status']

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

    const method = 'GET'
    const path = '/_profiling/status'
    const meta: TransportRequestMetadata = {
      name: 'profiling.status',
      acceptedParams: {
        path: [],
        body: [],
        query: ['master_timeout', 'timeout', 'wait_for_resources_created']
      }
    }
    return await this.transport.request({ path, method, querystring, body, meta }, options)
  }

  /**
    * Extracts a list of topN functions from Universal Profiling.
    * @see {@link https://www.elastic.co/guide/en/observability/master/universal-profiling.html | Elasticsearch API documentation}
    */
  async topnFunctions (this: That, params: T.ProfilingTopnFunctionsRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.ProfilingTopnFunctionsResponse>
  async topnFunctions (this: That, params: T.ProfilingTopnFunctionsRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.ProfilingTopnFunctionsResponse, unknown>>
  async topnFunctions (this: That, params: T.ProfilingTopnFunctionsRequest, options?: TransportRequestOptions): Promise<T.ProfilingTopnFunctionsResponse>
  async topnFunctions (this: That, params: T.ProfilingTopnFunctionsRequest, options?: TransportRequestOptions): Promise<any> {
    const {
      path: acceptedPath,
      body: acceptedBody,
      query: acceptedQuery
    } = this[kAcceptedParams]['profiling.topn_functions']

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

    let body: any = params.body ?? undefined
    for (const key in params) {
      if (acceptedBody.includes(key)) {
        // @ts-expect-error
        body = params[key]
      } else if (acceptedPath.includes(key)) {
        continue
      } else if (key !== 'body' && key !== 'querystring') {
        if (acceptedQuery.includes(key) || commonQueryParams.includes(key)) {
          // @ts-expect-error
          querystring[key] = params[key]
        } else {
          body = body ?? {}
          // @ts-expect-error
          body[key] = params[key]
        }
      }
    }

    const method = 'POST'
    const path = '/_profiling/topn/functions'
    const meta: TransportRequestMetadata = {
      name: 'profiling.topn_functions',
      acceptedParams: {
        path: [],
        body: ['conditions'],
        query: []
      }
    }
    return await this.transport.request({ path, method, querystring, body, meta }, options)
  }
}
