"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.functionsDeploySubcommand = exports.FUNCTIONS_DEPLOY_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 version_1 = require("remotion/version");
const deploy_function_1 = require("../../../api/deploy-function");
const validate_custom_role_arn_1 = require("../../../shared/validate-custom-role-arn");
const validate_timeout_1 = require("../../../shared/validate-timeout");
const validate_vpc_security_group_ids_1 = require("../../../shared/validate-vpc-security-group-ids");
const validate_vpc_subnet_ids_1 = require("../../../shared/validate-vpc-subnet-ids");
const args_1 = require("../../args");
const get_aws_region_1 = require("../../get-aws-region");
const log_1 = require("../../log");
exports.FUNCTIONS_DEPLOY_SUBCOMMAND = 'deploy';
const functionsDeploySubcommand = async ({ logLevel, providerSpecifics, fullClientSpecifics, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    const region = (0, get_aws_region_1.getAwsRegion)();
    const timeoutInSeconds = (_a = args_1.parsedLambdaCli.timeout) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_TIMEOUT;
    const memorySizeInMb = (_b = args_1.parsedLambdaCli.memory) !== null && _b !== void 0 ? _b : constants_1.DEFAULT_MEMORY_SIZE;
    const diskSizeInMb = (_c = args_1.parsedLambdaCli.disk) !== null && _c !== void 0 ? _c : constants_1.DEFAULT_EPHEMERAL_STORAGE_IN_MB;
    const customRoleArn = (_d = args_1.parsedLambdaCli['custom-role-arn']) !== null && _d !== void 0 ? _d : undefined;
    const createCloudWatchLogGroup = !args_1.parsedLambdaCli['disable-cloudwatch'];
    const enableLambdaInsights = (_e = args_1.parsedLambdaCli['enable-lambda-insights']) !== null && _e !== void 0 ? _e : false;
    const cloudWatchLogRetentionPeriodInDays = (_f = args_1.parsedLambdaCli['retention-period']) !== null && _f !== void 0 ? _f : constants_1.DEFAULT_CLOUDWATCH_RETENTION_PERIOD;
    const enableV5Runtime = (_g = args_1.parsedLambdaCli['enable-v5-runtime']) !== null && _g !== void 0 ? _g : undefined;
    if (enableV5Runtime) {
        log_1.Log.warn({ logLevel, indent: false }, `The --enable-v5-runtime flag is now enabled by default and has no function anymore.`);
    }
    const vpcSubnetIds = (_h = args_1.parsedLambdaCli['vpc-subnet-ids']) !== null && _h !== void 0 ? _h : undefined;
    const vpcSecurityGroupIds = (_j = args_1.parsedLambdaCli['vpc-security-group-ids']) !== null && _j !== void 0 ? _j : undefined;
    const runtimePreference = (_k = args_1.parsedLambdaCli['runtime-preference']) !== null && _k !== void 0 ? _k : 'default';
    lambda_client_1.LambdaClientInternals.validateMemorySize(memorySizeInMb);
    (0, validate_timeout_1.validateTimeout)(timeoutInSeconds);
    lambda_client_1.LambdaClientInternals.validateDiskSizeInMb(diskSizeInMb);
    (0, validate_custom_role_arn_1.validateCustomRoleArn)(customRoleArn);
    (0, validate_vpc_subnet_ids_1.validateVpcSubnetIds)(vpcSubnetIds);
    (0, validate_vpc_security_group_ids_1.validateVpcSecurityGroupIds)(vpcSecurityGroupIds);
    if (!cli_1.CliInternals.quietFlagProvided()) {
        cli_1.CliInternals.Log.info({ indent: false, logLevel }, cli_1.CliInternals.chalk.gray(`
Region = ${region}
Memory = ${memorySizeInMb}MB
Disk size = ${diskSizeInMb}MB
Timeout = ${timeoutInSeconds}sec
Version = ${version_1.VERSION}
CloudWatch Logging Enabled = ${createCloudWatchLogGroup}
CloudWatch Retention Period = ${cloudWatchLogRetentionPeriodInDays} days
Lambda Insights Enabled = ${enableLambdaInsights}

				`.trim()));
        if (vpcSubnetIds) {
            cli_1.CliInternals.Log.info({ indent: false, logLevel }, `
VPC Subnet IDs = ${vpcSubnetIds}
VPC Security Group IDs = ${vpcSecurityGroupIds}
`.trim());
        }
    }
    const output = cli_1.CliInternals.createOverwriteableCliOutput({
        quiet: cli_1.CliInternals.quietFlagProvided(),
        cancelSignal: null,
        // No browser logs
        updatesDontOverwrite: false,
        indent: false,
        logLevel,
    });
    output.update('Deploying Lambda...', false);
    const { functionName, alreadyExisted } = await (0, deploy_function_1.internalDeployFunction)({
        createCloudWatchLogGroup,
        region,
        timeoutInSeconds,
        memorySizeInMb,
        cloudWatchLogRetentionPeriodInDays,
        diskSizeInMb,
        customRoleArn,
        enableLambdaInsights,
        indent: false,
        logLevel,
        vpcSubnetIds,
        vpcSecurityGroupIds,
        runtimePreference,
        providerSpecifics,
        fullClientSpecifics,
        requestHandler: null,
    });
    if (cli_1.CliInternals.quietFlagProvided()) {
        cli_1.CliInternals.Log.info({ indent: false, logLevel }, functionName);
    }
    else if (alreadyExisted) {
        output.update(`Already exists as ${functionName}`, true);
    }
    else {
        output.update(`Deployed as ${functionName}\n`, true);
    }
};
exports.functionsDeploySubcommand = functionsDeploySubcommand;
