import { Temporal } from "@js-temporal/polyfill";
import { URLPattern } from "urlpattern-polyfill";
import { KvKey, KvStore } from "./kv-DRaeSXco.js";

//#region runtime/docloader.d.ts
/**
 * A remote JSON-LD document and its context fetched by
 * a {@link DocumentLoader}.
 */
interface RemoteDocument {
  /**
   * The URL of the context document.
   */
  contextUrl: string | null;
  /**
   * The fetched JSON-LD document.
   */
  document: unknown;
  /**
   * The URL of the fetched document.
   */
  documentUrl: string;
}
/**
 * A JSON-LD document loader that fetches documents from the Web.
 * @param url The URL of the document to load.
 * @returns The loaded remote document.
 */
type DocumentLoader = (url: string) => Promise<RemoteDocument>;
/**
 * A factory function that creates a {@link DocumentLoader} with options.
 * @param options The options for the document loader.
 * @returns The document loader.
 * @since 1.4.0
 */
type DocumentLoaderFactory = (options?: DocumentLoaderFactoryOptions) => DocumentLoader;
/**
 * Options for {@link DocumentLoaderFactory}.
 * @see {@link DocumentLoaderFactory}
 * @see {@link AuthenticatedDocumentLoaderFactory}
 * @since 1.4.0
 */
interface DocumentLoaderFactoryOptions {
  /**
   * Whether to allow fetching private network addresses.
   * Turned off by default.
   * @default `false``
   */
  allowPrivateAddress?: boolean;
  /**
   * Options for making `User-Agent` string.
   * If a string is given, it is used as the `User-Agent` header value.
   * If an object is given, it is passed to {@link getUserAgent} function.
   */
  userAgent?: GetUserAgentOptions | string;
}
/**
 * A factory function that creates an authenticated {@link DocumentLoader} for
 * a given identity.  This is used for fetching documents that require
 * authentication.
 * @param identity The identity to create the document loader for.
 *                 The actor's key pair.
 * @param options The options for the document loader.
 * @returns The authenticated document loader.
 * @since 0.4.0
 */
type AuthenticatedDocumentLoaderFactory = (identity: {
  keyId: URL;
  privateKey: CryptoKey;
}, options?: DocumentLoaderFactoryOptions) => DocumentLoader;
/**
 * Error thrown when fetching a JSON-LD document failed.
 */
declare class FetchError extends Error {
  /**
   * The URL that failed to fetch.
   */
  url: URL;
  /**
   * Constructs a new `FetchError`.
   *
   * @param url The URL that failed to fetch.
   * @param message Error message.
   */
  constructor(url: URL | string, message?: string);
}
/**
 * Options for creating a request.
 * @internal
 */

/**
 * Options for {@link getDocumentLoader}.
 * @since 1.3.0
 */
interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
  /**
   * Whether to preload the frequently used contexts.
   */
  skipPreloadedContexts?: boolean;
}
/**
 * Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
 *
 * The created loader preloads the below frequently used contexts by default
 * (unless `options.ignorePreloadedContexts` is set to `true`):
 *
 * - <https://www.w3.org/ns/activitystreams>
 * - <https://w3id.org/security/v1>
 * - <https://w3id.org/security/data-integrity/v1>
 * - <https://www.w3.org/ns/did/v1>
 * - <https://w3id.org/security/multikey/v1>
 * - <https://purl.archive.org/socialweb/webfinger>
 * - <http://schema.org/>
 * @param options Options for the document loader.
 * @returns The document loader.
 * @since 1.3.0
 */
declare function getDocumentLoader({
  allowPrivateAddress,
  skipPreloadedContexts,
  userAgent
}?: GetDocumentLoaderOptions): DocumentLoader;
/**
 * A JSON-LD document loader that utilizes the browser's `fetch` API.
 *
 * This loader preloads the below frequently used contexts:
 *
 * - <https://www.w3.org/ns/activitystreams>
 * - <https://w3id.org/security/v1>
 * - <https://w3id.org/security/data-integrity/v1>
 * - <https://www.w3.org/ns/did/v1>
 * - <https://w3id.org/security/multikey/v1>
 * - <https://purl.archive.org/socialweb/webfinger>
 * - <http://schema.org/>
 * @param url The URL of the document to load.
 * @param allowPrivateAddress Whether to allow fetching private network
 *                            addresses.  Turned off by default.
 * @returns The remote document.
 * @deprecated Use {@link getDocumentLoader} instead.
 */
declare function fetchDocumentLoader(url: string, allowPrivateAddress?: boolean): Promise<RemoteDocument>;
/**
 * The parameters for {@link kvCache} function.
 */
interface KvCacheParameters {
  /**
   * The document loader to decorate with a cache.
   */
  loader: DocumentLoader;
  /**
   * The key–value store to use for backing the cache.
   */
  kv: KvStore;
  /**
   * The key prefix to use for namespacing the cache.
   * `["_fedify", "remoteDocument"]` by default.
   */
  prefix?: KvKey;
  /**
   * The per-URL cache rules in the array of `[urlPattern, duration]` pairs
   * where `urlPattern` is either a string, a {@link URL}, or
   * a {@link URLPattern} and `duration` is a {@link Temporal.Duration}.
   * The `duration` is allowed to be at most 30 days.
   *
   * By default, 5 minutes for all URLs.
   */
  rules?: [string | URL | URLPattern, Temporal.Duration][];
}
/**
 * Decorates a {@link DocumentLoader} with a cache backed by a {@link Deno.Kv}.
 * @param parameters The parameters for the cache.
 * @returns The decorated document loader which is cache-enabled.
 */
declare function kvCache({
  loader,
  kv,
  prefix,
  rules
}: KvCacheParameters): DocumentLoader;
/**
 * Options for making `User-Agent` string.
 * @see {@link getUserAgent}
 * @since 1.3.0
 */
interface GetUserAgentOptions {
  /**
   * An optional software name and version, e.g., `"Hollo/1.0.0"`.
   */
  software?: string | null;
  /**
   * An optional URL to append to the user agent string.
   * Usually the URL of the ActivityPub instance.
   */
  url?: string | URL | null;
}
/**
 * Gets the user agent string for the given application and URL.
 * @param options The options for making the user agent string.
 * @returns The user agent string.
 * @since 1.3.0
 */
declare function getUserAgent({
  software,
  url
}?: GetUserAgentOptions): string;
//#endregion
export { AuthenticatedDocumentLoaderFactory, DocumentLoader, DocumentLoaderFactory, DocumentLoaderFactoryOptions, FetchError, GetDocumentLoaderOptions, GetUserAgentOptions, KvCacheParameters, RemoteDocument, fetchDocumentLoader, getDocumentLoader, getUserAgent, kvCache };