"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateControllerBase = void 0;
const path = require("path");
const semver = require("semver");
const _ = require("lodash");
class UpdateControllerBase {
    constructor($fs, $platformCommandHelper, $platformsDataService, $packageInstallationManager, $packageManager, $pacoteService) {
        this.$fs = $fs;
        this.$platformCommandHelper = $platformCommandHelper;
        this.$platformsDataService = $platformsDataService;
        this.$packageInstallationManager = $packageInstallationManager;
        this.$packageManager = $packageManager;
        this.$pacoteService = $pacoteService;
        this.getPackageManifest = _.memoize(this._getPackageManifest, (...args) => {
            return args.join("@");
        });
    }
    restoreBackup(folders, backupDir, projectDir) {
        for (const folder of folders) {
            this.$fs.deleteDirectory(path.join(projectDir, folder));
            const fileName = folder.replace(path.sep, "_");
            const folderToCopy = path.join(backupDir, fileName);
            if (this.$fs.exists(folderToCopy)) {
                this.$fs.copyFile(folderToCopy, path.resolve(projectDir, folder));
            }
        }
    }
    backup(folders, backupDir, projectDir) {
        this.$fs.deleteDirectory(backupDir);
        this.$fs.createDirectory(backupDir);
        for (const folder of folders) {
            const fileName = folder.replace(path.sep, "_");
            const folderToCopy = path.join(projectDir, folder);
            if (this.$fs.exists(folderToCopy)) {
                this.$fs.copyFile(folderToCopy, path.resolve(backupDir, fileName));
            }
        }
    }
    hasDependency(dependency, projectData) {
        const devDependencies = Object.keys(projectData.devDependencies);
        const dependencies = Object.keys(projectData.dependencies);
        return [...devDependencies, ...dependencies].some((packageName) => {
            return packageName === dependency.packageName;
        });
    }
    hasRuntimeDependency({ platform, projectData, }) {
        const lowercasePlatform = platform.toLowerCase();
        const currentPlatformVersion = this.$platformCommandHelper.getCurrentPlatformVersion(lowercasePlatform, projectData);
        return !!currentPlatformVersion;
    }
    async getMaxRuntimeVersion({ platform, projectData, }) {
        const lowercasePlatform = platform.toLowerCase();
        const currentPlatformVersion = this.$platformCommandHelper.getCurrentPlatformVersion(lowercasePlatform, projectData);
        const platformData = this.$platformsDataService.getPlatformData(lowercasePlatform, projectData);
        if (currentPlatformVersion) {
            return ((await this.$packageInstallationManager.getMaxSatisfyingVersionSafe(platformData.frameworkPackageName, currentPlatformVersion)) || currentPlatformVersion);
        }
    }
    async _getPackageManifest(templateName, version) {
        const packageVersion = semver.valid(version) ||
            (await this.$packageManager.getTagVersion(templateName, version));
        if (packageVersion && semver.valid(packageVersion)) {
            return await this.$pacoteService.manifest(`${templateName}@${packageVersion}`, { fullMetadata: true });
        }
        else {
            throw new Error(`Failed to get information for package: ${templateName}@${version}`);
        }
    }
}
exports.UpdateControllerBase = UpdateControllerBase;
