"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaabHelper = void 0;
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
const createDataSetRequestParser_1 = require("@aws/swb-app/lib/dataSets/createDataSetRequestParser");
const setup_1 = __importDefault(require("../setup"));
const randomTextGenerator_1 = __importDefault(require("../utils/randomTextGenerator"));
class PaabHelper {
    constructor(numberOfProjects, outputError) {
        this._numberOfProjects = 3;
        this._setup = setup_1.default.getSetup();
        this._randomTextGenerator = new randomTextGenerator_1.default(this._setup.getSettings().get('runId'));
        this._outputError = outputError;
        if (numberOfProjects)
            this._numberOfProjects = numberOfProjects;
    }
    async createResources(filename) {
        if (this._numberOfProjects > 3) {
            throw new Error('PaabHelper cannot support more than 3 projects');
        }
        // create IT admin session
        const adminSession = await this._setup.getDefaultAdminSession(this._outputError);
        const anonymousSession = await this._setup.createAnonymousSession();
        // Example: lib/integration-tests/tests/isolated/datasets/create.test.ts => isolated-datasets-create
        const testName = filename
            .replace(/.test.js/g, '')
            .split('/')
            .slice(-3)
            .join('-');
        console.log(testName);
        // set up new cost center
        const { data: costCenter } = await adminSession.resources.costCenters.create({
            name: this._randomTextGenerator.getFakeText('fakeCostCenterName'),
            accountId: this._setup.getSettings().get('defaultHostingAccountId'),
            description: 'Cost Center for integ test'
        });
        const projectIds = [];
        for (let i = 1; i <= this._numberOfProjects; i++) {
            const projectName = `Project${i}`;
            const projectResponse = await adminSession.resources.projects.create({
                name: testName + this._randomTextGenerator.getFakeText(projectName),
                description: `${projectName} for integ tests ${testName}`,
                costCenterId: costCenter.id
            });
            projectIds.push(projectResponse.data.id);
        }
        const [project1Id, project2Id, project3Id] = projectIds;
        // create PA1, PA2, Researcher1 sessions
        const pa1Session = await this._setup.getSessionForUserType('projectAdmin1', this._outputError);
        const pa2Session = await this._setup.getSessionForUserType('projectAdmin2', this._outputError);
        const rs1Session = await this._setup.getSessionForUserType('researcher1', this._outputError);
        // associate users with corresponding projects properly (as IT Admin)
        if (this._numberOfProjects >= 1) {
            await adminSession.resources.projects
                .project(project1Id)
                .assignUserToProject(pa1Session.getUserId(), { role: 'ProjectAdmin' });
            await adminSession.resources.projects
                .project(project1Id)
                .assignUserToProject(rs1Session.getUserId(), { role: 'Researcher' });
        }
        if (this._numberOfProjects >= 2) {
            await adminSession.resources.projects
                .project(project2Id)
                .assignUserToProject(pa2Session.getUserId(), { role: 'ProjectAdmin' });
        }
        if (this._numberOfProjects === 3) {
            await adminSession.resources.projects
                .project(project3Id)
                .assignUserToProject(pa1Session.getUserId(), { role: 'ProjectAdmin' });
            await adminSession.resources.projects
                .project(project3Id)
                .assignUserToProject(rs1Session.getUserId(), { role: 'Researcher' });
        }
        return {
            adminSession,
            pa1Session,
            pa2Session,
            rs1Session,
            anonymousSession,
            project1Id,
            project2Id,
            project3Id
        };
    }
    createDatasetRequest(projectId) {
        const settings = this._setup.getSettings();
        const dataSetName = this._randomTextGenerator.getFakeText('integration-test-dataSet');
        return createDataSetRequestParser_1.CreateDataSetRequestParser.parse({
            storageName: settings.get('DataSetsBucketName'),
            awsAccountId: settings.get('mainAccountId'),
            path: dataSetName,
            name: dataSetName,
            region: settings.get('awsRegion'),
            type: 'internal'
        });
    }
    async cleanup() {
        await this._setup.cleanup();
    }
}
exports.PaabHelper = PaabHelper;
//# sourceMappingURL=paabHelper.js.map