"use strict";
exports.__esModule = true;
var fs = require('fs');
var mfeBaseUrlKey = 'MFE_BASE_URL';
var srcUrlRegex = /url\((\\?["'])?(?!data:)\S+(\\?["'])?\)/g;
function normalizeCssPaths(params) {
    params.paths = params.paths ? params.paths : [];
    params.paths.forEach(function (path) { return normalizeCssPath(path, params.outDir); });
}
exports.normalizeCssPaths = normalizeCssPaths;
function normalizeCssPath(path, outputDirectory) {
    console.log("Start processing " + path);
    var css = fs.readFileSync(path, 'utf8');
    var counter = 0;
    var processedCss = css.replace(srcUrlRegex, function (srcUrl) {
        if (srcUrl.search(outputDirectory) != -1)
            return srcUrl;
        var fileName = getFileName(srcUrl);
        var processedUrl = "url('" + outputDirectory + "/" + fileName + "')";
        counter++;
        console.log("Replaced " + srcUrl + " -> " + processedUrl);
        return processedUrl;
    });
    console.log("Replaced " + counter + " urls");
    fs.writeFileSync(path, processedCss);
}
function getFileName(srcUrl) {
    var url = srcUrl.substring(4, srcUrl.length - 1); // unbox 'url(...)'
    url = url.replace(/(\\?["'])/g, '');
    var urlPaths = url.split('/');
    return urlPaths[urlPaths.length - 1].split('?')[0];
}
