"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recursiveCopy = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
// eslint-disable-next-line max-params
function recursiveCopy(templatePath, targetPath, skipFiles, replace, rename) {
    function replaceAll(string, search, replace) {
        return string.split(search).join(replace);
    }
    function createDirectoryContents(templatePath, targetPath) {
        // read all files/folders (1 level) from template folder
        const filesToCreate = fs_1.default.readdirSync(templatePath);
        // loop each file/folder
        for (const fileName of filesToCreate) {
            const origFilePath = path_1.default.join(templatePath, fileName);
            // get stats about the current file
            const stats = fs_1.default.statSync(origFilePath);
            // skip files that should not be copied
            if (skipFiles.includes(fileName))
                continue;
            if (stats.isFile()) {
                const ext = path_1.default.extname(origFilePath);
                const destFileName = rename[fileName] ? rename[fileName] : fileName;
                const writePath = path_1.default.join(targetPath, destFileName);
                if ([".png", ".jpeg", ".jpg", ".svg"].includes(ext)) {
                    fs_1.default.copyFileSync(origFilePath, writePath);
                }
                else {
                    // read file content and transform it using template engine
                    let contents = fs_1.default.readFileSync(origFilePath, "utf8");
                    for (const [key, value] of Object.entries(replace)) {
                        contents = replaceAll(contents, `{{ ${key} }}`, value);
                    }
                    // write file to destination folder
                    fs_1.default.writeFileSync(writePath, contents, "utf8");
                }
            }
            else if (stats.isDirectory()) {
                // create folder in destination folder
                fs_1.default.mkdirSync(path_1.default.join(targetPath, fileName));
                // copy files/folder inside current folder recursively
                createDirectoryContents(path_1.default.join(templatePath, fileName), path_1.default.join(targetPath, fileName));
            }
        }
    }
    return createDirectoryContents(templatePath, targetPath);
}
exports.recursiveCopy = recursiveCopy;
