"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
const os_1 = require("os");
const glob = require("glob");
const Utilities_1 = require("../utilities/Utilities");
const node_core_library_1 = require("@microsoft/node-core-library");
/**
 * This class represents the collection of change files existing in the repo and provides operations
 * for those change files.
 */
class ChangeFiles {
    /**
     * Validate if the newly added change files match the changed packages.
     */
    static validate(newChangeFilePaths, changedPackages) {
        const changedSet = new Set();
        newChangeFilePaths.forEach((filePath) => {
            console.log(`Found change file: ${filePath}`);
            const changeRequest = node_core_library_1.JsonFile.load(filePath);
            if (changeRequest && changeRequest.changes) {
                changeRequest.changes.forEach(change => {
                    changedSet.add(change.packageName);
                });
            }
            else {
                throw new Error(`Invalid change file: ${filePath}`);
            }
        });
        const requiredSet = new Set(changedPackages);
        changedSet.forEach((name) => {
            requiredSet.delete(name);
        });
        if (requiredSet.size > 0) {
            const missingProjects = [];
            requiredSet.forEach(name => {
                missingProjects.push(name);
            });
            throw new Error(`Change file does not contain ${missingProjects.join(',')}.`);
        }
    }
    static getChangeComments(newChangeFilePaths) {
        const changes = new Map();
        newChangeFilePaths.forEach((filePath) => {
            console.log(`Found change file: ${filePath}`);
            const changeRequest = node_core_library_1.JsonFile.load(filePath);
            if (changeRequest && changeRequest.changes) {
                changeRequest.changes.forEach(change => {
                    if (!changes.get(change.packageName)) {
                        changes.set(change.packageName, []);
                    }
                    if (change.comment && change.comment.length) {
                        changes.get(change.packageName).push(change.comment);
                    }
                });
            }
            else {
                throw new Error(`Invalid change file: ${filePath}`);
            }
        });
        return changes;
    }
    constructor(changesPath) {
        this._changesPath = changesPath;
    }
    /**
     * Get the array of absolute paths of change files.
     */
    getFiles() {
        if (this._files) {
            return this._files;
        }
        this._files = glob.sync(`${this._changesPath}/**/*.json`);
        return this._files || [];
    }
    /**
     * Get the path of changes folder.
     */
    getChangesPath() {
        return this._changesPath;
    }
    /**
     * Delete all change files
     */
    deleteAll(shouldDelete, updatedChangelogs) {
        if (updatedChangelogs) {
            // Skip changes files if the package's change log is not updated.
            const packagesToInclude = new Set();
            updatedChangelogs.forEach((changelog) => {
                packagesToInclude.add(changelog.name);
            });
            const filesToDelete = this.getFiles().filter((filePath) => {
                const changeRequest = node_core_library_1.JsonFile.load(filePath);
                for (const changeInfo of changeRequest.changes) {
                    if (!packagesToInclude.has(changeInfo.packageName)) {
                        return false;
                    }
                }
                return true;
            });
            return this._deleteFiles(filesToDelete, shouldDelete);
        }
        else {
            // Delete all change files.
            return this._deleteFiles(this.getFiles(), shouldDelete);
        }
    }
    _deleteFiles(files, shouldDelete) {
        if (files.length) {
            console.log(`${os_1.EOL}* ` +
                `${shouldDelete ? 'DELETING:' : 'DRYRUN: Deleting'} ` +
                `${files.length} change file(s).`);
            for (const filePath of files) {
                console.log(` - ${filePath}`);
                if (shouldDelete) {
                    Utilities_1.Utilities.deleteFile(filePath);
                }
            }
        }
        return files.length;
    }
}
exports.ChangeFiles = ChangeFiles;
//# sourceMappingURL=ChangeFiles.js.map