import { ShardLoader } from './shardLoader.js';
import { normalizePostcode, parseHuisnummer } from './format.js';
export { isValidPostcodeFormat, normalizePostcode, parseHuisnummer } from './format.js';
function entryToAddress(postcode, entry) {
    return {
        postcode,
        straat: entry.s,
        woonplaats: entry.w,
        gemeente: entry.g,
        provincie: entry.p,
    };
}
/**
 * Postcode-level validator backed by JSON shards bundled inside the npm package.
 * Requires no setup, no environment variables, and no network access. Works
 * on local Node, Vercel/AWS Lambda serverless, and anywhere Node >=20 runs.
 */
export class PostcodeValidator {
    loader;
    constructor(options = {}) {
        this.loader = new ShardLoader(options.dataDir);
    }
    /** Number of postcodes in the bundled dataset. Reads `manifest.json` once. */
    count() {
        return this.loader.getManifest()?.totalPostcodes ?? 0;
    }
    /** Version tag of the bundled dataset, or `null` if no manifest is present. */
    dataVersion() {
        return this.loader.getManifest()?.version ?? null;
    }
    /**
     * Look up the address for a postcode. Returns an array (0 or 1 entries)
     * to preserve the v0.x signature; `huisnummer` and lookup options are
     * accepted but ignored at the postcode level.
     */
    lookup(postcode, _huisnummer, _options) {
        const pc = normalizePostcode(postcode);
        if (!pc)
            return [];
        const entry = this.loader.loadShard(pc.slice(4, 6))[pc];
        return entry ? [entryToAddress(pc, entry)] : [];
    }
    /**
     * Exact single-result variant of {@link lookup}. Returns `null` when the
     * postcode is unknown or malformed.
     */
    findExact(postcode, _huisnummer, _options) {
        const pc = normalizePostcode(postcode);
        if (!pc)
            return null;
        const entry = this.loader.loadShard(pc.slice(4, 6))[pc];
        return entry ? entryToAddress(pc, entry) : null;
    }
    /** Cheap boolean existence check. */
    exists(postcode) {
        const pc = normalizePostcode(postcode);
        if (!pc)
            return false;
        return this.loader.loadShard(pc.slice(4, 6))[pc] !== undefined;
    }
    /**
     * Validate a postcode. The optional `huisnummer` is accepted for
     * backwards compatibility with the v0.x SQLite-backed API; it is
     * ignored beyond a malformedness check.
     */
    validate(postcode, huisnummer, _options = {}) {
        const pc = normalizePostcode(postcode);
        if (!pc) {
            return { valid: false, normalizedPostcode: null, matches: [], reason: 'invalid_format' };
        }
        if (huisnummer !== undefined && huisnummer !== null && parseHuisnummer(huisnummer) === null) {
            return { valid: false, normalizedPostcode: pc, matches: [], reason: 'invalid_huisnummer' };
        }
        const entry = this.loader.loadShard(pc.slice(4, 6))[pc];
        if (!entry) {
            return { valid: false, normalizedPostcode: pc, matches: [], reason: 'postcode_not_found' };
        }
        return {
            valid: true,
            normalizedPostcode: pc,
            matches: [entryToAddress(pc, entry)],
            reason: 'ok',
        };
    }
    /**
     * Retained for API compatibility with the v0.x SQLite-backed validator.
     * There is no open handle to close in this implementation.
     */
    close() {
        this.loader.clearCache();
    }
}
/**
 * Convenience factory for {@link PostcodeValidator}. Accepts no required
 * arguments: `createValidator()` is enough.
 */
export function createValidator(options = {}) {
    return new PostcodeValidator(options);
}
//# sourceMappingURL=index.js.map