/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { HtmlParser, RecursiveVisitor, visitAll } from '@angular/compiler';
export const findElement = (template, filter) => {
    const { rootNodes, errors } = parseTemplate(template);
    // Only evaluate templates without parse errors.
    if (!errors.length) {
        const visitor = new ElementCollector(filter);
        visitAll(visitor, rootNodes);
        return visitor.matches;
    }
    return [];
};
export const findAttribute = (template, filter) => {
    const { rootNodes, errors } = parseTemplate(template);
    // Only evaluate templates without parse errors.
    if (!errors.length) {
        const visitor = new AttributeCollector(filter);
        visitAll(visitor, rootNodes);
        return visitor.matches;
    }
    return [];
};
const parseTemplate = (template) => {
    return new HtmlParser().parse(template, '', {
        // Allows for ICUs to be parsed.
        tokenizeExpansionForms: true,
        // Explicitly disable blocks so that their characters are treated as plain text.
        tokenizeBlocks: true,
        preserveLineEndings: true
    });
};
class ElementCollector extends RecursiveVisitor {
    filter;
    /**
     * All elements which match the filter
     *
     * @defaultValue []
     */
    matches = [];
    constructor(filter) {
        super();
        this.filter = filter;
    }
    visitElement(ast, context) {
        if (this.filter(ast)) {
            this.matches.push(ast);
        }
        super.visitElement(ast, context);
    }
}
class AttributeCollector extends RecursiveVisitor {
    filter;
    /**
     * All attributes which match the filter
     *
     * @defaultValue []
     */
    matches = [];
    constructor(filter) {
        super();
        this.filter = filter;
    }
    visitAttribute(ast, context) {
        if (this.filter(ast)) {
            this.matches.push(ast);
        }
        super.visitAttribute(ast, context);
    }
}
