"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadMedia = exports.internalDownloadMedia = void 0;
const lambda_client_1 = require("@remotion/lambda-client");
const constants_1 = require("@remotion/lambda-client/constants");
const renderer_1 = require("@remotion/renderer");
const serverless_1 = require("@remotion/serverless");
const node_path_1 = __importDefault(require("node:path"));
const read_with_progress_1 = require("../functions/helpers/read-with-progress");
const internalDownloadMedia = async (input) => {
    var _a, _b, _c, _d, _e;
    const expectedBucketOwner = await input.providerSpecifics.getAccountId({
        region: input.region,
    });
    const overallProgress = await (0, serverless_1.getOverallProgressFromStorage)({
        bucketName: input.bucketName,
        expectedBucketOwner,
        region: input.region,
        renderId: input.renderId,
        providerSpecifics: input.providerSpecifics,
        forcePathStyle: input.forcePathStyle,
        requestHandler: input.requestHandler,
    });
    if (!overallProgress.renderMetadata) {
        throw new Error('Render did not finish yet');
    }
    const outputPath = node_path_1.default.resolve(process.cwd(), input.outPath);
    renderer_1.RenderInternals.ensureOutputDirectory(outputPath);
    const { key, renderBucketName, customCredentials } = (0, serverless_1.getExpectedOutName)({
        renderMetadata: overallProgress.renderMetadata,
        bucketName: input.bucketName,
        customCredentials: (_a = input.customCredentials) !== null && _a !== void 0 ? _a : null,
        bucketNamePrefix: constants_1.REMOTION_BUCKET_PREFIX,
    });
    const { sizeInBytes } = await (0, read_with_progress_1.lambdaDownloadFileWithProgress)({
        bucketName: renderBucketName,
        expectedBucketOwner,
        key,
        region: input.region,
        onProgress: (_b = input.onProgress) !== null && _b !== void 0 ? _b : (() => undefined),
        outputPath,
        customCredentials,
        logLevel: (_c = input.logLevel) !== null && _c !== void 0 ? _c : 'info',
        forcePathStyle: (_d = input.forcePathStyle) !== null && _d !== void 0 ? _d : false,
        requestHandler: (_e = input.requestHandler) !== null && _e !== void 0 ? _e : undefined,
        abortSignal: input.signal,
    });
    return {
        outputPath,
        sizeInBytes,
    };
};
exports.internalDownloadMedia = internalDownloadMedia;
/*
 * @description Downloads a rendered video, audio or still to the disk of the machine this API is called from.
 * @see [Documentation](https://remotion.dev/docs/lambda/downloadmedia)
 */
const downloadMedia = (input) => {
    var _a, _b, _c, _d, _e;
    return (0, exports.internalDownloadMedia)({
        ...input,
        providerSpecifics: lambda_client_1.LambdaClientInternals.awsImplementation,
        forcePathStyle: false,
        onProgress: (_a = input.onProgress) !== null && _a !== void 0 ? _a : (() => undefined),
        logLevel: (_b = input.logLevel) !== null && _b !== void 0 ? _b : 'info',
        customCredentials: (_c = input.customCredentials) !== null && _c !== void 0 ? _c : null,
        signal: (_d = input.signal) !== null && _d !== void 0 ? _d : new AbortController().signal,
        requestHandler: (_e = input.requestHandler) !== null && _e !== void 0 ? _e : undefined,
    });
};
exports.downloadMedia = downloadMedia;
