"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyFiles = exports.copyLayoutFiles = exports.copyMasterTemplateFiles = void 0;
const fs_extra_1 = require("fs-extra");
const globby = require("globby");
const multimatch = require("multimatch");
const upath_1 = require("upath");
function copyMasterTemplateFiles(baseDir, layoutPath, layoutDest, layoutFiles) {
    return fs_extra_1.copy(baseDir, layoutDest, {
        filter: getFilterFn(baseDir, [...layoutFiles, upath_1.join(layoutPath, '..')]),
    });
}
exports.copyMasterTemplateFiles = copyMasterTemplateFiles;
function copyLayoutFiles(layoutPath, layoutDest, ignoredFiles = []) {
    return fs_extra_1.copy(layoutPath, layoutDest, {
        filter: getFilterFn(layoutPath, ignoredFiles),
        overwrite: true,
    });
}
exports.copyLayoutFiles = copyLayoutFiles;
function copyFiles(baseDir, layoutPath, dest, path) {
    return __awaiter(this, void 0, void 0, function* () {
        const ignoreFiles = [
            ...getSettingsPathsList(path),
            'index.html',
            '.ewizard/settings.json',
        ];
        const { settings: contentSettingsPath } = path;
        yield copyLayoutFiles(layoutPath, dest, ignoreFiles);
        yield copyRootFiles(baseDir, dest);
        yield copyCommonResources(baseDir, dest, path.common);
        yield Promise.all([
            copyPublicFiles(dest),
            copyIndexHtml(baseDir, dest),
            copySettingsJson(layoutPath, dest, contentSettingsPath),
        ]);
    });
}
exports.copyFiles = copyFiles;
function getFilterFn(baseDir, ignoredFiles = []) {
    const defaultIgnore = ['.git', '.idea', '.vscode', 'build', 'node_modules', '*.zip'];
    const ignorePatterns = ignoredFiles.concat(defaultIgnore).map((value) => upath_1.resolve(baseDir, value));
    return (src) => !multimatch(upath_1.toUnix(src), ignorePatterns).length;
}
function copyIndexHtml(baseDir, dest) {
    const srcFile = upath_1.join(baseDir, 'index.html');
    const destFile = upath_1.join(dest, 'index.html');
    return fs_extra_1.copy(srcFile, destFile, { overwrite: false });
}
function copySettingsJson(baseDir, dest, settingsPath) {
    const srcFile = upath_1.join(baseDir, settingsPath);
    const destFile = upath_1.join(dest, settingsPath);
    return fs_extra_1.copy(srcFile, destFile);
}
function copyPublicFiles(dest) {
    const publicPath = upath_1.join(__dirname, '../../../public/', 'wiz-create');
    return fs_extra_1.copy(publicPath, dest);
}
function copyCommonResources(baseDir, dest, common) {
    return __awaiter(this, void 0, void 0, function* () {
        const commonPaths = getSettingsPathsList(common);
        const existingPaths = yield globby(commonPaths, { cwd: baseDir });
        return copyFilesByPaths(baseDir, dest, existingPaths);
    });
}
function copyRootFiles(baseDir, dest) {
    return __awaiter(this, void 0, void 0, function* () {
        const ignoredFiles = [
            'index.html',
            '.gitignore',
            'package*.json',
            'settings.json',
            '*.vue',
            '*.zip',
            '*.(md|MD)',
        ];
        const rootFiles = yield globby('*', { cwd: baseDir, dot: true, ignore: ignoredFiles });
        return copyFilesByPaths(baseDir, dest, rootFiles);
    });
}
function copyFilesByPaths(src, dest, paths) {
    return Promise.all(paths.map((path) => {
        const srcFile = upath_1.join(src, path);
        const destFile = upath_1.join(dest, path);
        return fs_extra_1.copy(srcFile, destFile, { overwrite: false });
    }));
}
function getSettingsPathsList(path) {
    return Object.values(path).reduce((paths, value) => {
        if (typeof value === 'string') {
            return [...paths, value];
        }
        if (isObjectOrArray(value)) {
            return [...paths, ...getSettingsPathsList(value)];
        }
        return paths;
    }, []);
}
function isObjectOrArray(value) {
    return Object.prototype.toString.call(value) === '[object Object]' || Array.isArray(value);
}
//# sourceMappingURL=copyFiles.js.map