'use strict';
import { getS3Client } from '../tools/cloud-client.js';
import { environmentVariables } from '../constants/ci-constants.js';
import { GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
import { mkdir, writeFile, readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import _0x1c3e3a from 'node:path';
import { folders, files, buildPaths } from '../../../base-repo/app-constants/project-paths.js';
import { cliLogSubtitle } from '../../../fd-toolbox/logging/cli-logger.js';
import { httpContentTypes } from '../../../fd-toolbox/constants/header-names.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0xd2f542,_0x4410ba)=>__defProp(_0xd2f542,'name',{'value':_0x4410ba,'configurable':!![]});const rollupCacheKey=folders['rollupCache']+'/'+files['rollupCacheJson'],localCachePath=_0x1c3e3a['join'](buildPaths['rollupCache'],files['rollupCacheJson']);async function downloadRollupCache(){const _0x3efde5=getS3Client(),_0x59825a=getStringEnvSetting(environmentVariables['r2NxCacheBucketName']);if(!_0x3efde5||!_0x59825a){cliLogSubtitle('S3\x20client\x20or\x20bucket\x20not\x20configured,\x20skipping\x20rollup\x20cache\x20download.');return;}try{!existsSync(buildPaths['rollupCache'])&&await mkdir(buildPaths['rollupCache'],{'recursive':!![]});const _0x5e55c6=new GetObjectCommand({'Bucket':_0x59825a,'Key':rollupCacheKey}),_0x482c4a=await _0x3efde5['send'](_0x5e55c6);if(_0x482c4a['Body']){const _0x3b7dc4=await _0x482c4a['Body']['transformToByteArray'](),_0x19ecda=Buffer['from'](_0x3b7dc4)['toString']('utf-8');await writeFile(localCachePath,_0x19ecda,'utf-8');}}catch{cliLogSubtitle('Failed\x20to\x20download\x20rollup\x20cache\x20from\x20cloud,\x20continuing\x20without\x20cache...');}}__name(downloadRollupCache,'downloadRollupCache');async function uploadRollupCache(){const _0x2e3d10=getS3Client(),_0x1c0465=getStringEnvSetting(environmentVariables['r2NxCacheBucketName']);if(!_0x2e3d10||!_0x1c0465||!existsSync(localCachePath))return;try{const _0x1542e6=await readFile(localCachePath,'utf-8'),_0x372cbf=new PutObjectCommand({'Bucket':_0x1c0465,'Key':rollupCacheKey,'Body':_0x1542e6,'ContentType':httpContentTypes['contentTypeApplicationJson']});await _0x2e3d10['send'](_0x372cbf);}catch{cliLogSubtitle('Failed\x20to\x20upload\x20rollup\x20cache\x20to\x20cloud,\x20continuing...');}}__name(uploadRollupCache,'uploadRollupCache');

export { downloadRollupCache, uploadRollupCache };
