import { readFileSync } from 'node:fs';
import { gunzipSync } from 'node:zlib';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
/**
 * Expands a compact ILS into a full IlsSystem.
 */
function expandIls(c) {
    const ils = { systemType: c.st };
    if (c.id !== undefined) {
        ils.identifier = c.id;
    }
    if (c.cat !== undefined) {
        ils.category = c.cat;
    }
    if (c.lf !== undefined) {
        ils.localizerFrequencyMhz = c.lf;
    }
    if (c.lc !== undefined) {
        ils.localizerMagneticCourseDeg = c.lc;
    }
    if (c.ga !== undefined) {
        ils.glideSlopeAngleDeg = c.ga;
    }
    if (c.gt !== undefined) {
        ils.glideSlopeType = c.gt;
    }
    if (c.dc !== undefined) {
        ils.dmeChannel = c.dc;
    }
    return ils;
}
/**
 * Expands a compact runway end into a full RunwayEnd.
 */
function expandRunwayEnd(c) {
    const end = { id: c.id };
    if (c.hdg !== undefined) {
        end.trueHeadingDeg = c.hdg;
    }
    if (c.ils !== undefined) {
        end.ils = expandIls(c.ils);
    }
    if (c.rht) {
        end.rightTraffic = true;
    }
    if (c.mt !== undefined) {
        end.markingType = c.mt;
    }
    if (c.mc !== undefined) {
        end.markingCondition = c.mc;
    }
    if (c.lat !== undefined) {
        end.lat = c.lat;
    }
    if (c.lon !== undefined) {
        end.lon = c.lon;
    }
    if (c.el !== undefined) {
        end.elevationFt = c.el;
    }
    if (c.tch !== undefined) {
        end.thresholdCrossingHeightFt = c.tch;
    }
    if (c.gpa !== undefined) {
        end.glidepathAngleDeg = c.gpa;
    }
    if (c.dt !== undefined) {
        end.displacedThresholdFt = c.dt;
    }
    if (c.dte !== undefined) {
        end.displacedThresholdElevationFt = c.dte;
    }
    if (c.tdz !== undefined) {
        end.tdzElevationFt = c.tdz;
    }
    if (c.vg !== undefined) {
        end.vgsiType = c.vg;
    }
    if (c.rvr) {
        end.hasRvr = true;
    }
    if (c.al !== undefined) {
        end.approachLights = c.al;
    }
    if (c.reil) {
        end.hasReil = true;
    }
    if (c.cl) {
        end.hasCenterlineLights = true;
    }
    if (c.tl) {
        end.hasTdzLights = true;
    }
    if (c.tora !== undefined) {
        end.toraFt = c.tora;
    }
    if (c.toda !== undefined) {
        end.todaFt = c.toda;
    }
    if (c.asda !== undefined) {
        end.asdaFt = c.asda;
    }
    if (c.lda !== undefined) {
        end.ldaFt = c.lda;
    }
    if (c.lahso !== undefined) {
        end.lahsoDistanceFt = c.lahso;
    }
    if (c.lahsoE !== undefined) {
        end.lahsoEntity = c.lahsoE;
    }
    return end;
}
/**
 * Expands a compact runway into a full Runway.
 */
function expandRunway(c) {
    const rwy = { id: c.id, ends: c.ends.map(expandRunwayEnd) };
    if (c.len !== undefined) {
        rwy.lengthFt = c.len;
    }
    if (c.w !== undefined) {
        rwy.widthFt = c.w;
    }
    if (c.sfc !== undefined) {
        rwy.surfaceType = c.sfc;
    }
    if (c.cond !== undefined) {
        rwy.condition = c.cond;
    }
    if (c.trt !== undefined) {
        rwy.treatment = c.trt;
    }
    if (c.pcn !== undefined) {
        rwy.pcn = c.pcn;
    }
    if (c.lgt !== undefined) {
        rwy.lighting = c.lgt;
    }
    if (c.wsw !== undefined) {
        rwy.weightLimitSingleWheelKlb = c.wsw;
    }
    if (c.wdw !== undefined) {
        rwy.weightLimitDualWheelKlb = c.wdw;
    }
    if (c.wdt !== undefined) {
        rwy.weightLimitDualTandemKlb = c.wdt;
    }
    if (c.wdd !== undefined) {
        rwy.weightLimitDdtKlb = c.wdd;
    }
    return rwy;
}
/**
 * Expands a compact frequency into a full AirportFrequency.
 */
function expandFrequency(c) {
    const freq = { frequencyMhz: c.f, use: c.u };
    if (c.s !== undefined) {
        freq.sectorization = c.s;
    }
    return freq;
}
/**
 * Expands a compact airport record into a full Airport.
 */
function expandAirport(c) {
    const apt = {
        faaId: c.id,
        name: c.nm,
        facilityType: c.ft,
        ownershipType: c.own,
        useType: c.use,
        status: c.st,
        city: c.city,
        country: c.ctry,
        lat: c.lat,
        lon: c.lon,
        runways: (c.rwys ?? []).map(expandRunway),
        frequencies: (c.freqs ?? []).map(expandFrequency),
    };
    if (c.state !== undefined) {
        apt.state = c.state;
    }
    if (c.icao !== undefined) {
        apt.icao = c.icao;
    }
    if (c.county !== undefined) {
        apt.county = c.county;
    }
    if (c.elev !== undefined) {
        apt.elevationFt = c.elev;
    }
    if (c.mv !== undefined) {
        apt.magneticVariationDeg = c.mv;
    }
    if (c.mvd !== undefined) {
        apt.magneticVariationDirection = c.mvd;
    }
    if (c.mvy !== undefined) {
        apt.magneticVariationYear = c.mvy;
    }
    if (c.tpa !== undefined) {
        apt.trafficPatternAltitudeFt = c.tpa;
    }
    if (c.chart !== undefined) {
        apt.sectionChart = c.chart;
    }
    if (c.artcc !== undefined) {
        apt.artccId = c.artcc;
    }
    if (c.twr !== undefined) {
        apt.towerType = c.twr;
    }
    if (c.fuel !== undefined) {
        apt.fuelTypes = c.fuel;
    }
    if (c.afr !== undefined) {
        apt.airframeRepair = c.afr;
    }
    if (c.ppr !== undefined) {
        apt.powerplantRepair = c.ppr;
    }
    if (c.boxo !== undefined) {
        apt.bottledOxygen = c.boxo;
    }
    if (c.bxo !== undefined) {
        apt.bulkOxygen = c.bxo;
    }
    if (c.ls !== undefined) {
        apt.lightingSchedule = c.ls;
    }
    if (c.bcn !== undefined) {
        apt.beaconColor = c.bcn;
    }
    if (c.fee) {
        apt.hasLandingFee = true;
    }
    if (c.act !== undefined) {
        apt.activationDate = c.act;
    }
    if (c.svc !== undefined) {
        apt.otherServices = c.svc;
    }
    if (c.notam !== undefined) {
        apt.notamId = c.notam;
    }
    return apt;
}
const dataPath = resolve(dirname(fileURLToPath(import.meta.url)), '../data/airports.json.gz');
const raw = JSON.parse(gunzipSync(readFileSync(dataPath)).toString('utf-8'));
const records = raw.records.map(expandAirport);
/**
 * Pre-processed snapshot of airport data derived from the FAA NASR
 * 28-day subscription cycle.
 *
 * Contains airport identification, location, elevation, runways (dimensions,
 * surface, lighting, declared distances), and communication frequencies for
 * every open aviation facility (airports, heliports, seaplane bases, etc.)
 * published by the FAA. Includes selected Canadian, Mexican, Caribbean, and
 * Pacific facilities that participate in US operations; their `state` field
 * is undefined while `country` is populated with a two-letter code.
 *
 * Pass the `records` array directly to `createAirportResolver()` from
 * `@squawk/airports` for zero-config lookups:
 *
 * ```typescript
 * import { usBundledAirports } from '@squawk/airport-data';
 * import { createAirportResolver } from '@squawk/airports';
 *
 * const resolver = createAirportResolver({ data: usBundledAirports.records });
 * ```
 */
export const usBundledAirports = {
    properties: {
        generatedAt: raw.meta.generatedAt,
        nasrCycleDate: raw.meta.nasrCycleDate,
        recordCount: raw.meta.recordCount,
    },
    records,
};
