import { readFileSync } from 'node:fs';
import { gunzipSync } from 'node:zlib';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
/**
 * Expands a compact record into a full AircraftRegistration.
 *
 * @param icaoHex - The ICAO hex key from the compact records object.
 * @param compact - The compact record to expand.
 * @returns A full AircraftRegistration object.
 */
function expandRecord(icaoHex, compact) {
    const record = {
        icaoHex,
        registration: compact.r,
    };
    if (compact.mk !== undefined) {
        record.make = compact.mk;
    }
    if (compact.md !== undefined) {
        record.model = compact.md;
    }
    if (compact.op !== undefined) {
        record.operator = compact.op;
    }
    if (compact.at !== undefined) {
        record.aircraftType = compact.at;
    }
    if (compact.et !== undefined) {
        record.engineType = compact.et;
    }
    if (compact.yr !== undefined) {
        record.yearManufactured = compact.yr;
    }
    return record;
}
const dataPath = resolve(dirname(fileURLToPath(import.meta.url)), '../data/icao-registry.json.gz');
const raw = JSON.parse(gunzipSync(readFileSync(dataPath)).toString('utf-8'));
const records = [];
for (const [icaoHex, compact] of Object.entries(raw.records)) {
    records.push(expandRecord(icaoHex, compact));
}
/**
 * Pre-processed snapshot of US aircraft registrations derived from the
 * FAA ReleasableAircraft database.
 *
 * Contains registration, make, model, operator, aircraft type, engine type,
 * and year of manufacture for all US-registered aircraft with an assigned
 * ICAO hex address.
 *
 * Pass the `records` array directly to `createIcaoRegistry()` from
 * `@squawk/icao-registry` for zero-config lookups:
 *
 * ```typescript
 * import { usBundledRegistry } from '@squawk/icao-registry-data';
 * import { createIcaoRegistry } from '@squawk/icao-registry';
 *
 * const registry = createIcaoRegistry({ data: usBundledRegistry.records });
 * ```
 */
export const usBundledRegistry = {
    properties: {
        generatedAt: raw.meta.generatedAt,
        recordCount: raw.meta.recordCount,
    },
    records,
};
