"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.addHammerJsToMain = void 0;
const workspace_1 = require("@schematics/angular/utility/workspace");
const schematics_1 = require("@angular/cdk/schematics");
const ast_1 = require("../utils/ast");
const hammerjsImportStatement = `import 'hammerjs';`;
/** Adds HammerJS to the main file of the specified Angular CLI project. */
function addHammerJsToMain(options) {
    return (host, context) => __awaiter(this, void 0, void 0, function* () {
        context.logger.debug('addHammerJsToMain');
        if (!options.gestures) {
            return;
        }
        const workspace = yield workspace_1.getWorkspace(host);
        const project = schematics_1.getProjectFromWorkspace(workspace, options.project);
        const mainFile = schematics_1.getProjectMainFile(project);
        const modulePath = schematics_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 = schematics_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