/**
 * @copyright Sister Software
 * @license AGPL-3.0
 * @author Teffen Ellis, et al.
 */
import { isVisibleClassification } from "mailwoman/core/classification";
import { Span } from "mailwoman/core/tokenization";
import { Solution } from "./Solution.js";
import { SolutionMatch } from "./SolutionMatch.js";
export class HashMapSolver {
    static MaxMatchesPerClassification = 8;
    generateHashMap(context, includePrivate = false, includeEmpty = false) {
        const classificationSolutionMap = new Map();
        for (const section of context.sections) {
            // Multi-word phrases
            const { phrases } = section;
            for (const phrase of phrases) {
                for (const match of phrase.classifications.values()) {
                    if (!includePrivate && !isVisibleClassification(match)) {
                        continue;
                    }
                    let solution = classificationSolutionMap.get(match.classification);
                    if (!solution) {
                        solution = new Solution();
                        classificationSolutionMap.set(match.classification, solution);
                        if (includeEmpty) {
                            solution.matches.push(new SolutionMatch(new Span(), match));
                        }
                    }
                    if (solution.matches.length >= HashMapSolver.MaxMatchesPerClassification) {
                        continue;
                    }
                    solution.matches.push(new SolutionMatch(phrase, match));
                }
            }
            // Single-word spans.
            // TODO: combine with above loop, or refactor to avoid duplication.
            for (const word of section.children) {
                for (const classification of word.classifications.values()) {
                    if (!includePrivate && !isVisibleClassification(classification)) {
                        continue;
                    }
                    let solution = classificationSolutionMap.get(classification.classification);
                    if (!solution) {
                        solution = new Solution();
                        classificationSolutionMap.set(classification.classification, solution);
                        if (includeEmpty) {
                            solution.matches.push(new SolutionMatch(new Span(), classification));
                        }
                    }
                    if (solution.matches.length >= HashMapSolver.MaxMatchesPerClassification) {
                        continue;
                    }
                    solution.matches.push(new SolutionMatch(word, classification));
                }
            }
        }
        return classificationSolutionMap;
    }
}
//# sourceMappingURL=HashMapSolver.js.map