"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.setVirtual = exports.NgTreeFileSystem = void 0;
// tslint:disable:no-implicit-dependencies
const core_1 = require("@angular-devkit/core");
const cli_core_1 = require("@igniteui/cli-core");
const angular_templates_1 = require("@igniteui/angular-templates");
//#region Temp duplicate of schematics pack fs
class NgTreeFileSystem {
    constructor(tree) {
        this.tree = tree;
    }
    fileExists(filePath) {
        return this.tree.exists(filePath);
    }
    readFile(filePath, encoding) {
        return (this.tree.read(filePath) || "").toString();
    }
    writeFile(filePath, text) {
        return this.tree.overwrite(filePath, text);
    }
    directoryExists(dirPath) {
        return this.tree.exists(dirPath);
    }
    glob(dirPath, pattern) {
        return [];
    }
}
exports.NgTreeFileSystem = NgTreeFileSystem;
function setVirtual(tree) {
    cli_core_1.App.container.set(cli_core_1.FS_TOKEN, new NgTreeFileSystem(tree));
    cli_core_1.App.container.set(cli_core_1.FS_TYPE_TOKEN, cli_core_1.FsTypes.virtual);
}
exports.setVirtual = setVirtual;
//#endregion
function default_1() {
    return (tree, context) => __awaiter(this, void 0, void 0, function* () {
        const { workspace } = yield core_1.workspaces.readWorkspace("/", (0, cli_core_1.createWorkspaceHost)(tree));
        const project = workspace.extensions.defaultProject ?
            workspace.projects.get(workspace.extensions.defaultProject) :
            workspace.projects.values().next().value;
        const moduleFile = `${project.sourceRoot}/${project.prefix}/app.module.ts`;
        context.logger.info(`Applying migration for Ignite UI CLI 5.0.0`);
        if (tree.exists(moduleFile)) {
            setVirtual(tree);
            const mainModule = new angular_templates_1.AngularTypeScriptFileUpdate(moduleFile, false, { indentSize: 2 });
            mainModule.addNgModuleMeta({ import: "HammerModule", from: "@angular/platform-browser" });
            cli_core_1.TypeScriptUtils.saveFile(moduleFile, mainModule.finalize());
        }
    });
}
exports.default = default_1;
