// 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 } from '@rushstack/node-core-library';
import { Colorize } from '@rushstack/terminal';
import schemaJson from '../../schemas/deploy-scenario.schema.json';
import { RushConstants } from '../RushConstants';
export class DeployScenarioConfiguration {
    constructor(json, projectJsonsByName) {
        this.json = json;
        this.projectJsonsByName = projectJsonsByName;
    }
    /**
     * Validates that the input string conforms to the naming rules for a "rush deploy" scenario name.
     */
    static validateScenarioName(scenarioName) {
        if (!scenarioName) {
            throw new Error('The scenario name cannot be an empty string');
        }
        if (!this._scenarioNameRegExp.test(scenarioName)) {
            throw new Error(`"${scenarioName}" is not a valid scenario name. The name must be comprised of` +
                ' lowercase letters and numbers, separated by single hyphens. Example: "my-scenario"');
        }
    }
    /**
     * Given the --scenarioName value, return the full path of the filename.
     *
     * Example: "ftp-site" --> "...common/config/rush/deploy-ftp-site.json"
     * Example: undefined --> "...common/config/rush/deploy.json"
     */
    static getConfigFilePath(scenarioName, rushConfiguration) {
        let scenarioFileName;
        if (scenarioName) {
            DeployScenarioConfiguration.validateScenarioName(scenarioName);
            scenarioFileName = `deploy-${scenarioName}.json`;
        }
        else {
            scenarioFileName = `deploy.json`;
        }
        return path.join(rushConfiguration.commonRushConfigFolder, scenarioFileName);
    }
    static loadFromFile(terminal, scenarioFilePath, rushConfiguration) {
        if (!FileSystem.exists(scenarioFilePath)) {
            throw new Error('The scenario config file was not found: ' + scenarioFilePath);
        }
        terminal.writeLine(Colorize.cyan(`Loading deployment scenario: ${scenarioFilePath}`));
        const deployScenarioJson = JsonFile.loadAndValidate(scenarioFilePath, DeployScenarioConfiguration._jsonSchema);
        // Apply the defaults
        if (!deployScenarioJson.linkCreation) {
            deployScenarioJson.linkCreation = 'default';
        }
        const deployScenarioProjectJsonsByName = new Map();
        for (const projectSetting of deployScenarioJson.projectSettings || []) {
            // Validate projectSetting.projectName
            if (!rushConfiguration.getProjectByName(projectSetting.projectName)) {
                throw new Error(`The "projectSettings" section refers to the project name "${projectSetting.projectName}"` +
                    ` which was not found in ${RushConstants.rushJsonFilename}`);
            }
            for (const additionalProjectsToInclude of projectSetting.additionalProjectsToInclude || []) {
                if (!rushConfiguration.getProjectByName(projectSetting.projectName)) {
                    throw new Error(`The "additionalProjectsToInclude" setting refers to the` +
                        ` project name "${additionalProjectsToInclude}" which was not found in ${RushConstants.rushJsonFilename}`);
                }
            }
            deployScenarioProjectJsonsByName.set(projectSetting.projectName, projectSetting);
        }
        return new DeployScenarioConfiguration(deployScenarioJson, deployScenarioProjectJsonsByName);
    }
}
// Used by validateScenarioName()
// Matches lowercase words separated by dashes.
// Example: "deploy-the-thing123"
DeployScenarioConfiguration._scenarioNameRegExp = /^[a-z0-9]+(-[a-z0-9]+)*$/;
DeployScenarioConfiguration._jsonSchema = JsonSchema.fromLoadedObject(schemaJson);
//# sourceMappingURL=DeployScenarioConfiguration.js.map