import { Command, flags } from '@heroku-cli/command';
import { Args, ux } from '@oclif/core';
import redisApi from '../../lib/redis/api.js';
export default class Timeout extends Command {
    static args = {
        database: Args.string({ description: 'name of the Key-Value Store database. If omitted, it defaults to the primary database associated with the app.' }),
    };
    static description = `set the number of seconds to wait before killing idle connections
    A value of zero means that connections will not be closed.
  `;
    static flags = {
        app: flags.app({ required: true }),
        remote: flags.remote(),
        seconds: flags.integer({ char: 's', description: 'set timeout value', required: true }),
    };
    static topic = 'redis';
    async run() {
        const { args, flags } = await this.parse(Timeout);
        const { app, seconds } = flags;
        const { database } = args;
        const addon = await redisApi(app, database, false, this.heroku).getRedisAddon();
        const { body: response } = await redisApi(app, database, false, this.heroku)
            .request(`/redis/v0/databases/${addon.id}/config`, 'PATCH', { timeout: seconds });
        ux.stdout(`Timeout for ${addon.name} (${addon.config_vars.join(', ')}) set to ${response.timeout.value} seconds.`);
        if (response.timeout.value === 0) {
            ux.stdout('Connections to the Redis instance can idle indefinitely.');
        }
        else {
            ux.stdout(`Connections to the Redis instance will be stopped after idling for ${response.timeout.value} seconds.`);
        }
    }
}
