import { Config } from "@oclif/core";
import { InvocationSource } from "@sanity/cli-core/commandPolicy";
import { SanityEnvironment } from "@sanity/cli-core/executionContext";
/**
 * Valid body types for fetch requests.
 *
 * Includes `Uint8Array` since the standard `fetch` accepts typed-array
 * request bodies. It is pinned to `Uint8Array<ArrayBuffer>` (rather than the
 * abstract `ArrayBufferView` or the default `Uint8Array<ArrayBufferLike>`) to
 * stay assignable to the platform `BodyInit` across the DOM and Node lib
 * definitions, which keeps `FetchFunction` compatible with the runtime
 * `fetch`. Widening further breaks that compatibility.
 *
 * @public
 */
type FetchBody = string | ArrayBuffer | Uint8Array<ArrayBuffer> | Blob | FormData | URLSearchParams | ReadableStream;
/**
 * Accepted header formats — tuple pairs, a plain record, or a `Headers` instance.
 *
 * @public
 */
type FetchHeaders = [string, string][] | Record<string, string> | Headers;
/**
 * Init options passed to a {@link FetchFunction}.
 *
 * This is a minimal subset of the standard `RequestInit` so the library
 * doesn't depend on the full DOM lib.
 *
 * @public
 */
interface FetchInit {
  /** HTTP method (GET, POST, PUT, etc.). */
  method?: string;
  /** Request headers. */
  headers?: FetchHeaders;
  /** Request body. */
  body?: FetchBody | null;
  /** Abort signal for cancellation. */
  signal?: AbortSignal;
  /**
   * Redirect handling strategy.
   *
   * ⚠️ In browsers, `'manual'` yields an opaque-redirect response (status `0`,
   * empty headers) per the Fetch spec — you cannot read the status or headers
   * (e.g. `location`) of the 3xx response. Reading them throws nothing and warns
   * nothing: `headers.get()` returns `null` and iteration is empty. Detect it via
   * `status === 0`. Non-browser runtimes (Node.js, Bun, Deno, edge runtimes,
   * workers) return the real 3xx response, so its status and headers are readable.
   */
  redirect?: 'error' | 'follow' | 'manual';
  /** Credentials mode (browser-only — some runtimes crash if set). */
  credentials?: 'include' | 'omit' | 'same-origin';
}
/**
 * Response object returned by a {@link FetchFunction}.
 *
 * A minimal subset of the standard `Response` — just the properties
 * get-it actually reads.
 *
 * @public
 */
interface FetchResponse {
  /** Whether the response status is in the 200–299 range. */
  ok: boolean;
  /** HTTP status code. */
  status: number;
  /** HTTP status text (e.g. "OK", "Not Found"). */
  statusText: string;
  /** Response headers. */
  headers: Headers;
  /** The final URL after any redirects. */
  url: string;
  /** Whether the response was the result of a redirect. */
  redirected: boolean;
  /** Readable stream of the response body, or `null` if empty. */
  body: ReadableStream<Uint8Array> | null;
  /** Reads the entire body as a string. */
  text(): Promise<string>;
  /** Reads the entire body as an ArrayBuffer. */
  arrayBuffer(): Promise<ArrayBuffer>;
}
/**
 * A function that performs an HTTP fetch. Must conform to the standard
 * `fetch()` signature but only needs to return a `FetchResponse`.
 *
 * @param input - The URL to fetch.
 * @param init - Optional request init options.
 * @returns A promise resolving to the response.
 *
 * @public
 */
type FetchFunction = (input: string, init?: FetchInit) => Promise<FetchResponse>;
/**
 * @internal
 */
interface InvokeSanityCliOptions {
  /**
   * Arguments after `sanity` (a leading `sanity` token is tolerated), either
   * as a single string — shell-style quoting is supported, but no shell is
   * ever executed — or as a pre-split argv array.
   */
  args: string | string[];
  /**
   * Where this invocation originates. Selects the command policy to enforce:
   * which commands are invokable and which invocations of them are permitted.
   */
  source: InvocationSource;
  /**
   * Auth token for this invocation. Scoped to this call via the CLI execution
   * context: it never touches process env or the process-wide token cache, so
   * concurrent invocations with different tokens are fully isolated.
   */
  token: string;
  /**
   * Optional oclif config override (mainly for tests). Defaults to this
   * package's config, loaded once and cached across invocations.
   */
  config?: Config;
  /**
   * Optional fetch implementation for API requests made by this invocation.
   * Scoped to this call via the CLI execution context; defaults to the CLI's
   * own transport. The execution context's transport hygiene (such as
   * stripping the embedding process's lineage header) is applied on top.
   */
  fetch?: FetchFunction;
  /**
   * Sanity deployment environment for this invocation. Scoped to this call
   * via the CLI execution context and defaults to production. The embedding
   * process's `SANITY_INTERNAL_ENV` is never consulted.
   */
  sanityEnv?: SanityEnvironment;
}
/**
 * @internal
 */
interface InvokeSanityCliResult {
  /** `0` on success, the command's exit code otherwise. */
  exitCode: number;
  /** Combined stdout and stderr output, in emission order. */
  output: string;
  /**
   * Canonical oclif command id when the invocation resolved to a command
   * exposed by the selected policy (for example, `datasets:create`).
   */
  commandId?: string;
}
/**
 * Run a policy-permitted CLI command in-process and capture its result.
 *
 * Command-level failures (unknown command, bad flags, API errors) are
 * reported through `exitCode`/`output` rather than thrown, so callers can
 * relay them verbatim.
 *
 * @internal
 */
declare function invokeSanityCli(options: InvokeSanityCliOptions): Promise<InvokeSanityCliResult>;
export { InvokeSanityCliOptions, InvokeSanityCliResult, invokeSanityCli };
//# sourceMappingURL=index.d.ts.map