// 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 BaseCORS extends APIResource {
  static override readonly _key: readonly ['r2', 'buckets', 'cors'] = Object.freeze([
    'r2',
    'buckets',
    'cors',
  ] as const);

  /**
   * Replaces the Cross-Origin Resource Sharing (CORS) rules for an R2 bucket. Rules
   * specify which origins, methods, and headers are allowed for browser requests to
   * objects in the bucket.
   *
   * @example
   * ```ts
   * const cors = await client.r2.buckets.cors.update(
   *   'example-bucket',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  update(
    bucketName: string,
    params: CORSUpdateParams,
    options?: RequestOptions,
  ): APIPromise<CORSUpdateResponse> {
    const { account_id, 'cf-r2-jurisdiction': cfR2Jurisdiction, ...body } = params;
    return (
      this._client.put(path`/accounts/${account_id}/r2/buckets/${bucketName}/cors`, {
        body,
        ...options,
        headers: buildHeaders([
          {
            ...(cfR2Jurisdiction?.toString() != null ?
              { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() }
            : undefined),
          },
          options?.headers,
        ]),
      }) as APIPromise<{ result: CORSUpdateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Delete the CORS policy for a bucket.
   *
   * @example
   * ```ts
   * const cors = await client.r2.buckets.cors.delete(
   *   'example-bucket',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  delete(
    bucketName: string,
    params: CORSDeleteParams,
    options?: RequestOptions,
  ): APIPromise<CORSDeleteResponse> {
    const { account_id, 'cf-r2-jurisdiction': cfR2Jurisdiction } = params;
    return (
      this._client.delete(path`/accounts/${account_id}/r2/buckets/${bucketName}/cors`, {
        ...options,
        headers: buildHeaders([
          {
            ...(cfR2Jurisdiction?.toString() != null ?
              { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() }
            : undefined),
          },
          options?.headers,
        ]),
      }) as APIPromise<{ result: CORSDeleteResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Get the CORS policy for a bucket.
   *
   * @example
   * ```ts
   * const cors = await client.r2.buckets.cors.get(
   *   'example-bucket',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  get(bucketName: string, params: CORSGetParams, options?: RequestOptions): APIPromise<CORSGetResponse> {
    const { account_id, 'cf-r2-jurisdiction': cfR2Jurisdiction } = params;
    return (
      this._client.get(path`/accounts/${account_id}/r2/buckets/${bucketName}/cors`, {
        ...options,
        headers: buildHeaders([
          {
            ...(cfR2Jurisdiction?.toString() != null ?
              { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() }
            : undefined),
          },
          options?.headers,
        ]),
      }) as APIPromise<{ result: CORSGetResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class CORS extends BaseCORS {}

export type CORSUpdateResponse = unknown;

export type CORSDeleteResponse = unknown;

export interface CORSGetResponse {
  rules?: Array<CORSGetResponse.Rule>;
}

export namespace CORSGetResponse {
  export interface Rule {
    /**
     * Object specifying allowed origins, methods and headers for this CORS rule.
     */
    allowed: Rule.Allowed;

    /**
     * Identifier for this rule.
     */
    id?: string;

    /**
     * Specifies the headers that can be exposed back, and accessed by, the JavaScript
     * making the cross-origin request. If you need to access headers beyond the
     * safelisted response headers, such as Content-Encoding or cf-cache-status, you
     * must specify it here.
     */
    exposeHeaders?: Array<string>;

    /**
     * Specifies the amount of time (in seconds) browsers are allowed to cache CORS
     * preflight responses. Browsers may limit this to 2 hours or less, even if the
     * maximum value (86400) is specified.
     */
    maxAgeSeconds?: number;
  }

  export namespace Rule {
    /**
     * Object specifying allowed origins, methods and headers for this CORS rule.
     */
    export interface Allowed {
      /**
       * Specifies the value for the Access-Control-Allow-Methods header R2 sets when
       * requesting objects in a bucket from a browser.
       */
      methods: Array<'GET' | 'PUT' | 'POST' | 'DELETE' | 'HEAD'>;

      /**
       * Specifies the value for the Access-Control-Allow-Origin header R2 sets when
       * requesting objects in a bucket from a browser.
       */
      origins: Array<string>;

      /**
       * Specifies the value for the Access-Control-Allow-Headers header R2 sets when
       * requesting objects in this bucket from a browser. Cross-origin requests that
       * include custom headers (e.g. x-user-id) should specify these headers as
       * AllowedHeaders.
       */
      headers?: Array<string>;
    }
  }
}

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

  /**
   * Body param
   */
  rules?: Array<CORSUpdateParams.Rule>;

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

export namespace CORSUpdateParams {
  export interface Rule {
    /**
     * Object specifying allowed origins, methods and headers for this CORS rule.
     */
    allowed: Rule.Allowed;

    /**
     * Identifier for this rule.
     */
    id?: string;

    /**
     * Specifies the headers that can be exposed back, and accessed by, the JavaScript
     * making the cross-origin request. If you need to access headers beyond the
     * safelisted response headers, such as Content-Encoding or cf-cache-status, you
     * must specify it here.
     */
    exposeHeaders?: Array<string>;

    /**
     * Specifies the amount of time (in seconds) browsers are allowed to cache CORS
     * preflight responses. Browsers may limit this to 2 hours or less, even if the
     * maximum value (86400) is specified.
     */
    maxAgeSeconds?: number;
  }

  export namespace Rule {
    /**
     * Object specifying allowed origins, methods and headers for this CORS rule.
     */
    export interface Allowed {
      /**
       * Specifies the value for the Access-Control-Allow-Methods header R2 sets when
       * requesting objects in a bucket from a browser.
       */
      methods: Array<'GET' | 'PUT' | 'POST' | 'DELETE' | 'HEAD'>;

      /**
       * Specifies the value for the Access-Control-Allow-Origin header R2 sets when
       * requesting objects in a bucket from a browser.
       */
      origins: Array<string>;

      /**
       * Specifies the value for the Access-Control-Allow-Headers header R2 sets when
       * requesting objects in this bucket from a browser. Cross-origin requests that
       * include custom headers (e.g. x-user-id) should specify these headers as
       * AllowedHeaders.
       */
      headers?: Array<string>;
    }
  }
}

export interface CORSDeleteParams {
  /**
   * 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 interface CORSGetParams {
  /**
   * 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 CORS {
  export {
    type CORSUpdateResponse as CORSUpdateResponse,
    type CORSDeleteResponse as CORSDeleteResponse,
    type CORSGetResponse as CORSGetResponse,
    type CORSUpdateParams as CORSUpdateParams,
    type CORSDeleteParams as CORSDeleteParams,
    type CORSGetParams as CORSGetParams,
  };
}
