import { readFileSync } from 'node:fs';
import { gunzipSync } from 'node:zlib';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
/**
 * Expands a compact navaid association into the full interface.
 */
function expandNavaidAssociation(c) {
    return {
        navaidId: c.nid,
        navaidType: c.ntp,
        bearingDeg: c.brg,
        distanceNm: c.dst,
    };
}
/**
 * Expands a compact fix record into the full Fix interface.
 */
function expandFix(c) {
    const fix = {
        identifier: c.id,
        icaoRegionCode: c.icao,
        state: c.st,
        country: c.ctry,
        lat: c.lat,
        lon: c.lon,
        useCode: c.uc,
        pitch: c.pit === true,
        catch: c.cat === true,
        suaAtcaa: c.sua === true,
        chartTypes: c.cht ?? [],
        navaidAssociations: c.nav ? c.nav.map(expandNavaidAssociation) : [],
    };
    if (c.hart !== undefined) {
        fix.highArtccId = c.hart;
    }
    if (c.lart !== undefined) {
        fix.lowArtccId = c.lart;
    }
    if (c.mra !== undefined) {
        fix.minimumReceptionAltitudeFt = c.mra;
    }
    if (c.cmp !== undefined) {
        fix.compulsory = c.cmp;
    }
    if (c.pid !== undefined) {
        fix.previousIdentifier = c.pid;
    }
    if (c.rmk !== undefined) {
        fix.chartingRemark = c.rmk;
    }
    return fix;
}
const dataPath = resolve(dirname(fileURLToPath(import.meta.url)), '../data/fixes.json.gz');
const raw = JSON.parse(gunzipSync(readFileSync(dataPath)).toString('utf-8'));
const records = raw.records.map(expandFix);
/**
 * Pre-processed snapshot of US fix/waypoint data derived from the FAA NASR
 * 28-day subscription cycle.
 *
 * Contains fix identification, location, usage category, ARTCC assignment,
 * chart associations, and navaid relationships for all non-CNF US named
 * fixes and waypoints.
 *
 * Pass the `records` array directly to `createFixResolver()` from
 * `@squawk/fixes` for zero-config lookups:
 *
 * ```typescript
 * import { usBundledFixes } from '@squawk/fix-data';
 * import { createFixResolver } from '@squawk/fixes';
 *
 * const resolver = createFixResolver({ data: usBundledFixes.records });
 * ```
 */
export const usBundledFixes = {
    properties: {
        generatedAt: raw.meta.generatedAt,
        nasrCycleDate: raw.meta.nasrCycleDate,
        recordCount: raw.meta.recordCount,
    },
    records,
};
