"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.functionsRmCommand = exports.FUNCTIONS_RM_SUBCOMMAND = void 0;
const cli_1 = require("@remotion/cli");
const lambda_client_1 = require("@remotion/lambda-client");
const constants_1 = require("@remotion/lambda-client/constants");
const get_function_info_1 = require("../../../api/get-function-info");
const get_aws_region_1 = require("../../get-aws-region");
const confirm_1 = require("../../helpers/confirm");
const quit_1 = require("../../helpers/quit");
const log_1 = require("../../log");
const index_1 = require("./index");
const ls_1 = require("./ls");
exports.FUNCTIONS_RM_SUBCOMMAND = 'rm';
const LEFT_COL = 16;
const functionsRmCommand = async (args, logLevel) => {
    if (args.length === 0) {
        log_1.Log.error({ indent: false, logLevel }, 'No function name passed.');
        log_1.Log.error({ indent: false, logLevel }, 'Pass another argument which is the name of the function you would like to remove.');
        log_1.Log.info({ indent: false, logLevel }, `You can run \`${constants_1.BINARY_NAME} ${index_1.FUNCTIONS_COMMAND} ${ls_1.FUNCTIONS_LS_SUBCOMMAND}\` to see a list of deployed Lambda functions.`);
        (0, quit_1.quit)(1);
    }
    if (args[0] === '()') {
        log_1.Log.info({ indent: false, logLevel }, 'No functions to remove.');
        return;
    }
    const region = (0, get_aws_region_1.getAwsRegion)();
    for (const functionName of args) {
        const infoOutput = cli_1.CliInternals.createOverwriteableCliOutput({
            quiet: cli_1.CliInternals.quietFlagProvided(),
            cancelSignal: null,
            // No browser logs
            updatesDontOverwrite: false,
            indent: false,
            logLevel,
        });
        infoOutput.update('Getting function info...', false);
        const info = await (0, get_function_info_1.getFunctionInfo)({
            region,
            functionName,
        });
        infoOutput.update([
            'Function name: '.padEnd(LEFT_COL, ' ') + ' ' + info.functionName,
            'Memory: '.padEnd(LEFT_COL, ' ') + ' ' + info.memorySizeInMb + 'MB',
            'Timeout: '.padEnd(LEFT_COL, ' ') + ' ' + info.timeoutInSeconds + 'sec',
            'Version: '.padEnd(LEFT_COL, ' ') + ' ' + info.version,
        ].join('\n'), true);
        if (!(await (0, confirm_1.confirmCli)({ delMessage: 'Delete? (Y/n)', allowForceFlag: true }))) {
            (0, quit_1.quit)(1);
        }
        const output = cli_1.CliInternals.createOverwriteableCliOutput({
            quiet: cli_1.CliInternals.quietFlagProvided(),
            cancelSignal: null,
            updatesDontOverwrite: cli_1.CliInternals.shouldUseNonOverlayingLogger({
                logLevel,
            }),
            indent: false,
            logLevel,
        });
        output.update('Deleting...', false);
        await (0, lambda_client_1.deleteFunction)({ region, functionName });
        output.update('Deleted!', true);
    }
};
exports.functionsRmCommand = functionsRmCommand;
