'use strict';

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

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

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);

const iconsFontImportRule = "@use '@koobiq/icons/fonts/kbq-icons';";
function deprecatedIcons(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 handleDeprecatedIcons = (newContent, filePath) => {
            if (fix) {
                migrations_deprecatedIcons_data.iconsMapping.forEach(({ replace, replaceWith }) => {
                    newContent = newContent.replace(new RegExp(`kbq-${replace}`, 'g'), `kbq-${replaceWith}`);
                });
            }
            else {
                const foundIcons = migrations_deprecatedIcons_data.iconsMapping.filter(({ replace }) => newContent.indexOf(replace) !== -1);
                if (foundIcons.length) {
                    const parsedFilePath = path__namespace.relative(__dirname, `.${filePath}`).replace(/\\/g, '/');
                    utils_messages.logMessage(logger, [
                        `Please pay attention! Found deprecated icons in file: `,
                        parsedFilePath,
                        foundIcons.map(({ replace, replaceWith }) => `\t${replace} -> \t${replaceWith}`).join('\n')
                    ]);
                }
            }
            return newContent;
        };
        // Update styles, templates & components
        targetDir.visit((filePath, entry) => {
            let initialContent;
            // if project property not provided, skip files in node_modules & dist
            if (filePath.includes('node_modules') || filePath.includes('dist')) {
                return;
            }
            if (filePath.endsWith('.html') || filePath.endsWith('.ts') || filePath.endsWith(stylesExt)) {
                initialContent = entry?.content.toString();
                let newContent = initialContent;
                if (newContent) {
                    migrations_deprecatedIcons_data.iconClassReplacements.forEach(({ replace, replaceWith }) => {
                        newContent = newContent.replace(new RegExp(replace, 'g'), replaceWith);
                    });
                    newContent = handleDeprecatedIcons(newContent, filePath);
                    if (initialContent !== newContent) {
                        tree.overwrite(filePath, newContent || '');
                    }
                }
            }
        });
        // check if icon styles from new scope should be included in styles file
        targetDir.visit((filePath, entry) => {
            if (filePath.endsWith(stylesExt) && !entry?.content.toString()?.includes(iconsFontImportRule)) {
                const parsedFilePath = path__namespace.relative(__dirname, `.${filePath}`).replace(/\\/g, '/');
                utils_messages.logMessage(logger, [
                    parsedFilePath,
                    `Provide \`${iconsFontImportRule}\` to support icon styles from new scope`
                ]);
            }
        });
    };
}

exports.default = deprecatedIcons;
