"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../../utils/constants");
const utilities_1 = require("../../utils/utilities");
const resource_1 = __importDefault(require("../base/resource"));
const projects_1 = __importDefault(require("../projects/projects"));
class EnvironmentTypeConfig extends resource_1.default {
    constructor(id, clientSession, parentApi, parentId) {
        super(clientSession, 'environmentTypeConfig', id, parentApi);
        this._parentId = parentId;
        this._clientSession = clientSession;
    }
    async associate() {
        await (0, utilities_1.sleep)(constants_1.DEFLAKE_DELAY_IN_MILLISECONDS); //Avoid throttling
        return this._axiosInstance.put(`${this._api}/relationships`);
    }
    async disassociate() {
        await (0, utilities_1.sleep)(constants_1.DEFLAKE_DELAY_IN_MILLISECONDS); //Avoid throttling
        return this._axiosInstance.delete(`${this._api}/relationships`);
    }
    projects() {
        return new projects_1.default(this._clientSession, this._api);
    }
    async cleanup() {
        var _a;
        const defAdminSession = await this._setup.getDefaultAdminSession();
        await (0, utilities_1.sleep)(constants_1.DEFLAKE_DELAY_IN_MILLISECONDS); //Avoid throttling when terminating multiple environment type configs
        const { data: associatedProjects } = await defAdminSession.resources.environmentTypes
            .environmentType(this._parentId)
            .configurations()
            .environmentTypeConfig(this._id)
            .projects()
            .get();
        if (associatedProjects.data) {
            await Promise.all((_a = associatedProjects.data) === null || _a === void 0 ? void 0 : _a.map(async (project) => {
                await defAdminSession.resources.projects
                    .project(project.id)
                    .environmentTypes()
                    .environmentType(this._parentId)
                    .configurations()
                    .environmentTypeConfig(this._id)
                    .disassociate();
            }));
        }
        await defAdminSession.resources.environmentTypes
            .environmentType(this._parentId)
            .configurations()
            .environmentTypeConfig(this._id)
            .delete();
    }
}
exports.default = EnvironmentTypeConfig;
//# sourceMappingURL=environmentTypeConfig.js.map