"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CobuildConfiguration = void 0;
const node_crypto_1 = require("node:crypto");
const node_core_library_1 = require("@rushstack/node-core-library");
const EnvironmentConfiguration_1 = require("./EnvironmentConfiguration");
const RushConstants_1 = require("../logic/RushConstants");
const cobuild_schema_json_1 = __importDefault(require("../schemas/cobuild.schema.json"));
/**
 * Use this class to load and save the "common/config/rush/cobuild.json" config file.
 * This file provides configuration options for the Rush Cobuild feature.
 * @beta
 */
class CobuildConfiguration {
    constructor(options) {
        var _a, _b;
        const { cobuildJson, cobuildLockProviderFactory, rushConfiguration } = options;
        this.cobuildContextId = EnvironmentConfiguration_1.EnvironmentConfiguration.cobuildContextId;
        this.cobuildFeatureEnabled = this.cobuildContextId ? cobuildJson.cobuildFeatureEnabled : false;
        this.cobuildRunnerId = EnvironmentConfiguration_1.EnvironmentConfiguration.cobuildRunnerId || (0, node_crypto_1.randomUUID)();
        this.cobuildLeafProjectLogOnlyAllowed =
            (_a = EnvironmentConfiguration_1.EnvironmentConfiguration.cobuildLeafProjectLogOnlyAllowed) !== null && _a !== void 0 ? _a : false;
        this.cobuildWithoutCacheAllowed =
            (_b = rushConfiguration.experimentsConfiguration.configuration.allowCobuildWithoutCache) !== null && _b !== void 0 ? _b : false;
        this._cobuildLockProviderFactory = cobuildLockProviderFactory;
        this._cobuildJson = cobuildJson;
    }
    /**
     * Attempts to load the cobuild.json data from the standard file path `common/config/rush/cobuild.json`.
     * If the file has not been created yet, then undefined is returned.
     */
    static async tryLoadAsync(terminal, rushConfiguration, rushSession) {
        const jsonFilePath = CobuildConfiguration.getCobuildConfigFilePath(rushConfiguration);
        try {
            return await CobuildConfiguration._loadAsync(jsonFilePath, terminal, rushConfiguration, rushSession);
        }
        catch (err) {
            if (!node_core_library_1.FileSystem.isNotExistError(err)) {
                throw err;
            }
        }
    }
    static getCobuildConfigFilePath(rushConfiguration) {
        return `${rushConfiguration.commonRushConfigFolder}/${RushConstants_1.RushConstants.cobuildFilename}`;
    }
    static async _loadAsync(jsonFilePath, terminal, rushConfiguration, rushSession) {
        let cobuildJson;
        try {
            cobuildJson = await node_core_library_1.JsonFile.loadAndValidateAsync(jsonFilePath, CobuildConfiguration._jsonSchema);
        }
        catch (e) {
            if (node_core_library_1.FileSystem.isNotExistError(e)) {
                return undefined;
            }
            throw e;
        }
        if (!(cobuildJson === null || cobuildJson === void 0 ? void 0 : cobuildJson.cobuildFeatureEnabled)) {
            return undefined;
        }
        const cobuildLockProviderFactory = rushSession.getCobuildLockProviderFactory(cobuildJson.cobuildLockProvider);
        if (!cobuildLockProviderFactory) {
            throw new Error(`Unexpected cobuild lock provider: ${cobuildJson.cobuildLockProvider}`);
        }
        return new CobuildConfiguration({
            cobuildJson,
            rushConfiguration,
            rushSession,
            cobuildLockProviderFactory
        });
    }
    async createLockProviderAsync(terminal) {
        if (this.cobuildFeatureEnabled) {
            terminal.writeLine(`Running cobuild (runner ${this.cobuildContextId}/${this.cobuildRunnerId})`);
            const cobuildLockProvider = await this._cobuildLockProviderFactory(this._cobuildJson);
            this._cobuildLockProvider = cobuildLockProvider;
            await this._cobuildLockProvider.connectAsync();
        }
    }
    async destroyLockProviderAsync() {
        var _a;
        if (this.cobuildFeatureEnabled) {
            await ((_a = this._cobuildLockProvider) === null || _a === void 0 ? void 0 : _a.disconnectAsync());
        }
    }
    getCobuildLockProvider() {
        if (!this._cobuildLockProvider) {
            throw new Error(`Cobuild lock provider has not been created`);
        }
        return this._cobuildLockProvider;
    }
}
exports.CobuildConfiguration = CobuildConfiguration;
CobuildConfiguration._jsonSchema = node_core_library_1.JsonSchema.fromLoadedObject(cobuild_schema_json_1.default);
//# sourceMappingURL=CobuildConfiguration.js.map