// this file is @generated

import {
  type ListResponseOperationalWebhookEndpointOut,
  ListResponseOperationalWebhookEndpointOutSerializer,
} from "../models/listResponseOperationalWebhookEndpointOut";
import {
  type OperationalWebhookEndpointHeadersIn,
  OperationalWebhookEndpointHeadersInSerializer,
} from "../models/operationalWebhookEndpointHeadersIn";
import {
  type OperationalWebhookEndpointHeadersOut,
  OperationalWebhookEndpointHeadersOutSerializer,
} from "../models/operationalWebhookEndpointHeadersOut";
import {
  type OperationalWebhookEndpointIn,
  OperationalWebhookEndpointInSerializer,
} from "../models/operationalWebhookEndpointIn";
import {
  type OperationalWebhookEndpointOut,
  OperationalWebhookEndpointOutSerializer,
} from "../models/operationalWebhookEndpointOut";
import {
  type OperationalWebhookEndpointSecretIn,
  OperationalWebhookEndpointSecretInSerializer,
} from "../models/operationalWebhookEndpointSecretIn";
import {
  type OperationalWebhookEndpointSecretOut,
  OperationalWebhookEndpointSecretOutSerializer,
} from "../models/operationalWebhookEndpointSecretOut";
import {
  type OperationalWebhookEndpointUpsertIn,
  OperationalWebhookEndpointUpsertInSerializer,
} from "../models/operationalWebhookEndpointUpsertIn";
import type { Ordering } from "../models/ordering";
import { HttpMethod, SvixRequest, type SvixRequestContext } from "../request";

export interface OperationalWebhookEndpointListOptions {
  /** Limit the number of returned items */
  limit?: number;
  /** The iterator returned from a prior invocation */
  iterator?: string | null;
  /** The sorting order of the returned items */
  order?: Ordering;
}

export interface OperationalWebhookEndpointCreateOptions {
  idempotencyKey?: string;
}

export interface OperationalWebhookEndpointRotateSecretOptions {
  idempotencyKey?: string;
}

export class OperationalWebhookEndpoint {
  public constructor(private readonly requestCtx: SvixRequestContext) {}

  /** List operational webhook endpoints. */
  public async list(
    options?: OperationalWebhookEndpointListOptions
  ): Promise<ListResponseOperationalWebhookEndpointOut> {
    const request = new SvixRequest(
      HttpMethod.GET,
      "/api/v1/operational-webhook/endpoint"
    );

    request.setQueryParams({
      limit: options?.limit,
      iterator: options?.iterator,
      order: options?.order,
    });

    return await request.send(
      this.requestCtx,
      ListResponseOperationalWebhookEndpointOutSerializer._fromJsonObject
    );
  }

  /** Create an operational webhook endpoint. */
  public async create(
    operationalWebhookEndpointIn: OperationalWebhookEndpointIn,
    options?: OperationalWebhookEndpointCreateOptions
  ): Promise<OperationalWebhookEndpointOut> {
    const request = new SvixRequest(
      HttpMethod.POST,
      "/api/v1/operational-webhook/endpoint"
    );

    request.setHeaderParam("idempotency-key", options?.idempotencyKey);
    request.setBody(
      OperationalWebhookEndpointInSerializer._toJsonObject(operationalWebhookEndpointIn)
    );

    return await request.send(
      this.requestCtx,
      OperationalWebhookEndpointOutSerializer._fromJsonObject
    );
  }

  /** Get an operational webhook endpoint. */
  public async get(endpointId: string): Promise<OperationalWebhookEndpointOut> {
    const request = new SvixRequest(
      HttpMethod.GET,
      "/api/v1/operational-webhook/endpoint/{endpoint_id}"
    );

    request.setPathParam("endpoint_id", endpointId);

    return await request.send(
      this.requestCtx,
      OperationalWebhookEndpointOutSerializer._fromJsonObject
    );
  }

  /** Create or update an operational webhook endpoint. */
  public async upsert(
    endpointId: string,
    operationalWebhookEndpointUpsertIn: OperationalWebhookEndpointUpsertIn
  ): Promise<OperationalWebhookEndpointOut> {
    const request = new SvixRequest(
      HttpMethod.PUT,
      "/api/v1/operational-webhook/endpoint/{endpoint_id}"
    );

    request.setPathParam("endpoint_id", endpointId);
    request.setBody(
      OperationalWebhookEndpointUpsertInSerializer._toJsonObject(
        operationalWebhookEndpointUpsertIn
      )
    );

    return await request.send(
      this.requestCtx,
      OperationalWebhookEndpointOutSerializer._fromJsonObject
    );
  }

  /** Delete an operational webhook endpoint. */
  public async delete(endpointId: string): Promise<void> {
    const request = new SvixRequest(
      HttpMethod.DELETE,
      "/api/v1/operational-webhook/endpoint/{endpoint_id}"
    );

    request.setPathParam("endpoint_id", endpointId);

    return await request.sendNoResponseBody(this.requestCtx);
  }

  /**
   * Get an operational webhook endpoint's signing secret.
   *
   * This is used to verify the authenticity of the webhook.
   * For more information please refer to [the consuming webhooks docs](https://docs.svix.com/consuming-webhooks/).
   */
  public async getSecret(
    endpointId: string
  ): Promise<OperationalWebhookEndpointSecretOut> {
    const request = new SvixRequest(
      HttpMethod.GET,
      "/api/v1/operational-webhook/endpoint/{endpoint_id}/secret"
    );

    request.setPathParam("endpoint_id", endpointId);

    return await request.send(
      this.requestCtx,
      OperationalWebhookEndpointSecretOutSerializer._fromJsonObject
    );
  }

  /**
   * Rotates an operational webhook endpoint's signing secret.
   *
   * The previous secret will remain valid for the next 24 hours.
   */
  public async rotateSecret(
    endpointId: string,
    operationalWebhookEndpointSecretIn: OperationalWebhookEndpointSecretIn = {},
    options?: OperationalWebhookEndpointRotateSecretOptions
  ): Promise<void> {
    const request = new SvixRequest(
      HttpMethod.POST,
      "/api/v1/operational-webhook/endpoint/{endpoint_id}/secret/rotate"
    );

    request.setPathParam("endpoint_id", endpointId);
    request.setHeaderParam("idempotency-key", options?.idempotencyKey);
    request.setBody(
      OperationalWebhookEndpointSecretInSerializer._toJsonObject(
        operationalWebhookEndpointSecretIn
      )
    );

    return await request.sendNoResponseBody(this.requestCtx);
  }

  /** Get the additional headers to be sent with the operational webhook. */
  public async getHeaders(
    endpointId: string
  ): Promise<OperationalWebhookEndpointHeadersOut> {
    const request = new SvixRequest(
      HttpMethod.GET,
      "/api/v1/operational-webhook/endpoint/{endpoint_id}/headers"
    );

    request.setPathParam("endpoint_id", endpointId);

    return await request.send(
      this.requestCtx,
      OperationalWebhookEndpointHeadersOutSerializer._fromJsonObject
    );
  }

  /** Set the additional headers to be sent with the operational webhook. */
  public async setHeaders(
    endpointId: string,
    operationalWebhookEndpointHeadersIn: OperationalWebhookEndpointHeadersIn
  ): Promise<void> {
    const request = new SvixRequest(
      HttpMethod.PUT,
      "/api/v1/operational-webhook/endpoint/{endpoint_id}/headers"
    );

    request.setPathParam("endpoint_id", endpointId);
    request.setBody(
      OperationalWebhookEndpointHeadersInSerializer._toJsonObject(
        operationalWebhookEndpointHeadersIn
      )
    );

    return await request.sendNoResponseBody(this.requestCtx);
  }
}
