export default class BuildHostRunstateChanger {
    _DEST_RUNSTATE_TO_CURRENT_RUNSTATE_MAP = {
        running: ["running"],
        stopped: ["suspended", "stopped"],
        suspended: ["suspended", "stopped"]
    };
    cloudClient;
    environmentId;
    constructor(environmentId, cloudClient) {
        this.environmentId = environmentId;
        this.cloudClient = cloudClient;
    }
    async changeRunstate(destRunstate) {
        const config = await this.cloudClient.get(`/configurations/${this.environmentId}`);
        if (config.runstate === "busy") {
            throw new Error("Build Host is busy. Please wait a moment and try again.");
        }
        if (this._DEST_RUNSTATE_TO_CURRENT_RUNSTATE_MAP[destRunstate].includes(config.runstate)) {
            return;
        }
        await this.cloudClient.put(`/configurations/${this.environmentId}`, { runstate: destRunstate });
    }
    async start() {
        return this.changeRunstate("running");
    }
    async stop() {
        return this.changeRunstate("stopped");
    }
    async suspend() {
        return this.changeRunstate("suspended");
    }
}
