/**
 * @copyright Sister Software
 * @license AGPL-3.0
 * @author Teffen Ellis, et al.
 */
import { WordClassifier } from "mailwoman/core";
import { repoRootPathBuilder } from "mailwoman/sdk/repo";
import { readFile } from "node:fs/promises";
const dictPath = repoRootPathBuilder("resources", "chromium-i18n", "ssl-address");
const DefaultPostcodeCountries = [
    // ---
    "US",
    "GB",
    "FR",
    "DE",
    "ES",
    "PT",
    "AU",
    "NZ",
    "KR",
    "JP",
    "IN",
    "RU",
    "BR",
    "NL",
    "PL",
];
export class PostcodeClassifier extends WordClassifier {
    postcodePatterns = [];
    countryCodes = [];
    constructor(specs = DefaultPostcodeCountries) {
        super();
        this.countryCodes = specs;
    }
    async ready() {
        const patterns = await Promise.all(Iterator.from(this.countryCodes).map(async (cc) => {
            const countryDictPath = dictPath(`${cc.toUpperCase()}.json`);
            const spec = await readFile(countryDictPath, "utf8").then(JSON.parse);
            const pattern = new RegExp("^(" + spec.zip + ")$", "i");
            return pattern;
        }));
        this.postcodePatterns = patterns
            // remove countries with 3-digit postcodes
            .filter((row) => !row.test("100"));
        return this;
    }
    explore(span) {
        if (!span.flags.has("numeral"))
            return;
        // Skip postcodes in the start position unless it's the only token in the section.
        if (span.is("start_token") && (span.previousSiblings.size > 0 || span.nextSiblings.size > 0)) {
            return;
        }
        for (const pattern of this.postcodePatterns) {
            if (pattern.test(span.normalized)) {
                span.classifications.add("postcode");
                break;
            }
        }
    }
}
//# sourceMappingURL=PostcodeClassifier.js.map