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

import { APIResource } from '../../../core/resource';
import * as ScriptsAPI from './scripts';
import * as TailAPI from './tail';
import { APIPromise } from '../../../core/api-promise';
import { RequestOptions } from '../../../internal/request-options';
import { path } from '../../../internal/utils/path';

export class BaseSettings extends APIResource {
  static override readonly _key: readonly ['workers', 'scripts', 'settings'] = Object.freeze([
    'workers',
    'scripts',
    'settings',
  ] as const);

  /**
   * Patch Worker script-level settings when using
   * [Worker Versions](https://developers.cloudflare.com/api/operations/worker-versions-list-versions).
   * Including but not limited to Logpush and Tail Consumers.
   *
   * @example
   * ```ts
   * const scriptSetting =
   *   await client.workers.scripts.settings.edit(
   *     'this-is_my_script-01',
   *     { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  edit(
    scriptName: string,
    params: SettingEditParams,
    options?: RequestOptions,
  ): APIPromise<ScriptsAPI.ScriptSetting> {
    const { account_id, ...body } = params;
    return (
      this._client.patch(path`/accounts/${account_id}/workers/scripts/${scriptName}/script-settings`, {
        body,
        ...options,
      }) as APIPromise<{ result: ScriptsAPI.ScriptSetting }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Get Worker script-level settings when using
   * [Worker Versions](https://developers.cloudflare.com/api/operations/worker-versions-list-versions).
   * Includes Logpush and Tail Consumers.
   *
   * @example
   * ```ts
   * const scriptSetting =
   *   await client.workers.scripts.settings.get(
   *     'this-is_my_script-01',
   *     { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  get(
    scriptName: string,
    params: SettingGetParams,
    options?: RequestOptions,
  ): APIPromise<ScriptsAPI.ScriptSetting> {
    const { account_id } = params;
    return (
      this._client.get(
        path`/accounts/${account_id}/workers/scripts/${scriptName}/script-settings`,
        options,
      ) as APIPromise<{ result: ScriptsAPI.ScriptSetting }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class Settings extends BaseSettings {}

export interface SettingEditParams {
  /**
   * Path param: Identifier.
   */
  account_id: string;

  /**
   * Body param: Whether Logpush is turned on for the Worker.
   */
  logpush?: boolean;

  /**
   * Body param: Observability settings for the Worker.
   */
  observability?: SettingEditParams.Observability | null;

  /**
   * Body param: Tags associated with the Worker.
   */
  tags?: Array<string> | null;

  /**
   * Body param: List of Workers that will consume logs from the attached Worker.
   */
  tail_consumers?: Array<TailAPI.ConsumerScriptParam> | null;
}

export namespace SettingEditParams {
  /**
   * Observability settings for the Worker.
   */
  export interface Observability {
    /**
     * Whether observability is enabled for the Worker.
     */
    enabled: boolean;

    /**
     * The sampling rate for incoming requests. From 0 to 1 (1 = 100%, 0.1 = 10%).
     * Default is 1.
     */
    head_sampling_rate?: number | null;

    /**
     * Real-time Issues settings for the Worker.
     */
    issues?: Observability.Issues | null;

    /**
     * Log settings for the Worker.
     */
    logs?: Observability.Logs | null;

    /**
     * Whether query strings are removed from request URLs in logs and traces.
     */
    redact_query_string?: boolean;

    /**
     * Trace settings for the Worker.
     */
    traces?: Observability.Traces | null;
  }

  export namespace Observability {
    /**
     * Real-time Issues settings for the Worker.
     */
    export interface Issues {
      /**
       * Whether real-time Issues are enabled for the Worker.
       */
      enabled?: boolean;
    }

    /**
     * Log settings for the Worker.
     */
    export interface Logs {
      /**
       * Whether logs are enabled for the Worker.
       */
      enabled: boolean;

      /**
       * Whether
       * [invocation logs](https://developers.cloudflare.com/workers/observability/logs/workers-logs/#invocation-logs)
       * are enabled for the Worker.
       */
      invocation_logs: boolean;

      /**
       * A list of destinations where logs will be exported to.
       */
      destinations?: Array<string>;

      /**
       * The sampling rate for logs. From 0 to 1 (1 = 100%, 0.1 = 10%). Default is 1.
       */
      head_sampling_rate?: number | null;

      /**
       * Whether log persistence is enabled for the Worker.
       */
      persist?: boolean;
    }

    /**
     * Trace settings for the Worker.
     */
    export interface Traces {
      /**
       * A list of destinations where traces will be exported to.
       */
      destinations?: Array<string>;

      /**
       * Whether traces are enabled for the Worker.
       */
      enabled?: boolean;

      /**
       * The sampling rate for traces. From 0 to 1 (1 = 100%, 0.1 = 10%). Default is 1.
       */
      head_sampling_rate?: number | null;

      /**
       * Whether trace persistence is enabled for the Worker.
       */
      persist?: boolean;

      /**
       * Controls how inbound trace context (traceparent/tracestate) headers on incoming
       * requests are handled. "authenticated" honors inbound trace context only when
       * accompanied by a valid trace auth token. "accept" unconditionally accepts
       * inbound trace context. Requires the trace propagation feature to be enabled.
       * Returns null when the trace propagation feature is not enabled for the account.
       */
      propagation_policy?: 'authenticated' | 'accept' | null;
    }
  }
}

export interface SettingGetParams {
  /**
   * Identifier.
   */
  account_id: string;
}

export declare namespace Settings {
  export { type SettingEditParams as SettingEditParams, type SettingGetParams as SettingGetParams };
}
