'use strict';
import { S3Client, ListObjectsCommand, PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
import { getStringEnvSetting } from '../../../../fd-toolbox/infra/env-functions.js';
import { envSettingKeys } from '../../../common/infra/env-setting-keys.js';
import { getNowWithoutTimezone } from '../../../../fd-toolbox/time/date-time.js';
import { remoteBlobConstants } from '../../../../base-repo/constants/remote-blob-constants.js';
import { httpContentTypes } from '../../../../fd-toolbox/constants/header-names.js';
import { readFile } from 'fs/promises';
import { formatBlobName } from '../../../../fd-toolbox/http/url/urls.js';
import { getBlobUrl } from '../../../meta/core/resources/blob/blob-urls.js';

var __defProp=Object['defineProperty'],__name=(_0x654d2e,_0x56308a)=>__defProp(_0x654d2e,'name',{'value':_0x56308a,'configurable':!![]});const defaultBucketName=getStringEnvSetting(envSettingKeys['s3CompatibleBucketName'])??'',endpoint=getStringEnvSetting(envSettingKeys['s3CompatibleEndpoint'])??'',accessKey=getStringEnvSetting(envSettingKeys['s3CompatibleAccessKey'])??'',secretAccessKey=getStringEnvSetting(envSettingKeys['s3CompatibleSecretAccessKey'])??'',blobClient=new S3Client({'region':remoteBlobConstants['blobClientRegion'],'endpoint':endpoint,'credentials':{'accessKeyId':accessKey,'secretAccessKey':secretAccessKey}});async function getRemoteBlobs(_0x5b5937){const _0x83a947={'Bucket':defaultBucketName},_0x329ecc=await blobClient['send'](new ListObjectsCommand(_0x83a947));if(!_0x329ecc['Contents'])return [];let _0x18955d=_0x329ecc['Contents']['map'](_0x525ac4=>({'key':_0x525ac4['Key'],'url':getBlobUrl(_0x525ac4['Key']),'isRemote':!![]}));return _0x5b5937&&(_0x18955d=_0x18955d['filter'](_0x5c6d26=>_0x5c6d26['key']['includes'](_0x5b5937))),_0x18955d;}__name(getRemoteBlobs,'getRemoteBlobs');async function uploadRemoteBlob(_0x3e57de,_0x2a7bea,_0x2b1a9e){const _0x5ff050={'Bucket':defaultBucketName,'Key':_0x3e57de,'Body':Buffer['from'](_0x2b1a9e),'ContentType':_0x2a7bea};return await blobClient['send'](new PutObjectCommand(_0x5ff050)),_0x5ff050['Key']&&getBlobUrl(_0x5ff050['Key']);}__name(uploadRemoteBlob,'uploadRemoteBlob');async function getRemoteBlobContent(_0x2e190e){const _0x143fdf=new GetObjectCommand({'Bucket':defaultBucketName,'Key':_0x2e190e}),_0x53ada9=await blobClient['send'](_0x143fdf),_0x30d32c=await _0x53ada9['Body']?.['transformToByteArray']();if(!_0x30d32c)return void(-0xda1+-0x1a2e+0x4f*0x81);const _0x4759ec={'content':_0x30d32c,'contentType':_0x53ada9?.['ContentType']??httpContentTypes['contentTypeHeaderOctetStream'],'blobName':_0x2e190e};return _0x4759ec;}__name(getRemoteBlobContent,'getRemoteBlobContent');async function uploadFileToRemote(_0xecd8cf){const _0x19f03b=await readFile(_0xecd8cf),_0x302795={'Bucket':defaultBucketName,'Key':getNowWithoutTimezone()+'_'+formatBlobName(_0xecd8cf),'Body':_0x19f03b,'ContentType':httpContentTypes['contentTypeHeaderOctetStream']};return await blobClient['send'](new PutObjectCommand(_0x302795)),_0x302795['Key']&&getBlobUrl(_0x302795['Key']);}__name(uploadFileToRemote,'uploadFileToRemote');

export { getRemoteBlobContent, getRemoteBlobs, uploadFileToRemote, uploadRemoteBlob };
