"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const package_handler_1 = require("../utils/package-handler");
const dependency_handler_1 = require("../utils/dependency-handler");
const add_normalize_1 = require("./add-normalize");
const config_1 = require("@schematics/angular/utility/config");
/**
 * Checks whether a property exists in the angular workspace.
 */
function propertyExistsInWorkspace(targetProp, workspace) {
    const foundProp = dependency_handler_1.getPropertyFromWorkspace(targetProp, workspace);
    return foundProp !== null && foundProp.key === targetProp;
}
const enableIESupport = (tree, context) => {
    var _a;
    const targetFile = '/.browserslistrc';
    let updateFile = false;
    let content = ((_a = tree.read(targetFile)) === null || _a === void 0 ? void 0 : _a.toString());
    while (content === null || content === void 0 ? void 0 : content.includes('not IE')) {
        content = content.replace('not IE', 'IE');
        updateFile = true;
    }
    if (updateFile) {
        tree.overwrite(targetFile, content);
    }
    else {
        context.logger.warn(`Either IE support is already enabled OR you may need to update ${targetFile} file manually.`);
    }
};
function updatePolyfillsForIESupport(tree, targetFile, polyfillsData) {
    // Target the web-animations-js commented import statement and uncomment it.
    const webAnimationsLine = '// import \'web-animations-js\';';
    const classlistLine = '// import \'classlist.js\';';
    polyfillsData = polyfillsData.replace(webAnimationsLine, webAnimationsLine.substring(3, webAnimationsLine.length));
    polyfillsData = polyfillsData.replace(classlistLine, classlistLine.substring(3, classlistLine.length));
    tree.overwrite(targetFile, polyfillsData);
}
function readInput(options) {
    return (tree, context) => {
        var _a;
        if (options.polyfills) {
            const workspace = config_1.getWorkspace(tree);
            const project = workspace.projects[workspace.defaultProject];
            const polyfillsFilePath = dependency_handler_1.getConfigFile(project, 'polyfills', context);
            const browserslistrcFile = (tree.read('/.browserslistrc'));
            const animationsWarn = 'You may want to manually uncomment \'// import \'web-animations-js\' in polyfills.ts';
            // .browserslistrc --> If project targets angular cli version >= 10.0
            if (browserslistrcFile) {
                enableIESupport(tree, context);
            }
            else {
                context.logger.warn('.browserslistrc file not found. You may want to manually add it and enable IE support.');
            }
            if (polyfillsFilePath) {
                const polyfillsData = (_a = tree.read(polyfillsFilePath)) === null || _a === void 0 ? void 0 : _a.toString();
                if (polyfillsData) {
                    updatePolyfillsForIESupport(tree, polyfillsFilePath, polyfillsData);
                }
                else {
                    context.logger.warn('polyfills.ts file not found OR empty. ' + animationsWarn);
                }
            }
            else {
                context.logger.warn('polyfills.ts file path not found. ' + animationsWarn);
            }
        }
    };
}
function addNormalize(options) {
    return (tree, context) => {
        if (options.resetCss) {
            const result = add_normalize_1.addResetCss(tree);
            if (!result) {
                context.logger.warn(`Could not complete adding reset styles. Those may need to be added manually.`);
            }
        }
    };
}
function default_1(options) {
    return schematics_1.chain([
        readInput(options),
        addNormalize(options),
        dependency_handler_1.addDependencies(options),
        package_handler_1.installPackageJsonDependencies(options),
        dependency_handler_1.logSuccess(options)
    ]);
}
exports.default = default_1;
