"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("@schematics/angular/utility/config");
const schematics_1 = require("@angular/cdk/schematics");
const ast_1 = require("../utils/ast");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const hammerjsImportStatement = `import 'hammerjs';`;
/** Adds HammerJS to the main file of the specified Angular CLI project. */
function addHammerJsToMain(options) {
    return (host, context) => {
        context.logger.debug('addHammerJsToMain');
        if (!options.gestures) {
            return;
        }
        const workspace = config_1.getWorkspace(host);
        const project = schematics_1.getProjectFromWorkspace(workspace, options.project);
        const mainFile = schematics_1.getProjectMainFile(project);
        const modulePath = ng_ast_utils_1.getAppModulePath(host, mainFile);
        let recorder = host.beginUpdate(mainFile);
        const buffer = host.read(mainFile);
        if (!buffer) {
            return console.error(`Could not read the project main file (${mainFile}). Please manually ` +
                `import HammerJS in your main TypeScript file.`);
        }
        const fileContent = buffer.toString('utf8');
        if (fileContent.includes(hammerjsImportStatement)) {
            return console.log(`HammerJS is already imported in the project main file (${mainFile}).`);
        }
        recorder.insertRight(0, `${hammerjsImportStatement}\n`);
        host.commitUpdate(recorder);
        ast_1.addImport(host, modulePath, ['LyHammerGestureConfig'], '@alyle/ui');
        ast_1.addImport(host, modulePath, ['HAMMER_GESTURE_CONFIG'], '@angular/platform-browser');
        const moduleSource = ast_1.getTsSourceFile(host, modulePath);
        const themeSimbolName = `{ provide: HAMMER_GESTURE_CONFIG, useClass: LyHammerGestureConfig }`;
        const metadataChanges = ast_utils_1.addSymbolToNgModuleMetadata(moduleSource, modulePath, 'providers', themeSimbolName);
        if (metadataChanges) {
            recorder = host.beginUpdate(modulePath);
            metadataChanges.forEach((change) => {
                recorder.insertRight(change.pos, change.toAdd);
            });
            host.commitUpdate(recorder);
        }
    };
}
exports.addHammerJsToMain = addHammerJsToMain;
//# sourceMappingURL=gestures.js.map