'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_iconsReplacement_data = require('./data.js');
require('@angular-devkit/schematics');
require('@schematics/angular/utility');

const replaceIcons = (contentToBeUpdated, replacementData) => {
    let res = contentToBeUpdated;
    for (const { from, to } of replacementData) {
        res = res.replace(new RegExp(`kbq-${from}`, 'g'), `kbq-${to}`);
    }
    return res;
};
const logReplacement = ({ logger, replacementData, contentToBeUpdated, filePath }) => {
    const foundIcons = replacementData.filter(({ from }) => contentToBeUpdated.indexOf(from) !== -1);
    if (foundIcons.length) {
        const parsedFilePath = path.relative(__dirname, `.${filePath}`).replace(/\\/g, '/');
        utils_messages.logMessage(logger, [
            `Please pay attention! Found deprecated icons in file: `,
            parsedFilePath,
            foundIcons.map(({ from, to }) => `\t${from} -> \t${to}`).join('\n')
        ]);
    }
};
const processIconReplacement = ({ fileContent, filePath, fix, logger, replacementData }) => {
    if (fix) {
        const updatedContent = replaceIcons(fileContent, replacementData);
        return { fileContent: updatedContent, changed: updatedContent !== fileContent, errors: [] };
    }
    else {
        logReplacement({
            logger,
            replacementData,
            contentToBeUpdated: fileContent,
            filePath
        });
        return { fileContent, changed: false, errors: [] };
    }
};
const resolveReplacementData = (projectRootPath, tree, customReplacementDataPath) => {
    if (customReplacementDataPath) {
        const replacementData = tree.get(`${projectRootPath}/${customReplacementDataPath}`);
        if (!replacementData)
            return migrations_iconsReplacement_data.iconReplacementData;
        return JSON.parse(replacementData.content.toString());
    }
    return migrations_iconsReplacement_data.iconReplacementData;
};
function migrate(options) {
    return async (tree, context) => {
        const { logger } = context;
        const { project, allowed, fix, replacementDataPath } = options;
        const projectDefinition = await utils_packageConfig.setupOptions(project, tree);
        const replacementData = resolveReplacementData(projectDefinition.root, tree, replacementDataPath);
        tree.getDir(projectDefinition.root).visit((filePath, entry) => {
            if (allowed.length === 0 || allowed.some((ext) => filePath.endsWith(ext))) {
                const initialContent = entry?.content.toString();
                if (!initialContent)
                    return;
                const { fileContent, changed } = processIconReplacement({
                    fileContent: initialContent,
                    filePath,
                    fix,
                    logger,
                    replacementData
                });
                if (changed) {
                    tree.overwrite(filePath, fileContent);
                }
            }
        });
    };
}

exports.default = migrate;
exports.logReplacement = logReplacement;
exports.processIconReplacement = processIconReplacement;
exports.replaceIcons = replaceIcons;
exports.resolveReplacementData = resolveReplacementData;
