"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"));
class Environment extends resource_1.default {
    constructor(id, clientSession, parentApi, projectId) {
        super(clientSession, 'environment', id, parentApi);
        this._projectId = projectId;
    }
    async connect() {
        await (0, utilities_1.sleep)(constants_1.DEFLAKE_DELAY_IN_MILLISECONDS); //Avoid throttling when connecting to multiple environments
        return this._axiosInstance.get(`${this._api}/connections`);
    }
    async stop() {
        await (0, utilities_1.sleep)(constants_1.DEFLAKE_DELAY_IN_MILLISECONDS); //Avoid throttling when stopping multiple environments
        return this._axiosInstance.put(`${this._api}/stop`);
    }
    async start() {
        await (0, utilities_1.sleep)(constants_1.DEFLAKE_DELAY_IN_MILLISECONDS); //Avoid throttling when starting multiple environments
        return this._axiosInstance.put(`${this._api}/start`);
    }
    async terminate() {
        await (0, utilities_1.sleep)(constants_1.DEFLAKE_DELAY_IN_MILLISECONDS); //Avoid throttling when terminating multiple environments
        return this._axiosInstance.put(`${this._api}/terminate`);
    }
    async cleanup() {
        const defAdminSession = await this._setup.getDefaultAdminSession();
        const { data: resource } = await defAdminSession.resources.projects
            .project(this._projectId)
            .environments()
            .environment(this._id)
            .get();
        let envStatus = resource.status;
        if (['TERMINATED'].includes(envStatus)) {
            // Exit early because environment has already been terminated
            return;
        }
        try {
            console.log(`Attempting to delete environment ${this._id}. This will take a few minutes.`);
            await (0, utilities_1.poll)(async () => defAdminSession.resources.projects
                .project(this._projectId)
                .environments()
                .environment(this._id)
                .get(), (env) => { var _a; return !['PENDING', 'STOPPING', 'STARTING'].includes((_a = env === null || env === void 0 ? void 0 : env.data) === null || _a === void 0 ? void 0 : _a.status); }, constants_1.ENVIRONMENT_START_MAX_WAITING_SECONDS);
            if (!['STOPPED', 'TERMINATING'].includes(envStatus)) {
                console.log(`Environment must be stopped to terminate. Currently in state ${envStatus}. Stopping environment ${this._id}.`);
                await defAdminSession.resources.projects
                    .project(this._projectId)
                    .environments()
                    .environment(this._id)
                    .stop();
                await (0, utilities_1.poll)(async () => defAdminSession.resources.projects
                    .project(this._projectId)
                    .environments()
                    .environment(this._id)
                    .get(), (env) => { var _a; return ((_a = env === null || env === void 0 ? void 0 : env.data) === null || _a === void 0 ? void 0 : _a.status) === 'STOPPED'; }, constants_1.ENVIRONMENT_START_MAX_WAITING_SECONDS);
            }
            const { data: completedResource } = await defAdminSession.resources.projects
                .project(this._projectId)
                .environments()
                .environment(this._id)
                .get();
            envStatus = completedResource.status;
            console.log(`Terminating environment ${this._id}.`);
            await defAdminSession.resources.projects
                .project(this._projectId)
                .environments()
                .environment(this._id)
                .terminate();
            await (0, utilities_1.poll)(async () => defAdminSession.resources.projects
                .project(this._projectId)
                .environments()
                .environment(this._id)
                .get(), (env) => { var _a; return ((_a = env === null || env === void 0 ? void 0 : env.data) === null || _a === void 0 ? void 0 : _a.status) === 'TERMINATED'; }, constants_1.ENVIRONMENT_START_MAX_WAITING_SECONDS);
            console.log(`Deleted environment ${this._id}`);
        }
        catch (e) {
            console.log(`Could not delete environment. Last known status for env ${this._id} was "${envStatus}". 
        Encountered error: ${e}`);
        }
    }
}
exports.default = Environment;
//# sourceMappingURL=environment.js.map