"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const client_lambda_1 = require("@aws-sdk/client-lambda");
const lambda_client_1 = require("@remotion/lambda-client");
const version_1 = require("remotion/version");
const get_regions_1 = require("../api/get-regions");
const quit_1 = require("../cli/helpers/quit");
const layerInfo = {
    'ap-northeast-1': [],
    'ap-south-1': [],
    'ap-southeast-1': [],
    'ap-southeast-2': [],
    'eu-central-1': [],
    'eu-west-1': [],
    'eu-west-2': [],
    'us-east-1': [],
    'us-east-2': [],
    'us-west-2': [],
    'af-south-1': [],
    'ap-east-1': [],
    'ap-northeast-2': [],
    'ap-northeast-3': [],
    'ca-central-1': [],
    'eu-north-1': [],
    'eu-south-1': [],
    'eu-west-3': [],
    'sa-east-1': [],
    'us-west-1': [],
    'ap-southeast-4': [],
    'ap-southeast-5': [],
    'eu-central-2': [],
};
const getBucketName = (region) => {
    return `remotionlambda-binaries-${region}`;
};
const parseRegionFlag = () => {
    var _a;
    const arg = process.argv
        .slice(2)
        .find((a) => a.startsWith('--region=') || a === '--region');
    if (!arg)
        return null;
    if (arg === '--region') {
        const idx = process.argv.indexOf('--region');
        return ((_a = process.argv[idx + 1]) !== null && _a !== void 0 ? _a : null);
    }
    return arg.split('=')[1];
};
const parseSkipFlag = () => {
    var _a;
    const arg = process.argv
        .slice(2)
        .find((a) => a.startsWith('--skip=') || a === '--skip');
    if (!arg)
        return [];
    const value = arg === '--skip'
        ? ((_a = process.argv[process.argv.indexOf('--skip') + 1]) !== null && _a !== void 0 ? _a : '')
        : arg.split('=')[1];
    return value
        .split(',')
        .map((s) => s.trim())
        .filter(Boolean);
};
const makeLayerPublic = async () => {
    const runtimes = ['nodejs24.x'];
    const layers = [
        'fonts',
        'chromium',
        'emoji-apple',
        'emoji-google',
        'cjk',
    ];
    const onlyRegion = parseRegionFlag();
    const skipRegions = parseSkipFlag();
    const regions = onlyRegion
        ? [onlyRegion]
        : (0, get_regions_1.getRegions)().filter((r) => !skipRegions.includes(r));
    if (onlyRegion) {
        console.log(`Filtering to region: ${onlyRegion}`);
    }
    if (skipRegions.length > 0) {
        console.log(`Skipping regions: ${skipRegions.join(', ')}`);
    }
    for (const region of regions) {
        for (const layer of layers) {
            const layerName = `remotion-binaries-${layer}-arm64`;
            const { Version, LayerArn } = await lambda_client_1.LambdaClientInternals.getLambdaClient(region, undefined, null).send(new client_lambda_1.PublishLayerVersionCommand({
                Content: {
                    S3Bucket: getBucketName(region),
                    S3Key: `remotion-layer-${layer}-v18-arm64.zip`,
                },
                LayerName: layerName,
                LicenseInfo: layer === 'chromium'
                    ? 'Chromium 149.0.7790.0, compiled from source. Read Chromium License: https://chromium.googlesource.com/chromium/src/+/refs/heads/main/LICENSE'
                    : layer === 'emoji-apple'
                        ? 'Apple Emojis (https://github.com/samuelngs/apple-emoji-linux). For educational purposes only - Apple is a trademark of Apple Inc., registered in the U.S. and other countries.'
                        : layer === 'emoji-google'
                            ? 'Google Emojis (https://github.com/googlefonts/noto-emoji)'
                            : layer === 'cjk'
                                ? 'Noto Sans (Chinese, Japanese, Korean)'
                                : 'Contains Noto Sans font. Read Noto Sans License: https://fonts.google.com/noto/specimen/Noto+Sans/about',
                CompatibleRuntimes: runtimes,
                Description: version_1.VERSION,
            }));
            await lambda_client_1.LambdaClientInternals.getLambdaClient(region, undefined, undefined).send(new client_lambda_1.AddLayerVersionPermissionCommand({
                Action: 'lambda:GetLayerVersion',
                LayerName: layerName,
                Principal: '*',
                VersionNumber: Version,
                StatementId: 'public-layer',
            }));
            if (!layerInfo[region]) {
                layerInfo[region] = [];
            }
            if (!LayerArn) {
                throw new Error('layerArn is null');
            }
            if (!Version) {
                throw new Error('Version is null');
            }
            layerInfo[region].push({
                layerArn: LayerArn,
                version: Version,
            });
            console.log({ LayerArn, Version });
        }
    }
};
makeLayerPublic()
    .then(() => {
    console.log('\n\n\n\n\n');
    console.log(JSON.stringify(layerInfo, null, 2));
})
    .catch((err) => {
    console.log(err);
    (0, quit_1.quit)(1);
});
