import {
  Agent,
  EnvHttpProxyAgent,
  fetch as undiciFetch,
  FormData as UndiciFormData,
  ProxyAgent,
} from 'undici'
import type Dispatcher from 'undici/types/dispatcher'

import type {FetchFunction, FetchInit, FetchResponse} from './types'

/**
 * TLS configuration for mutual TLS (client certificates).
 *
 * @public
 */
export interface TlsOptions {
  /** Client certificate (PEM-encoded) */
  cert?: string | Buffer
  /** Client private key (PEM-encoded) */
  key?: string | Buffer
  /** Certificate authority (PEM-encoded) — use for self-signed server certs */
  ca?: string | Buffer
}

/**
 * Options for {@link createNodeFetch}.
 *
 * @public
 */
export interface NodeFetchOptions {
  /** true = read proxy from env (default), string = explicit proxy URL, false = no proxy */
  proxy?: string | boolean
  /** Maximum number of connections per origin */
  connections?: number
  /**
   * Enable HTTP/2 support. Defaults to `false` (HTTP/1.1 only). We opt out
   * unless explicitly enabled so the transport stays predictable across
   * undici versions regardless of their default negotiation behavior.
   */
  allowH2?: boolean
  /** TLS options for mutual TLS (client certificates) */
  tls?: TlsOptions
}

/**
 * Creates a `FetchFunction` backed by an undici dispatcher.
 *
 * - `proxy: true` (default) — uses `EnvHttpProxyAgent` which reads
 *   `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY` from the environment.
 * - `proxy: "<url>"` — uses `ProxyAgent` with an explicit proxy URL.
 * - `proxy: false` — uses a plain `Agent` with no proxy.
 *
 * Since get-it v9.6.0, explicit `Host` headers are preserved in Node.js for
 * virtual-host routing. The override is discarded when a redirect crosses
 * origins, including on any later return to the original origin.
 * @public
 */
export function createNodeFetch(options?: NodeFetchOptions): FetchFunction {
  const proxyOption = options?.proxy ?? true

  let dispatcher: Dispatcher

  const tls = options?.tls
  // Default to HTTP/1.1-only; opt into HTTP/2 only when explicitly enabled so
  // the transport stays predictable regardless of undici's default negotiation.
  const allowH2 = options?.allowH2 ?? false

  if (proxyOption === true) {
    dispatcher = new EnvHttpProxyAgent({
      connections: options?.connections,
      allowH2,
      requestTls: tls ? {cert: tls.cert, key: tls.key, ca: tls.ca} : undefined,
    })
  } else if (typeof proxyOption === 'string') {
    dispatcher = new ProxyAgent({
      uri: proxyOption,
      connections: options?.connections,
      allowH2,
      requestTls: tls ? {cert: tls.cert, key: tls.key, ca: tls.ca} : undefined,
    })
  } else {
    dispatcher = new Agent({
      connections: options?.connections,
      allowH2,
      connect: tls ? {cert: tls.cert, key: tls.key, ca: tls.ca} : undefined,
    })
  }

  // Streamed request bodies require `duplex: 'half'` in undici/Node —
  // omitting it throws "duplex option is required when sending a body".
  // Typed explicitly so the literal isn't widened to `string`.
  const halfDuplex: {duplex: 'half'} = {duplex: 'half'}

  return async function nodeFetch(input: string, reqInit?: FetchInit): Promise<FetchResponse> {
    const {body: inputBody, ...rest} = reqInit ?? {}
    const body = normalizeBody(inputBody)
    const host = reqInit?.headers ? new Headers(reqInit.headers).get('host') : null
    const init = {
      ...rest,
      dispatcher: host === null ? dispatcher : preserveHost(dispatcher, input, host),
      // Only set body when it's defined — fetch _can_ throw on
      // `body: undefined` for some request methods in strict mode.
      ...(body === undefined ? {} : {body}),
      ...(body instanceof ReadableStream ? halfDuplex : {}),
    }
    const response = await undiciFetch(input, init)
    return adaptResponse(response)
  }
}

function preserveHost(dispatcher: Dispatcher, input: string, host: string): Dispatcher {
  // Bun's built-in undici shim has no dispatcher interception. Its native fetch
  // already accepts Host; keep its existing behavior, including redirects.
  if (typeof dispatcher.compose !== 'function') return dispatcher

  const origin = new URL(input).origin
  let sameOrigin = true

  // Fetch removes Host before dispatch. Restore it at the transport boundary
  // so fetch still owns body consumption, decompression, redirects and aborts.
  // This interceptor belongs to one fetch call, not the shared connection pool.
  return dispatcher.compose((dispatch) => (opts, handler) => {
    if (opts.origin === undefined || new URL(opts.origin).origin !== origin) {
      sameOrigin = false
    }
    if (!sameOrigin) return dispatch(opts, handler)

    const headers = opts.headers
    if (Array.isArray(headers)) {
      return dispatch({...opts, headers: [...headers, 'host', host]}, handler)
    }
    const entries = headers && Symbol.iterator in headers ? headers : Object.entries(headers ?? {})
    return dispatch({...opts, headers: {...Object.fromEntries(entries), host}}, handler)
  })
}

function normalizeBody(body: FetchInit['body']) {
  // Undici only recognizes its own FormData class; native FormData would
  // otherwise be sent as the string "[object FormData]".
  if (!(body instanceof FormData) || body instanceof UndiciFormData) return body

  const form = new UndiciFormData()
  for (const [name, value] of body) {
    form.append(name, value)
  }
  return form
}

/**
 * The subset of Response properties we actually use. Typed structurally to
 * avoid coupling to undici's `Response` type and any version skew between
 * undici and the `undici-types` bundled by @types/node.
 */
interface FetchResponseLike {
  ok: boolean
  status: number
  statusText: string
  headers: Headers
  url: string
  redirected: boolean
  body: ReadableStream<Uint8Array> | null
  text(): Promise<string>
  arrayBuffer(): Promise<ArrayBuffer>
}

/**
 * Adapts a fetch `Response` into our `FetchResponse` interface.
 */
function adaptResponse(response: FetchResponseLike): FetchResponse {
  return {
    ok: response.ok,
    status: response.status,
    statusText: response.statusText,
    headers: response.headers,
    url: response.url,
    redirected: response.redirected,
    body: response.body,
    text: () => response.text(),
    arrayBuffer: () => response.arrayBuffer(),
  }
}
