'use strict';
import { withIdProps } from '../../../fd-toolbox-core/types/resource-with-id.js';
import { apiPost } from '../../../fd-toolbox/api/api-client.js';
import { notify } from '../../../fd-toolbox/notifications.js';
import { routes } from '../../../fd-toolbox/routing/routes.js';
import { fetchUiResourceMeta } from '../ui-meta-providers.js';
import { isObject, isString } from '../../../fd-toolbox/types/ensure-type.js';
import { defaultFileName, fileTypes, notifications } from '../../../fd-toolbox/constants/internal-file-constants.js';
import { httpContentTypes } from '../../../fd-toolbox/constants/header-names.js';
import { wellKnownUserProps } from '../../../fd-toolbox/constants/common-user-fields.js';
import { first } from '../../../fd-toolbox/server/collections/single-funcs.js';
import { htmlTags } from '../../../base-repo/constants/html-element-names.js';

var __defProp=Object['defineProperty'],__name=(_0x3aa16a,_0x594a83)=>__defProp(_0x3aa16a,'name',{'value':_0x594a83,'configurable':!![]});function downloadFile({data:_0x11066e,fileName:_0x808c1c,mimeType:_0x3107c4}){const _0x4ea595=new Blob([_0x11066e],{'type':_0x3107c4??httpContentTypes['contentTypeHeaderOctetStream']}),_0x3cbc4d=URL['createObjectURL'](_0x4ea595),_0x5e2a29=createDownloadLink(_0x3cbc4d,_0x808c1c??defaultFileName);document['body']['appendChild'](_0x5e2a29),_0x5e2a29['click'](),_0x5e2a29['remove'](),URL['revokeObjectURL'](_0x3cbc4d);}__name(downloadFile,'downloadFile');async function exportTableItemsInCSVFile(_0x47c171,_0x20ea42){if(_0x20ea42){const _0x53623e=_0x20ea42['getSelectedRows']();if(_0x53623e&&_0x53623e['length']>0x25*0x1b+-0x311+-0xd6){const _0x5ce079=await fetchUiResourceMeta(_0x47c171),_0x151687=_0x53623e['map'](({item:_0x30dbd0})=>formatItemForExport(_0x30dbd0,_0x5ce079)),_0x20ece4=await apiPost(routes['export'],{'fileName':_0x47c171+'.csv','value':_0x151687});_0x20ece4?.['data']&&(downloadFile({'data':_0x20ece4['data'],'fileName':''+_0x47c171+fileTypes['csv']['extensions'],'mimeType':fileTypes['csv']['mimeType']}),notify(notifications['exportCompleted']));}else notify(notifications['noItemsSelected']);}}__name(exportTableItemsInCSVFile,'exportTableItemsInCSVFile');function createDownloadLink(_0x1a3bcd,_0x13121b){const _0x427b42=document['createElement'](htmlTags['a']);return _0x427b42['href']=_0x1a3bcd,_0x427b42['download']=_0x13121b,_0x427b42;}__name(createDownloadLink,'createDownloadLink');function isEmailObject(_0x473009){return isObject(_0x473009)&&wellKnownUserProps['email']in _0x473009;}__name(isEmailObject,'isEmailObject');function processArrayValue(_0x4d6d01){const _0x4adf30=first(_0x4d6d01);if(_0x4d6d01['length']>0x1*-0x17ab+0x8c5+0x2*0x773&&isObject(_0x4adf30)&&wellKnownUserProps['email']in _0x4adf30)return _0x4d6d01['map'](_0x85e62e=>{if(isEmailObject(_0x85e62e))return _0x85e62e[wellKnownUserProps['email']];return String(_0x85e62e);})['join'](',\x20');return _0x4d6d01['join'](',\x20');}__name(processArrayValue,'processArrayValue');function processArrayProperties(_0x41d804){const _0x103c92={'id':_0x41d804['id']||''};for(const [_0x561e05,_0x491f9a]of Object['entries'](_0x41d804)){Array['isArray'](_0x491f9a)?_0x103c92[_0x561e05]=processArrayValue(_0x491f9a):_0x103c92[_0x561e05]=_0x491f9a;}return _0x103c92;}__name(processArrayProperties,'processArrayProperties');function formatItemForExport(_0x1b747a,_0x13dddb){const _0x4ebcd7=processArrayProperties(_0x1b747a),_0x159b0a={'id':''},_0x65198a=isString(_0x4ebcd7['id'])?_0x4ebcd7['id']:_0x4ebcd7['id']??'';for(const [_0x4ce36b,_0x2a6361]of Object['entries'](_0x4ebcd7)){const _0x347aca=_0x13dddb['resourceMeta']['properties'][_0x4ce36b],_0x2094af=_0x347aca?.['displayName']||_0x4ce36b;_0x4ce36b!==withIdProps['id']&&(_0x159b0a[_0x2094af]=_0x2a6361??'');}return _0x159b0a['id']=_0x65198a,_0x159b0a;}__name(formatItemForExport,'formatItemForExport');

export { downloadFile, exportTableItemsInCSVFile };
