/*
 * 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[] }>
}

export default class Encryption {
  transport: Transport
  [kAcceptedParams]: Record<string, { path: string[], body: string[], query: string[] }>
  constructor (transport: Transport) {
    this.transport = transport
    this[kAcceptedParams] = {
      'encryption.reset': {
        path: [],
        body: [],
        query: [
          'accept_data_loss',
          'master_timeout',
          'timeout'
        ]
      }
    }
  }

  /**
    * Reset the project encryption key. Destroy the current project encryption key (PEK) and generate a new one. This is the recovery path for when the on-disk encrypted PEK becomes permanently inaccessible, for example because the key encryption material protecting it was lost. All data that was encrypted under the destroyed key becomes permanently unrecoverable. Each feature that stores encrypted data decides how to handle its own data during the reset: some features drop the encrypted values entirely, while others preserve the rest of the affected data and only clear the values that can no longer be decrypted. Because this operation causes permanent data loss, it requires the `accept_data_loss` query parameter to be set to `true`.
    * @see {@link https://www.elastic.co/docs/api/doc/elasticsearch#TODO | Elasticsearch API documentation}
    */
  async reset (this: That, params: T.EncryptionResetRequest, options?: TransportRequestOptionsWithOutMeta): Promise<T.EncryptionResetResponse>
  async reset (this: That, params: T.EncryptionResetRequest, options?: TransportRequestOptionsWithMeta): Promise<TransportResult<T.EncryptionResetResponse, unknown>>
  async reset (this: That, params: T.EncryptionResetRequest, options?: TransportRequestOptions): Promise<T.EncryptionResetResponse>
  async reset (this: That, params: T.EncryptionResetRequest, options?: TransportRequestOptions): Promise<any> {
    const {
      path: acceptedPath
    } = this[kAcceptedParams]['encryption.reset']

    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 = '/_encryption/_reset'
    const meta: TransportRequestMetadata = {
      name: 'encryption.reset',
      acceptedParams: {
        path: [],
        body: [],
        query: ['accept_data_loss', 'master_timeout', 'timeout']
      }
    }
    return await this.transport.request({ path, method, querystring, body, meta }, options)
  }
}
