/**
 * @packageDocumentation
 * Browser / edge entry point. Asynchronously fetches, decompresses, and
 * parses the bundled `data/airports.json.gz` snapshot using Web Streams
 * (`DecompressionStream`) and the global `fetch`. Works in every evergreen
 * browser, Cloudflare Workers, Deno Deploy, and Node 22+.
 *
 * Node consumers should use the default {@link "."} entry point instead,
 * which performs the same work synchronously at module load time.
 */
/**
 * Asynchronously loads, decompresses, and parses the bundled airport
 * dataset. Returns the same `AirportDataset` shape as the Node entry
 * point exports as `usBundledAirports`.
 *
 * Handles servers that advertise transport-level gzip via
 * `Content-Encoding: gzip` (in which case `fetch()` decodes the body
 * automatically) as well as servers that serve the `.gz` as opaque bytes.
 *
 * ```typescript
 * import { loadUsBundledAirports } from '@squawk/airport-data/browser';
 * import { createAirportResolver } from '@squawk/airports';
 *
 * const dataset = await loadUsBundledAirports();
 * const resolver = createAirportResolver({ data: dataset.records });
 * ```
 *
 * To host the asset on your own CDN or to override the URL for any other
 * reason, pass an explicit `url`:
 *
 * ```typescript
 * const dataset = await loadUsBundledAirports({
 *   url: 'https://your-cdn.example/airports.json.gz',
 * });
 * ```
 */
export async function loadUsBundledAirports(options) {
    const url = options?.url ?? new URL('../data/airports.json.gz', import.meta.url);
    const fetchImpl = options?.fetch ?? globalThis.fetch;
    const res = await fetchImpl(url);
    if (!res.ok) {
        throw new Error(`Failed to fetch airport dataset from ${String(url)}: ${res.status} ${res.statusText}`);
    }
    if (res.body === null) {
        throw new Error(`Response body is null for ${String(url)}`);
    }
    const transportEncoded = res.headers.get('content-encoding')?.toLowerCase().includes('gzip') ?? false;
    const stream = transportEncoded
        ? res.body
        : res.body.pipeThrough(new DecompressionStream('gzip'));
    const text = await new Response(stream).text();
    const raw = JSON.parse(text);
    return {
        properties: raw.meta,
        records: raw.records,
    };
}
