"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.getAppComponentPath = void 0;
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const vendored_ast_utils_1 = require("./vendored-ast-utils");
const ts = require("typescript");
const path_1 = require("path");
const core_1 = require("@angular-devkit/core");
const ast_1 = require("./ast");
const schematics_1 = require("@angular/cdk/schematics");
const workspace_1 = require("@schematics/angular/utility/workspace");
function getAppComponentPath(host, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const workspace = yield workspace_1.getWorkspace(host);
        const project = schematics_1.getProjectFromWorkspace(workspace, options.project);
        const mainPath = schematics_1.getProjectMainFile(project);
        const modulePath = ng_ast_utils_1.getAppModulePath(host, mainPath);
        const moduleSource = ast_1.getTsSourceFile(host, modulePath);
        const decoratorMetadata = vendored_ast_utils_1.getDecoratorMetadata(moduleSource, 'NgModule', '@angular/core');
        const propertyName = 'bootstrap';
        const { properties } = decoratorMetadata[0];
        const property = properties
            .filter(prop => prop.kind === ts.SyntaxKind.PropertyAssignment)
            .filter((prop) => {
            const name = prop.name;
            switch (name.kind) {
                case ts.SyntaxKind.Identifier:
                    return name.getText() === propertyName;
                case ts.SyntaxKind.StringLiteral:
                    return name.text === propertyName;
            }
            return false;
        })[0];
        const bootstrapValue = (property.initializer.getText()).split(/\[|\]|\,\s?/g).filter(s => s)[0];
        const appComponentPath = moduleSource.statements
            .filter(prop => prop.kind === ts.SyntaxKind.ImportDeclaration)
            .filter(prop => vendored_ast_utils_1.findNode(prop, ts.SyntaxKind.ImportSpecifier, bootstrapValue))
            .map(({ moduleSpecifier }) => moduleSpecifier.text)[0];
        const mainDir = path_1.dirname(modulePath);
        return core_1.normalize(`/${mainDir}/${appComponentPath}.ts`);
    });
}
exports.getAppComponentPath = getAppComponentPath;
//# sourceMappingURL=get-app-component-path.js.map