/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import ts from 'typescript';
export const getInlineTemplates = (source) => {
    const templateNodes = [];
    const componentDecoratorVisitor = (node) => {
        if (ts.isDecorator(node) &&
            ts.isCallExpression(node.expression) &&
            ts.isIdentifier(node.expression.expression) &&
            node.expression.expression.text === 'Component') {
            const arg = node.expression.arguments[0];
            if (ts.isObjectLiteralExpression(arg)) {
                const template = arg.properties.find(property => ts.isPropertyAssignment(property) &&
                    ts.isIdentifier(property.name) &&
                    property.name.text === 'template');
                const initializer = template?.initializer;
                if (initializer &&
                    (ts.isNoSubstitutionTemplateLiteral(initializer) || ts.isStringLiteral(initializer))) {
                    templateNodes.push(initializer);
                }
            }
        }
        else {
            node.forEachChild(componentDecoratorVisitor);
        }
    };
    source.forEachChild(componentDecoratorVisitor);
    return templateNodes;
};
export const getTemplateUrl = (source) => {
    const templateUrls = [];
    const componentDecoratorVisitor = (node) => {
        if (ts.isDecorator(node) &&
            ts.isCallExpression(node.expression) &&
            ts.isIdentifier(node.expression.expression) &&
            node.expression.expression.text === 'Component') {
            const arg = node.expression.arguments[0];
            if (ts.isObjectLiteralExpression(arg)) {
                const template = arg.properties.find(property => ts.isPropertyAssignment(property) &&
                    ts.isIdentifier(property.name) &&
                    property.name.text === 'templateUrl');
                const initializer = template?.initializer;
                if (initializer && ts.isStringLiteral(initializer)) {
                    templateUrls.push(initializer.text);
                }
            }
        }
        else {
            node.forEachChild(componentDecoratorVisitor);
        }
    };
    source.forEachChild(componentDecoratorVisitor);
    return templateUrls;
};
