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

import { APIResource } from '../../../../core/resource';
import { APIPromise } from '../../../../core/api-promise';
import { buildHeaders } from '../../../../internal/headers';
import { RequestOptions } from '../../../../internal/request-options';
import { path } from '../../../../internal/utils/path';

export class BaseManaged extends APIResource {
  static override readonly _key: readonly ['r2', 'buckets', 'domains', 'managed'] = Object.freeze([
    'r2',
    'buckets',
    'domains',
    'managed',
  ] as const);

  /**
   * Enables or disables public access to the R2 bucket through its managed r2.dev
   * domain. Custom domain access is unaffected. The r2.dev domain is rate-limited
   * and intended for development use.
   *
   * @example
   * ```ts
   * const managed =
   *   await client.r2.buckets.domains.managed.update(
   *     'example-bucket',
   *     {
   *       account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *       enabled: true,
   *     },
   *   );
   * ```
   */
  update(
    bucketName: string,
    params: ManagedUpdateParams,
    options?: RequestOptions,
  ): APIPromise<ManagedUpdateResponse> {
    const { account_id, 'cf-r2-jurisdiction': cfR2Jurisdiction, ...body } = params;
    return (
      this._client.put(path`/accounts/${account_id}/r2/buckets/${bucketName}/domains/managed`, {
        body,
        ...options,
        headers: buildHeaders([
          {
            ...(cfR2Jurisdiction?.toString() != null ?
              { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() }
            : undefined),
          },
          options?.headers,
        ]),
      }) as APIPromise<{ result: ManagedUpdateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Gets the R2 bucket's managed r2.dev domain and whether public access is enabled.
   * The r2.dev domain is rate-limited and intended for development use.
   *
   * @example
   * ```ts
   * const manageds =
   *   await client.r2.buckets.domains.managed.list(
   *     'example-bucket',
   *     { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  list(
    bucketName: string,
    params: ManagedListParams,
    options?: RequestOptions,
  ): APIPromise<ManagedListResponse> {
    const { account_id, 'cf-r2-jurisdiction': cfR2Jurisdiction } = params;
    return (
      this._client.get(path`/accounts/${account_id}/r2/buckets/${bucketName}/domains/managed`, {
        ...options,
        headers: buildHeaders([
          {
            ...(cfR2Jurisdiction?.toString() != null ?
              { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() }
            : undefined),
          },
          options?.headers,
        ]),
      }) as APIPromise<{ result: ManagedListResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class Managed extends BaseManaged {}

export interface ManagedUpdateResponse {
  /**
   * Bucket ID.
   */
  bucketId: string;

  /**
   * Domain name of the bucket's r2.dev domain.
   */
  domain: string;

  /**
   * Whether this bucket is publicly accessible at the r2.dev domain.
   */
  enabled: boolean;
}

export interface ManagedListResponse {
  /**
   * Bucket ID.
   */
  bucketId: string;

  /**
   * Domain name of the bucket's r2.dev domain.
   */
  domain: string;

  /**
   * Whether this bucket is publicly accessible at the r2.dev domain.
   */
  enabled: boolean;
}

export interface ManagedUpdateParams {
  /**
   * Path param: Cloudflare account ID that owns the R2 resource.
   */
  account_id: string;

  /**
   * Body param: Whether to enable public bucket access at the r2.dev domain.
   */
  enabled: boolean;

  /**
   * Header param: Jurisdiction where objects in this bucket are guaranteed to be
   * stored.
   */
  'cf-r2-jurisdiction'?: 'default' | 'eu' | 'us' | 'fedramp' | 'fedramp-high';
}

export interface ManagedListParams {
  /**
   * Path param: Cloudflare account ID that owns the R2 resource.
   */
  account_id: string;

  /**
   * Header param: Jurisdiction where objects in this bucket are guaranteed to be
   * stored.
   */
  'cf-r2-jurisdiction'?: 'default' | 'eu' | 'us' | 'fedramp' | 'fedramp-high';
}

export declare namespace Managed {
  export {
    type ManagedUpdateResponse as ManagedUpdateResponse,
    type ManagedListResponse as ManagedListResponse,
    type ManagedUpdateParams as ManagedUpdateParams,
    type ManagedListParams as ManagedListParams,
  };
}
