// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import { APIResource } from '../../../resource';
import { isRequestOptions } from '../../../core';
import * as Core from '../../../core';
import * as ListsAPI from './lists';
import { CursorPaginationAfter, type CursorPaginationAfterParams } from '../../../pagination';

export class Items extends APIResource {
  /**
   * Appends new items to the list.
   *
   * This operation is asynchronous. To get current the operation status, invoke the
   * `Get bulk operation status` endpoint with the returned `operation_id`.
   *
   * There is a limit of 1 pending bulk operation per account. If an outstanding bulk
   * operation is in progress, the request will be rejected.
   *
   * @example
   * ```ts
   * const item = await client.rules.lists.items.create(
   *   '2c0fc9fa937b11eaa1b71c4d701ab86e',
   *   {
   *     account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *     body: [{ ip: '10.0.0.1' }],
   *   },
   * );
   * ```
   */
  create(
    listId: string,
    params: ItemCreateParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<ItemCreateResponse> {
    const { account_id = this._client.accountId, body } = params;
    return (
      this._client.post(`/accounts/${account_id}/rules/lists/${listId}/items`, {
        body: body,
        ...options,
      }) as Core.APIPromise<{ result: ItemCreateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Removes all existing items from the list and adds the provided items to the
   * list.
   *
   * This operation is asynchronous. To get current the operation status, invoke the
   * `Get bulk operation status` endpoint with the returned `operation_id`.
   *
   * There is a limit of 1 pending bulk operation per account. If an outstanding bulk
   * operation is in progress, the request will be rejected.
   *
   * @example
   * ```ts
   * const item = await client.rules.lists.items.update(
   *   '2c0fc9fa937b11eaa1b71c4d701ab86e',
   *   {
   *     account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *     body: [{ ip: '10.0.0.1' }],
   *   },
   * );
   * ```
   */
  update(
    listId: string,
    params: ItemUpdateParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<ItemUpdateResponse> {
    const { account_id = this._client.accountId, body } = params;
    return (
      this._client.put(`/accounts/${account_id}/rules/lists/${listId}/items`, {
        body: body,
        ...options,
      }) as Core.APIPromise<{ result: ItemUpdateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Fetches all the items in the list.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const itemListResponse of client.rules.lists.items.list(
   *   '2c0fc9fa937b11eaa1b71c4d701ab86e',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    listId: string,
    params?: ItemListParams,
    options?: Core.RequestOptions,
  ): Core.PagePromise<ItemListResponsesCursorPaginationAfter, ItemListResponse>;
  list(
    listId: string,
    options?: Core.RequestOptions,
  ): Core.PagePromise<ItemListResponsesCursorPaginationAfter, ItemListResponse>;
  list(
    listId: string,
    params: ItemListParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.PagePromise<ItemListResponsesCursorPaginationAfter, ItemListResponse> {
    if (isRequestOptions(params)) {
      return this.list(listId, {}, params);
    }
    const { account_id = this._client.accountId, ...query } = params;
    return this._client.getAPIList(
      `/accounts/${account_id}/rules/lists/${listId}/items`,
      ItemListResponsesCursorPaginationAfter,
      { query, ...options },
    );
  }

  /**
   * Removes one or more items from a list.
   *
   * This operation is asynchronous. To get current the operation status, invoke the
   * `Get bulk operation status` endpoint with the returned `operation_id`.
   *
   * There is a limit of 1 pending bulk operation per account. If an outstanding bulk
   * operation is in progress, the request will be rejected.
   *
   * @example
   * ```ts
   * const item = await client.rules.lists.items.delete(
   *   '2c0fc9fa937b11eaa1b71c4d701ab86e',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  delete(
    listId: string,
    params: ItemDeleteParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<ItemDeleteResponse> {
    const { account_id = this._client.accountId, ...body } = params;
    return (
      this._client.delete(`/accounts/${account_id}/rules/lists/${listId}/items`, {
        body,
        ...options,
      }) as Core.APIPromise<{ result: ItemDeleteResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Fetches a list item in the list.
   *
   * @example
   * ```ts
   * const item = await client.rules.lists.items.get(
   *   '2c0fc9fa937b11eaa1b71c4d701ab86e',
   *   '34b12448945f11eaa1b71c4d701ab86e',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  get(
    listId: string,
    itemId: string,
    params?: ItemGetParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<ItemGetResponse>;
  get(listId: string, itemId: string, options?: Core.RequestOptions): Core.APIPromise<ItemGetResponse>;
  get(
    listId: string,
    itemId: string,
    params: ItemGetParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<ItemGetResponse> {
    if (isRequestOptions(params)) {
      return this.get(listId, itemId, {}, params);
    }
    const { account_id = this._client.accountId } = params;
    return (
      this._client.get(
        `/accounts/${account_id}/rules/lists/${listId}/items/${itemId}`,
        options,
      ) as Core.APIPromise<{ result: ItemGetResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}

export class ItemListResponsesCursorPaginationAfter extends CursorPaginationAfter<ItemListResponse> {}

export interface ListCursor {
  after?: string;

  before?: string;
}

export interface ListCursorParam {
  after?: string;

  before?: string;
}

export interface ListItem {
  /**
   * The unique operation ID of the asynchronous action.
   */
  operation_id: string;
}

export interface ItemCreateResponse {
  /**
   * The unique operation ID of the asynchronous action.
   */
  operation_id: string;
}

export interface ItemUpdateResponse {
  /**
   * The unique operation ID of the asynchronous action.
   */
  operation_id: string;
}

export type ItemListResponse =
  | ItemListResponse.ListsListItemIPFull
  | ItemListResponse.ListsListItemHostnameFull
  | ItemListResponse.ListsListItemRedirectFull
  | ItemListResponse.ListsListItemASNFull;

export namespace ItemListResponse {
  export interface ListsListItemIPFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.
     */
    ip: string;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemHostnameFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * Valid characters for hostnames are ASCII(7) letters from a to z, the digits from
     * 0 to 9, wildcards (\*), and the hyphen (-).
     */
    hostname: ListsAPI.Hostname;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemRedirectFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * The definition of the redirect.
     */
    redirect: ListsAPI.Redirect;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemASNFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * Defines a non-negative 32 bit integer.
     */
    asn: number;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }
}

export interface ItemDeleteResponse {
  /**
   * The unique operation ID of the asynchronous action.
   */
  operation_id: string;
}

export type ItemGetResponse =
  | ItemGetResponse.ListsListItemIPFull
  | ItemGetResponse.ListsListItemHostnameFull
  | ItemGetResponse.ListsListItemRedirectFull
  | ItemGetResponse.ListsListItemASNFull;

export namespace ItemGetResponse {
  export interface ListsListItemIPFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.
     */
    ip: string;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemHostnameFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * Valid characters for hostnames are ASCII(7) letters from a to z, the digits from
     * 0 to 9, wildcards (\*), and the hyphen (-).
     */
    hostname: ListsAPI.Hostname;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemRedirectFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * The definition of the redirect.
     */
    redirect: ListsAPI.Redirect;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemASNFull {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;

    /**
     * Defines a non-negative 32 bit integer.
     */
    asn: number;

    /**
     * The RFC 3339 timestamp of when the list was created.
     */
    created_on: string;

    /**
     * The RFC 3339 timestamp of when the list was last modified.
     */
    modified_on: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }
}

export interface ItemCreateParams {
  /**
   * Path param: The Account ID for this resource.
   */
  account_id?: string;

  /**
   * Body param
   */
  body: Array<
    | ItemCreateParams.ListsListItemIPComment
    | ItemCreateParams.ListsListItemRedirectComment
    | ItemCreateParams.ListsListItemHostnameComment
    | ItemCreateParams.ListsListItemASNComment
  >;
}

export namespace ItemCreateParams {
  export interface ListsListItemIPComment {
    /**
     * An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.
     */
    ip: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemRedirectComment {
    /**
     * The definition of the redirect.
     */
    redirect: ListsAPI.RedirectParam;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemHostnameComment {
    /**
     * Valid characters for hostnames are ASCII(7) letters from a to z, the digits from
     * 0 to 9, wildcards (\*), and the hyphen (-).
     */
    hostname: ListsAPI.HostnameParam;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemASNComment {
    /**
     * Defines a non-negative 32 bit integer.
     */
    asn: number;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }
}

export interface ItemUpdateParams {
  /**
   * Path param: The Account ID for this resource.
   */
  account_id?: string;

  /**
   * Body param
   */
  body: Array<
    | ItemUpdateParams.ListsListItemIPComment
    | ItemUpdateParams.ListsListItemRedirectComment
    | ItemUpdateParams.ListsListItemHostnameComment
    | ItemUpdateParams.ListsListItemASNComment
  >;
}

export namespace ItemUpdateParams {
  export interface ListsListItemIPComment {
    /**
     * An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.
     */
    ip: string;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemRedirectComment {
    /**
     * The definition of the redirect.
     */
    redirect: ListsAPI.RedirectParam;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemHostnameComment {
    /**
     * Valid characters for hostnames are ASCII(7) letters from a to z, the digits from
     * 0 to 9, wildcards (\*), and the hyphen (-).
     */
    hostname: ListsAPI.HostnameParam;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }

  export interface ListsListItemASNComment {
    /**
     * Defines a non-negative 32 bit integer.
     */
    asn: number;

    /**
     * Defines an informative summary of the list item.
     */
    comment?: string;
  }
}

export interface ItemListParams extends CursorPaginationAfterParams {
  /**
   * Path param: The Account ID for this resource.
   */
  account_id?: string;

  /**
   * Query param: Amount of results to include in each paginated response. A
   * non-negative 32 bit integer.
   */
  per_page?: number;

  /**
   * Query param: A search query to filter returned items. Its meaning depends on the
   * list type: IP addresses must start with the provided string, hostnames and bulk
   * redirects must contain the string, and ASNs must match the string exactly.
   */
  search?: string;
}

export interface ItemDeleteParams {
  /**
   * Path param: The Account ID for this resource.
   */
  account_id?: string;

  /**
   * Body param
   */
  items?: Array<ItemDeleteParams.Item>;
}

export namespace ItemDeleteParams {
  export interface Item {
    /**
     * Defines the unique ID of the item in the List.
     */
    id: string;
  }
}

export interface ItemGetParams {
  /**
   * The Account ID for this resource.
   */
  account_id?: string;
}

Items.ItemListResponsesCursorPaginationAfter = ItemListResponsesCursorPaginationAfter;

export declare namespace Items {
  export {
    type ListCursor as ListCursor,
    type ListItem as ListItem,
    type ItemCreateResponse as ItemCreateResponse,
    type ItemUpdateResponse as ItemUpdateResponse,
    type ItemListResponse as ItemListResponse,
    type ItemDeleteResponse as ItemDeleteResponse,
    type ItemGetResponse as ItemGetResponse,
    ItemListResponsesCursorPaginationAfter as ItemListResponsesCursorPaginationAfter,
    type ItemCreateParams as ItemCreateParams,
    type ItemUpdateParams as ItemUpdateParams,
    type ItemListParams as ItemListParams,
    type ItemDeleteParams as ItemDeleteParams,
    type ItemGetParams as ItemGetParams,
  };
}
