"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.callsProvidersFunction = callsProvidersFunction;
exports.callsProvidersFunctionStandalone = callsProvidersFunctionStandalone;
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@angular/cdk/schematics");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const app_config_1 = require("@schematics/angular/utility/standalone/app_config");
const util_1 = require("@schematics/angular/utility/standalone/util");
const ts = require("typescript");
const main_ts_file_path_1 = require("./main-ts-file-path");
function callsProvidersFunction(tree, projectName, functionName) {
    return __awaiter(this, void 0, void 0, function* () {
        const mainPath = yield (0, main_ts_file_path_1.getMainTsFilePath)(tree, projectName);
        const isStandalone = (0, schematics_2.isStandaloneApp)(tree, mainPath);
        if (isStandalone) {
            const bootstrapCall = (0, util_1.findBootstrapApplicationCall)(tree, mainPath);
            return callsProvidersFunctionStandalone(tree, bootstrapCall, mainPath, functionName);
        }
        return callsProvidersFunctionNgModule(tree, mainPath, functionName);
    });
}
/**
 * Checks whether a providers function is being called in a `bootstrapApplication` call.
 * @param tree File tree of the project.
 * @param bootstrapCall Call to `bootstrapApplication`
 * @param filePath Path of the file in which to check.
 * @param functionName Name of the function to search for.
 */
function callsProvidersFunctionStandalone(tree, bootstrapCall, filePath, functionName) {
    const appConfig = (0, app_config_1.findAppConfig)(bootstrapCall, tree, filePath);
    const providersLiteral = appConfig ? (0, util_1.findProvidersLiteral)(appConfig.node) : null;
    return !!(providersLiteral === null || providersLiteral === void 0 ? void 0 : providersLiteral.elements.some((el) => ts.isCallExpression(el) && ts.isIdentifier(el.expression) && el.expression.text === functionName));
}
function callsProvidersFunctionNgModule(tree, mainPath, functionName) {
    const appModulePath = (0, schematics_2.getAppModulePath)(tree, mainPath);
    const appModuleSource = (0, util_1.getSourceFile)(tree, appModulePath);
    const ngModuleDecorator = (0, ast_utils_1.getDecoratorMetadata)(appModuleSource, 'NgModule', '@angular/core');
    if (!ngModuleDecorator.length) {
        throw new schematics_1.SchematicsException(`❌ App module declaration not found.`);
    }
    const providersLiteral = (0, util_1.findProvidersLiteral)(ngModuleDecorator[0]);
    return !!(providersLiteral === null || providersLiteral === void 0 ? void 0 : providersLiteral.elements.some((el) => ts.isCallExpression(el) && ts.isIdentifier(el.expression) && el.expression.text === functionName));
}
//# sourceMappingURL=calls-providers-function.js.map