"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.config = void 0;
const core_1 = require("../../core");
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const listr_1 = __importDefault(require("listr"));
const base_1 = __importDefault(require("./base"));
const enum_1 = require("../utils/enum");
const config_1 = __importDefault(require("../utils/config"));
const updatePath = path_1.default.join(config_1.default.CacheRoot, config_1.default.updateFile);
const jssdkPath = path_1.default.join(config_1.default.CacheRoot, config_1.default.gadgetJSSDKFiles);
const downloadTemp = path_1.default.join(config_1.default.CacheRoot, config_1.default.downloadTemp);
function deleteDirectory(path) {
    if (path.startsWith(config_1.default.CacheRoot)) {
        return fs_extra_1.default.remove(path);
    }
    return Promise.resolve();
}
exports.config = {
    [enum_1.CacheType.update]: {
        title: 'Clear update cache',
        task() {
            return deleteDirectory(updatePath);
        },
    },
    [enum_1.CacheType.jssdk]: {
        title: 'Clear JSSDK files',
        task() {
            return Promise.all([
                deleteDirectory(jssdkPath),
                deleteDirectory(downloadTemp),
            ]).then(() => void 0);
        },
    },
};
class Cache extends base_1.default {
    constructor() {
        super(core_1.ModuleName.eventTracker);
    }
    clear(type, slience) {
        const keys = type === enum_1.CacheType.all ? Object.keys(exports.config) : [type];
        const items = keys.map((item) => exports.config[item]);
        if (slience) {
            return Promise.all(items.map((item) => item.task())).then(() => void 0);
        }
        const tasks = new listr_1.default(items, {
            concurrent: false,
            exitOnError: false,
        });
        return tasks.run();
    }
}
exports.default = Cache;
