// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { FileSystem, JsonFile, Objects } from '@rushstack/node-core-library';
/**
 * A base class for flag file.
 * @internal
 */
export class FlagFile {
    /**
     * Creates a new flag file
     * @param folderPath - the folder that this flag is managing
     * @param state - optional, the state that should be managed or compared
     */
    constructor(folderPath, flagName, initialState) {
        this.path = `${folderPath}/${flagName}.flag`;
        this._state = initialState;
    }
    /**
     * Returns true if the file exists and the contents match the current state.
     */
    async isValidAsync() {
        let oldState;
        try {
            oldState = await JsonFile.loadAsync(this.path);
            const newState = this._state;
            return Objects.areDeepEqual(oldState, newState);
        }
        catch (err) {
            return false;
        }
    }
    /**
     * Writes the flag file to disk with the current state
     */
    async createAsync() {
        await JsonFile.saveAsync(this._state, this.path, {
            ensureFolderExists: true
        });
    }
    /**
     * Removes the flag file
     */
    async clearAsync() {
        await FileSystem.deleteFileAsync(this.path);
    }
}
//# sourceMappingURL=FlagFile.js.map