'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_newIconsPack_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 data = migrations_newIconsPack_data.newIconsPackData;
function newIconsPack(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) {
                data.forEach(({ replace, replaceWith }) => {
                    newContent = newContent.replace(new RegExp(`kbq-${replace}`, 'g'), `kbq-${replaceWith}`);
                });
            }
            else {
                const foundIcons = data.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 templates & components
        targetDir.visit((path, entry) => {
            // if project property not provided, skip files in node_modules & dist
            if (path.includes('node_modules') || path.includes('dist')) {
                return;
            }
            if (path.endsWith('.html') || path.endsWith('.ts') || path.endsWith('.md')) {
                const initialContent = entry?.content.toString();
                let newContent = initialContent;
                if (newContent) {
                    migrations_newIconsPack_data.iconReplacements.forEach(({ replace, replaceWith }) => {
                        newContent = newContent.replace(new RegExp(replace, 'g'), replaceWith);
                    });
                    newContent = handleDeprecatedIcons(newContent, path);
                    if (initialContent !== newContent) {
                        tree.overwrite(path, newContent || '');
                    }
                }
            }
        });
        // update 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(stylesExt)) {
                const initialContent = entry?.content.toString();
                let newContent = initialContent;
                if (newContent) {
                    // replace icons in styles
                    newContent = newContent.replace(new RegExp('mc-', 'g'), 'kbq-');
                    newContent = handleDeprecatedIcons(newContent, path);
                }
                if (initialContent !== newContent) {
                    tree.overwrite(path, newContent || '');
                }
            }
        });
    };
}

exports.default = newIconsPack;
