'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var path = require('path');
var utils_packageConfig = require('../../utils/package-config.js');
var migrations_cssSelectors_data = require('./data.js');
require('@angular-devkit/schematics');
require('@schematics/angular/utility');
require('../../utils/messages.js');

function _interopNamespaceDefault(e) {
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n.default = e;
    return Object.freeze(n);
}

var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);

function cssSelectors(options) {
    let targetDir;
    return async (tree, context) => {
        const { project, fix, stylesExt } = options;
        try {
            const projectDefinition = await utils_packageConfig.setupOptions(project, tree);
            targetDir = projectDefinition ? tree.getDir(projectDefinition.root) : tree;
        }
        catch (e) {
            targetDir = tree;
        }
        const { logger } = context;
        const getDeprecatedFrom = (content, replacementInfo) => {
            return replacementInfo.filter(({ replace }) => new RegExp(`\\b${replace}\\b`, 'g').exec(content));
        };
        const handleReplacements = (newContent, path) => {
            if (fix) {
                migrations_cssSelectors_data.typographyCssSelectorsReplacement.forEach(({ replace, replaceWith }) => {
                    newContent = newContent.replace(new RegExp(`\\b${replace}\\b`, 'g'), replaceWith);
                });
            }
            else {
                const foundSelectors = getDeprecatedFrom(newContent, migrations_cssSelectors_data.typographyCssSelectorsReplacement);
                if (foundSelectors.length) {
                    logger.warn(getTypographyReplacements(path, foundSelectors));
                }
            }
            // Just suggest about colors replacements without changing file,
            // since it's potentially a complex task to handle
            const foundColors = getDeprecatedFrom(newContent, migrations_cssSelectors_data.colorsVarsReplacement);
            if (foundColors.length) {
                logger.warn(getBaseReplacements(path, foundColors));
            }
            return newContent;
        };
        // Update templates & components & styles
        targetDir.visit((path, entry) => {
            // if project property not provided, provide node_modules to be changed
            if (path.includes('node_modules'))
                return;
            if (path.endsWith('.html') || path.endsWith('.ts') || path.endsWith(stylesExt)) {
                const initialContent = entry?.content.toString();
                let newContent = initialContent;
                if (newContent) {
                    newContent = handleReplacements(newContent, path);
                    if (initialContent !== newContent) {
                        tree.overwrite(path, newContent || '');
                    }
                }
            }
        });
    };
}
function getBaseReplacements(filePath, foundSelectors) {
    const parsedFilePath = path__namespace.relative(__dirname, `.${filePath}`).replace(/\\/g, '/');
    return `
-------------------------
Please pay attention! Found deprecated сss-selectors in file:
${parsedFilePath}
Replace with specified rules:
${foundSelectors.map(({ replace, replaceWith }) => `\t${replace} -> \t${replaceWith}`).join('\n')}
-------------------------
    `;
}
function getTypographyReplacements(filePath, foundSelectors) {
    const baseMessage = getBaseReplacements(filePath, foundSelectors);
    return `${baseMessage}\n
Or use --fix=true to replace automatically
Pay attention: overwriting is possible. Check the code after automatic replacement was done.
-------------------------
    `;
}

exports.default = cssSelectors;
