"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-ignore
const scheduler = __importStar(require("node-cron"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
// @ts-ignore
const fse = __importStar(require("fs-extra"));
const logger_1 = __importDefault(require("../logger"));
/**
 * Define a schedule and implementation for backup using config.backup.cron
 */
class BackupScheduler {
    /**
     *
     * @param {string} cron cron schedule
     * @param {string} mocksDir location of http mockdir to be backed up
     * @param {string} grpcMocksDir location of grpc mockdir to be backed up
     * @param {string} grpcProtosDir location of grpc protos dir to be backed up
     * @param {string} wsMocksDir location of ws mockdir to be backed up
     * @param {string} key location of server.key to be backed up
     * @param {string} cert location of server.cert to be backed up
     * @param {string} configFilePath location of config file to be backed up
     */
    constructor(cron, mocksDir, grpcMocksDir, grpcProtosDir, wsMocksDir, key, cert, configFilePath) {
        /**
         *
         * Create an initial back up while starting the application.
         * Schedule further backup as specified by cron schedule in config
         * @param {boolean} enableHttps Indicates whether HTTPs is enabled/disabled
         * @param {boolean} enableHttp2 Indicates whether HTTP2 is enabled/disabled
         * @param {boolean} enablegRPC Indicates whether gRPC is enabled/disabled
         * @param {boolean} enableWs Indicates whether Websocket is enabled/disabled
         */
        this.schedule = (enableHttps, enableHttp2, enablegRPC, enableWs) => {
            this.createBackup(enableHttps, enableHttp2, enablegRPC, enableWs);
            scheduler.schedule(this.cron, () => {
                this.createBackup(enableHttps, enableHttp2, enablegRPC, enableWs);
            });
            logger_1.default.info(`Scheduled a backup cron job with specified cron: ${this.cron}`);
        };
        /**
         * If following protocols are not enabled, camouflage will not look for directories specific to these protocols,
         * such as certs and grpc/mocks or grpc/protos while creating a backup
         * Copy mocks directory, grpc/mocks and grpc/protos directories, certs directory and config file to backup
         * folder in users' home directory
         * @param {boolean} enableHttps Indicates whether HTTPs is enabled/disabled
         * @param {boolean} enableHttp2 Indicates whether HTTP2 is enabled/disabled
         * @param {boolean} enablegRPC Indicates whether gRPC is enabled/disabled
         * @param {boolean} enableWs Indicates whether Websocket is enabled/disabled
         */
        this.createBackup = (enableHttps, enableHttp2, enablegRPC, enableWs) => {
            logger_1.default.debug("Creating a new back up.");
            fse.copySync(path_1.default.resolve(this.mocksDir), path_1.default.join(os_1.default.homedir(), ".camouflage_backup", "mocks"));
            if (enablegRPC) {
                fse.copySync(path_1.default.resolve(this.grpcMocksDir), path_1.default.join(os_1.default.homedir(), ".camouflage_backup", "grpc", "mocks"));
                fse.copySync(path_1.default.resolve(this.grpcProtosDir), path_1.default.join(os_1.default.homedir(), ".camouflage_backup", "grpc", "protos"));
            }
            if (enableHttps || enableHttp2) {
                fse.copySync(path_1.default.resolve(this.key), path_1.default.join(os_1.default.homedir(), ".camouflage_backup", "certs", "server.key"));
                fse.copySync(path_1.default.resolve(this.cert), path_1.default.join(os_1.default.homedir(), ".camouflage_backup", "certs", "server.cert"));
            }
            if (enableWs) {
                fse.copySync(path_1.default.resolve(this.wsMocksDir), path_1.default.join(os_1.default.homedir(), ".camouflage_backup", "ws_mocks"));
            }
            fse.copySync(path_1.default.resolve(this.configFilePath), path_1.default.join(os_1.default.homedir(), ".camouflage_backup", this.configFileName));
            logger_1.default.debug("Finished creating a new back up.");
        };
        this.cron = cron;
        this.mocksDir = mocksDir;
        this.grpcMocksDir = grpcMocksDir;
        this.grpcProtosDir = grpcProtosDir;
        this.wsMocksDir = wsMocksDir;
        this.key = key;
        this.cert = cert;
        this.configFilePath = configFilePath;
        this.configFileName = configFilePath.split(path_1.default.sep).slice(-1)[0];
    }
}
exports.default = BackupScheduler;
//# sourceMappingURL=index.js.map