// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import { FileSystem, JsonFile, JsonSchema, User } from '@rushstack/node-core-library';
import { RushConstants } from '../logic/RushConstants';
import schemaJson from '../schemas/rush-user-settings.schema.json';
/**
 * Rush per-user configuration data.
 *
 * @beta
 */
export class RushUserConfiguration {
    constructor(rushUserConfigurationJson) {
        this.buildCacheFolder = rushUserConfigurationJson === null || rushUserConfigurationJson === void 0 ? void 0 : rushUserConfigurationJson.buildCacheFolder;
        if (this.buildCacheFolder && !path.isAbsolute(this.buildCacheFolder)) {
            throw new Error('buildCacheFolder must be an absolute path');
        }
    }
    static async initializeAsync() {
        const rushUserFolderPath = RushUserConfiguration.getRushUserFolderPath();
        const rushUserSettingsFilePath = path.join(rushUserFolderPath, 'settings.json');
        let rushUserSettingsJson;
        try {
            rushUserSettingsJson = await JsonFile.loadAndValidateAsync(rushUserSettingsFilePath, RushUserConfiguration._schema);
        }
        catch (e) {
            if (!FileSystem.isNotExistError(e)) {
                throw e;
            }
        }
        return new RushUserConfiguration(rushUserSettingsJson);
    }
    static getRushUserFolderPath() {
        const homeFolderPath = User.getHomeFolder();
        return `${homeFolderPath}/${RushConstants.rushUserConfigurationFolderName}`;
    }
}
RushUserConfiguration._schema = JsonSchema.fromLoadedObject(schemaJson);
//# sourceMappingURL=RushUserConfiguration.js.map