"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());
    });
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _EmailsArchiver_pathToThemesManifest;
Object.defineProperty(exports, "__esModule", { value: true });
const systemSettings = require("@ewizardjs/system-settings");
const fs_extra_1 = require("fs-extra");
const globby = require("globby");
const multimatch = require("multimatch");
const ramda_1 = require("ramda");
const upath_1 = require("@ewizardjs/upath");
const uniq_1 = require("../../utils/uniq");
const Archiver_1 = require("./Archiver");
const filesPattern_1 = require("./filesPattern");
const utils_1 = require("./utils");
class EmailsArchiver extends Archiver_1.default {
    constructor(presentationPath, options) {
        super(presentationPath, options);
        this.presentationPath = presentationPath;
        this.options = options;
        _EmailsArchiver_pathToThemesManifest.set(this, void 0);
    }
    getPathToThemesManifest() {
        return __awaiter(this, void 0, void 0, function* () {
            if (__classPrivateFieldGet(this, _EmailsArchiver_pathToThemesManifest, "f")) {
                return __classPrivateFieldGet(this, _EmailsArchiver_pathToThemesManifest, "f");
            }
            const { themes, themesManifest } = yield systemSettings.getByPath(this.presentationPath, 'path');
            __classPrivateFieldSet(this, _EmailsArchiver_pathToThemesManifest, {
                themes,
                themesManifest: (0, upath_1.dirname)(themesManifest) === '.' ? (0, upath_1.join)(themes, themesManifest) : themesManifest,
            }, "f");
            return __classPrivateFieldGet(this, _EmailsArchiver_pathToThemesManifest, "f");
        });
    }
    getRequiredFiles() {
        return __awaiter(this, void 0, void 0, function* () {
            const [requiredFiles, { themesManifest }] = yield Promise.all([
                globby(filesPattern_1.REQUIRED_FILES_PATTERNS, { cwd: this.presentationPath, dot: true }),
                this.getPathToThemesManifest(),
            ]);
            requiredFiles.push(themesManifest);
            return requiredFiles;
        });
    }
    collectRedundantThemesPatterns() {
        return __awaiter(this, void 0, void 0, function* () {
            const { themes, themesManifest } = yield this.getPathToThemesManifest();
            const fullPath = (0, upath_1.join)(this.presentationPath, themesManifest);
            try {
                const themesList = yield (0, fs_extra_1.readJSON)(fullPath);
                const themesFolders = themesList.themes.map((theme) => (0, upath_1.join)(themes, theme.id));
                return [
                    `${(0, upath_1.join)(themes, '**', '*')}`,
                    `!${(0, upath_1.join)(themes, '*.json')}`,
                    ...themesFolders.map((folder) => (0, upath_1.join)(`!${folder}`, '**', '*')),
                ];
            }
            catch (_a) {
                return [];
            }
        });
    }
    collectRedundantBlocksPatterns() {
        return __awaiter(this, void 0, void 0, function* () {
            const { blocks, blocksManifest, modulesManifest } = (yield systemSettings.get(this.presentationPath)).path;
            const fullPath = (0, upath_1.join)(this.presentationPath, blocksManifest);
            const modulesBlocksPatterns = yield this.getModulesBlocksPatterns(modulesManifest, blocks);
            try {
                const blocksjson = yield (0, fs_extra_1.readJSON)(fullPath);
                const bocksFolders = blocksjson.components.map((block) => (0, upath_1.join)(blocks, block.id));
                return [
                    `${(0, upath_1.join)(blocks, '**', '*')}`,
                    `!${(0, upath_1.join)(blocks, '*.json')}`,
                    ...bocksFolders.map((folder) => (0, upath_1.join)(`!${folder}`, '**', '*')),
                    ...modulesBlocksPatterns,
                ];
            }
            catch (_a) {
                return [];
            }
        });
    }
    getModulesBlocksPatterns(modulesManifest, blocks) {
        return __awaiter(this, void 0, void 0, function* () {
            const modulesManifestPath = (0, upath_1.join)(this.presentationPath, modulesManifest);
            try {
                const modules = yield (0, fs_extra_1.readJSON)(modulesManifestPath);
                const modulesFolders = modules.components.map((module) => (0, upath_1.join)(blocks, module.id));
                return modulesFolders.map((folder) => (0, upath_1.join)(`!${folder}`, '**', '*'));
            }
            catch (error) {
                return [];
            }
        });
    }
    getPatternsForRedundantFiles() {
        return __awaiter(this, void 0, void 0, function* () {
            return Promise.all([
                this.collectRedundantThemesPatterns(),
                this.collectRedundantBlocksPatterns(),
            ]);
        });
    }
    collectFilesToArchive() {
        return __awaiter(this, void 0, void 0, function* () {
            const [requiredFiles, files, redundantFilesPatternsList] = yield Promise.all([
                this.getRequiredFiles(),
                this.getFilesList(),
                this.getPatternsForRedundantFiles(),
            ]);
            const allFiles = (0, uniq_1.default)(this.options.files.concat(requiredFiles, files));
            const redundantFiles = redundantFilesPatternsList.reduce((redundantFiles, redundantFilesPatterns) => {
                const matchedFiles = multimatch(allFiles, redundantFilesPatterns, { dot: true });
                return redundantFiles.concat(matchedFiles);
            }, []);
            return (0, ramda_1.without)(redundantFiles, allFiles);
        });
    }
    getFilePatterns() {
        return __awaiter(this, void 0, void 0, function* () {
            const settings = yield systemSettings.get(this.presentationPath);
            return this.getMergedPatterns(settings === null || settings === void 0 ? void 0 : settings.archive);
        });
    }
    getMergedPatterns(archiveSettings) {
        return __awaiter(this, void 0, void 0, function* () {
            const { patterns, ignore } = archiveSettings !== null && archiveSettings !== void 0 ? archiveSettings : {};
            const monorepoPatterns = yield this.getMonorepoPatterns();
            const emailPatterns = filesPattern_1.EMAIL_FILES_PATTERN.concat(monorepoPatterns);
            if (patterns) {
                return emailPatterns.concat(patterns);
            }
            if (ignore === null || ignore === void 0 ? void 0 : ignore.length) {
                return emailPatterns.concat((0, utils_1.getRevertedIgnorePatterns)(ignore));
            }
            return emailPatterns;
        });
    }
    getFilesList() {
        return __awaiter(this, void 0, void 0, function* () {
            const filePatterns = yield this.getFilePatterns();
            return globby(filePatterns, { cwd: this.presentationPath, dot: true });
        });
    }
    getMonorepoPatterns() {
        return __awaiter(this, void 0, void 0, function* () {
            const { layouts, masterTemplatePath, masterTemplateLayout, } = (yield systemSettings.get(this.presentationPath)).path;
            const patterns = [];
            if (masterTemplatePath && typeof masterTemplatePath === 'string') {
                const filesPattern = '**/*.json';
                const layoutsPath = (0, upath_1.join)(masterTemplatePath, layouts);
                patterns.push(...[(0, upath_1.join)(masterTemplatePath, filesPattern), `!${(0, upath_1.join)(layoutsPath, filesPattern)}`]);
                if (masterTemplateLayout && typeof masterTemplateLayout === 'string') {
                    patterns.push((0, upath_1.join)(layoutsPath, masterTemplateLayout, filesPattern));
                }
            }
            return patterns;
        });
    }
}
exports.default = EmailsArchiver;
_EmailsArchiver_pathToThemesManifest = new WeakMap();
//# sourceMappingURL=EmailsArchiver.js.map