'use strict';
'use client';
import { supportedImageTypes, supportedVideoTypes } from '../../../../fd-toolbox/constants/file-loader-data-types.js';
import { fieldValuesSeparator } from '../../../../fd-toolbox/constants/constants.js';
import { apiGetBlob } from '../../../../fd-toolbox/api/api-client.js';
import { uploadBlobs, deleteBlob } from '../../edit-resource/services/blob-components.js';
import { promiseStatuses } from '../../../../fd-toolbox-core/constants/promises.js';
import { contentFormatElements } from '../../../../fd-toolbox/constants/html-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x5bfa99,_0x14de53)=>__defProp(_0x5bfa99,'name',{'value':_0x14de53,'configurable':!![]});async function processMediaFiles(_0x2b8021,_0x2cb80b,_0x368c3e,_0x20d398,_0x53de08,_0xeedbf3,_0x56a8b3){if(!_0x368c3e)return;const _0x11328e=Array['from'](_0x368c3e),_0x737c78=await getFilesUrlAndPreview(_0x11328e,[],_0x2cb80b);_0x2b8021(_0x1838b1=>[..._0x1838b1,..._0x737c78]);if(_0xeedbf3||!_0x53de08)_0x56a8b3?.(_0x11328e,[]);else {const _0x481438=await uploadBlobs(_0x11328e,_0x53de08,_0x20d398?.['relation']?.['relatedPropertyName']),_0x43f134=await getFilesUrlAndPreview(_0x11328e,_0x481438,_0x2cb80b);_0x56a8b3?.(_0x11328e,_0x481438),_0x2b8021(_0xa170=>[..._0xa170['filter'](_0x5bc44a=>!_0x737c78['includes'](_0x5bc44a)),..._0x43f134]);}}__name(processMediaFiles,'processMediaFiles');async function getFilesUrlAndPreview(_0x3fee5c,_0x36c455,_0x3dac03){const _0x5b7572=[],_0xce6f84=_0x3fee5c['map'](async(_0x291df3,_0x25aa74)=>{const _0x134207=await processFile(_0x291df3,_0x36c455?.[_0x25aa74],_0x3dac03);if(_0x134207)_0x5b7572['push'](_0x134207);});return await Promise['all'](_0xce6f84),_0x5b7572;}__name(getFilesUrlAndPreview,'getFilesUrlAndPreview');async function deleteFile(_0x50243a,_0x3dd3ba,_0x1db7bd){_0x1db7bd(_0x51019f=>_0x51019f['filter']((_0x1acb75,_0x2cc3f1)=>_0x2cc3f1!==_0x50243a)),await deleteBlob(_0x3dd3ba[_0x50243a]['url']);}__name(deleteFile,'deleteFile');function onFileUploadHandler(_0x3d23d7,_0x3ecaf9,_0x3459b9,_0x12236e,_0x491909,_0x2f3e4d,_0x18ac40,_0x54cfde,_0x276975){_0x3d23d7&&(_0x3459b9(void(0x950+0x130b*0x2+-0x2f66)),processMediaFiles(_0x3ecaf9,_0x3459b9,_0x491909??void(0x1caf+-0xf36+-0xd79),_0x2f3e4d,_0x18ac40,_0x54cfde,_0x276975)),_0x12236e&&(_0x12236e['value']='');}__name(onFileUploadHandler,'onFileUploadHandler');async function onInitUpload(_0x24fa1b,_0x24149c){const _0x575519=_0x24149c?_0x24149c['split'](fieldValuesSeparator):[],_0x42fa16=await Promise['allSettled'](_0x575519['map'](_0x1c03b7=>apiGetBlob(_0x1c03b7['slice'](_0x1c03b7['lastIndexOf']('/')+(0xfa2+0x4*-0x577+0x63b))))),_0x43f256=_0x42fa16['filter'](_0x5604ff=>_0x5604ff['status']===promiseStatuses['fulfilled']&&_0x5604ff['value']instanceof Blob),_0x31741c=_0x43f256['map']((_0x4b5267,_0x5095c8)=>createFileFromBlob(_0x4b5267['value'],_0x575519[_0x5095c8])),_0x45047a=await getFilesUrlAndPreview(_0x31741c,_0x575519);_0x24fa1b(_0x45047a);}__name(onInitUpload,'onInitUpload');async function handleFileDelete(_0x526481,_0x30d84f,_0x4718e3,_0x45fe2d,_0x11e850){_0x30d84f['length']===-0x3fb*0x1+-0xc*0x5f+0x870&&_0x45fe2d(''),await deleteFile(_0x526481,_0x30d84f,_0x4718e3),_0x11e850?.(_0x30d84f['map'](_0x22cebc=>_0x22cebc['url']),_0x30d84f[_0x526481]['url']);}__name(handleFileDelete,'handleFileDelete');function createFileFromBlob(_0x5b5eac,_0x94dba0){const _0x5288bd=_0x94dba0['slice'](_0x94dba0['lastIndexOf']('/')+(-0x701+0x3a4+-0x1af*-0x2));return new File([_0x5b5eac],_0x5288bd,{'type':_0x5b5eac['type']});}__name(createFileFromBlob,'createFileFromBlob');function getVideoPreview(_0x4924e3,_0x129754=0x1f1e+0x17*0x3b+-0x2468){const _0x44aaf6=document['createElement'](contentFormatElements['video']);return _0x44aaf6['src']=_0x4924e3,new Promise(_0x152a7c=>{_0x44aaf6['onloadeddata']=()=>{_0x44aaf6['currentTime']=_0x129754,_0x44aaf6['onseeked']=()=>{const _0x43c838=document['createElement']('canvas'),_0x3dbf2e=_0x43c838['getContext']('2d');if(_0x3dbf2e){_0x43c838['width']=_0x44aaf6['videoWidth'],_0x43c838['height']=_0x44aaf6['videoHeight'],_0x3dbf2e['drawImage'](_0x44aaf6,-0x1af5+0x1*0x18b9+0x23c,-0x1*-0x1551+0x32*0x14+-0x1939,_0x43c838['width'],_0x43c838['height']);const _0x253101=_0x43c838['toDataURL'](supportedImageTypes[-0xd85+0xb54+0x2*0x119]);_0x152a7c(_0x253101);}};};});}__name(getVideoPreview,'getVideoPreview');async function processFile(_0x25c6c8,_0x5c2212,_0x4ec1ad){const _0x122c53=_0x5c2212??URL['createObjectURL'](_0x25c6c8);let _0x195422;if(supportedImageTypes['includes'](_0x25c6c8['type']))_0x195422={'url':_0x122c53};else {if(supportedVideoTypes['includes'](_0x25c6c8['type'])){const _0x5656bc=await getVideoPreview(_0x122c53);_0x195422={'url':_0x122c53,'preview':_0x5656bc};}else _0x4ec1ad?.('Unsupported\x20file\x20type:\x20'+_0x25c6c8['type']);}return _0x195422;}__name(processFile,'processFile');

export { deleteFile, getFilesUrlAndPreview, handleFileDelete, onFileUploadHandler, onInitUpload, processMediaFiles };
