'use strict';
'use client';
import { apiPost, apiPut } from '../../../../fd-toolbox/api/api-client.js';
import { propertyTypes, relationTypes } from '../../../../fd-toolbox/enums/enums.js';
import { propertyTypeFormats } from '../../constants/property-type-formats.js';
import { combineEndpointPath } from '../../../../fd-toolbox/http/url/urls.js';
import { shouldBeAvailableForEditingOrCreate, isRichText, isBigText } from '../../core/property-types.js';
import { getValidators } from './form-validations.js';
import { getRelatedEditResourceConfig } from '../config/internal-edit-resource-configs.js';
import { handleValidationError, prepareEditedResource, ensureDefaultValueForPropertyType, getAllPropertyMetas } from './edit-resources.js';
import { getPropertyMeta } from './property-metas.js';
import { processRelation } from './edit-resource-relations.js';
import { uploadBlobs, uploadBlob, deleteBlob } from './blob-components.js';
import { getNewUuid } from '../../../../fd-toolbox/uuids.js';
import { editResourceInternalFormKeys } from '../edit-resources-constants.js';
import { isString } from '../../../../fd-toolbox/types/ensure-type.js';
import { getLoginState, setLoginState } from '../../../../fd-toolbox/auth/internal-login-states.js';
import { wellKnownMetaProps } from '../../../../fd-toolbox-core/constants/meta-constants.js';
import { getExtension } from '../../../../fd-toolbox/io/io.js';
import { getObjectValues } from '../../../../fd-toolbox/lib/utils.js';
import { userProps } from '../../core/resources/users/props/user-props.js';
import { getOptionalPropertyValue } from '../../../../fd-toolbox/objects/property-value.js';
import { getBlobUrl } from '../../core/resources/blob/blob-urls.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { getFormValues, getFormField } from '../edit-resource-validation.js';

var __defProp=Object['defineProperty'],__name=(_0x5a42a2,_0x478db6)=>__defProp(_0x5a42a2,'name',{'value':_0x478db6,'configurable':!![]});async function submitEditResource(_0x54e218,_0xe9b681,_0x2dbdb5,_0x179af5,_0x2328e9,_0x2ab573,_0x121dfe,_0x33b733){const _0x1f806f=_0x121dfe??_0x54e218['getValues']();try{const _0x392131=await getEditResourceForUpdate(_0x179af5,_0x1f806f,_0xe9b681),_0x453b2b=getOptionalPropertyValue(_0x392131['resource'],userProps['firstName']),_0x18c4bb=getOptionalPropertyValue(_0x392131['resource'],userProps['lastName']);updateLoginStateOnEditResourceSave(_0x392131,{'email':getOptionalPropertyValue(_0x392131['resource'],userProps['email']),'displayName':[_0x453b2b,_0x18c4bb]['join']('\x20')}),await createOrUpdateEditResource(_0x392131,_0x2dbdb5,_0x179af5,_0xe9b681,_0x54e218,_0x2328e9,_0x2ab573,_0x33b733);}catch(_0x3a3f3d){handleValidationError(_0x3a3f3d,_0x54e218);}}__name(submitEditResource,'submitEditResource');async function getEditResourceForUpdate(_0xb9661d,_0x296f2f,_0x302199){let _0x425dc0={..._0x302199};const _0x83424c=await Promise['all'](Object['keys'](_0x296f2f)['map'](_0x20c871=>getPropertyMeta(_0x20c871,_0xb9661d['resourceName']))),_0x348c85=_0x83424c['filter'](_0x18cbbe=>_0x18cbbe);prepareEditedResource(_0x348c85,_0x425dc0,_0x296f2f);let _0x3b4be4=[];return Object['entries'](_0x296f2f)['forEach'](([_0x3d2e60,_0x4d3558])=>{const _0x1dbdb6=_0xb9661d['properties'][_0x3d2e60];if(_0x1dbdb6)_0x3b4be4['push'](...processRelation(_0x3d2e60,_0x4d3558,_0x425dc0,_0x1dbdb6,_0x296f2f,_0x302199,_0xb9661d));}),{'resourceName':_0xb9661d['resourceName'],'resource':_0x425dc0,'propertyRelationSyncInfos':_0x3b4be4};}__name(getEditResourceForUpdate,'getEditResourceForUpdate');async function createOrUpdateEditResource(_0x3ae239,_0x3f18fc,_0x4a27d6,_0x1ee5e1,_0x206b0c,_0x37d59b,_0x1eae83,_0x1f182e){try{const _0x5e0555=await createOrUpdateResource(_0x3ae239,_0x3f18fc,_0x4a27d6,_0x1ee5e1,_0x206b0c);_0x1f182e?.();if(_0x3f18fc)_0x1eae83?.(!!_0x37d59b,_0x5e0555['id']);}catch(_0x3d60ae){handleValidationError(_0x3d60ae,_0x206b0c);}}__name(createOrUpdateEditResource,'createOrUpdateEditResource');async function createOrUpdateResource(_0x597804,_0x1fdaf2,_0xcd5bb,_0x39139b,_0x399777){await tryUploadImage(_0x39139b,_0x399777,_0x597804);let _0x34cd5b;_0x1fdaf2?_0x34cd5b=await apiPost(_0xcd5bb['resourceName'],_0x597804):_0x34cd5b=await apiPut(combineEndpointPath(_0xcd5bb['resourceName'],_0x597804['resource']['id']),_0x597804);const _0x1200bc={..._0x39139b};if(_0x1fdaf2&&_0x34cd5b['data']?.['result'])_0x1200bc['id']=_0x34cd5b['data']['result']['id'];if(_0x1fdaf2)await tryUploadBlobs(_0x1200bc,_0x399777,_0xcd5bb);return _0x34cd5b['data']?.['result'];}__name(createOrUpdateResource,'createOrUpdateResource');async function tryUploadBlobs(_0x14a753,_0x9d8e56,_0x5f411a){const _0x5f487d=getFormValues(_0x9d8e56),_0x70a782=getFormField(_0x9d8e56,editResourceInternalFormKeys['blobFiles']);_0x70a782&&wellKnownMetaProps['blobList']in _0x5f487d&&await uploadBlobs(_0x70a782,_0x14a753['id'],_0x5f411a['properties'][editResourceInternalFormKeys['blobFiles']]?.['relation']?.['relatedPropertyName']);}__name(tryUploadBlobs,'tryUploadBlobs');function createEditResourceForm(_0x2cfc63,_0xacef1,_0x3682e6,_0x22056b,_0x252ac9=![]){getObjectValues(_0xacef1['properties'])['forEach'](_0x1cd19e=>{if(_0x22056b(_0x1cd19e['name']))return;const _0x34797d=_0xacef1['resourceMeta']['properties'][_0x1cd19e['name']];if(shouldBeAvailableForEditingOrCreate(_0x1cd19e,_0x34797d)){const _0x2a8345=ensureDefaultValueForPropertyType(_0x34797d,_0x3682e6,_0x252ac9);_0x2cfc63(_0x1cd19e['name'],_0x2a8345);}else _0x1cd19e['name']===wellKnownMetaProps['imageUrl']&&_0x2cfc63(_0x1cd19e['name'],ensureDefaultValueForPropertyType(_0x34797d,_0x3682e6,_0x252ac9));});const _0x1cb7f7={};return getObjectValues(_0xacef1['properties'])['forEach'](_0xb737e3=>{const _0x1dbb0a=_0xacef1['resourceMeta']['properties'][_0xb737e3['name']];shouldBeAvailableForEditingOrCreate(_0xb737e3,_0x1dbb0a)&&(_0x1cb7f7[_0xb737e3['name']]=getValidators(_0x1dbb0a));}),_0x1cb7f7;}__name(createEditResourceForm,'createEditResourceForm');function isDefaultInputField(_0x42ce9b){const _0x3414ee=[propertyTypeFormats['enum'],propertyTypeFormats['flagsEnum'],propertyTypeFormats['dateTime'],propertyTypeFormats['dateOnly'],propertyTypeFormats['markdown']],_0xbb170f=[propertyTypes['integer'],propertyTypes['number'],propertyTypes['string']];return _0xbb170f['includes'](_0x42ce9b['type'])&&!_0x3414ee['some'](_0x37b276=>_0x37b276===_0x42ce9b['typeFormat'])&&!isRichText(_0x42ce9b['type'],_0x42ce9b['typeFormat'])&&!isBigText(_0x42ce9b['type'],_0x42ce9b['typeFormat'])&&!isImageField(_0x42ce9b);}__name(isDefaultInputField,'isDefaultInputField');function isImageField(_0x433e8b){return _0x433e8b['isBlobImage'];}__name(isImageField,'isImageField');function configureManageSection(_0x150e55,_0x154241){const _0x62a38=getObjectValues(_0x150e55['resourceMeta']['properties'])['filter'](_0x524f0=>_0x524f0['relation']&&[relationTypes['oneToMany'],relationTypes['manyToMany']]['includes'](_0x524f0['relation']['type'])),_0x167bc8=[];for(const _0x1e54a1 of _0x62a38){_0x167bc8['push']({'property':_0x1e54a1,'relatedResources':getRelatedResources(_0x154241,_0x1e54a1['name']),'linkElementId':_0x1e54a1['name']+'RelationUrl','editResourceConfig':getRelatedEditResourceConfig(_0x1e54a1['relation']['relatedResourceName'])});}return _0x167bc8;}__name(configureManageSection,'configureManageSection');function splitPropertyMetasLeftAndRight(_0x423c7c,_0x5580d7,_0x41a43d){let _0x16c1a4=getObjectValues(_0x41a43d['properties'])['filter'](_0x34c175=>{const _0x1c5a57=_0x41a43d['resourceMeta']['properties'][_0x34c175['name']];return shouldBeAvailableForEditingOrCreate(_0x34c175,_0x1c5a57);});for(let _0x2035f4=0xbb5*-0x1+0xb45+-0x70*-0x1;_0x2035f4<_0x16c1a4['length'];_0x2035f4++){_0x2035f4%(0x1bda+0x266d+-0x4245)===-0x61*-0x9+0x4*-0x41e+0x1*0xd0f?_0x423c7c[_0x16c1a4[_0x2035f4]['name']]=_0x16c1a4[_0x2035f4]:_0x5580d7[_0x16c1a4[_0x2035f4]['name']]=_0x16c1a4[_0x2035f4];}return [_0x423c7c,_0x5580d7];}__name(splitPropertyMetasLeftAndRight,'splitPropertyMetasLeftAndRight');function fillColumnData(_0x226494,_0xe3a078,_0x4e5191,_0x1e11fb,_0x5316c1){const [_0x54525d]=getAllPropertyMetas(_0x226494,_0x1e11fb);_0xe3a078(_0x54525d);if(_0x5316c1)_0x4e5191(_0x5316c1);}__name(fillColumnData,'fillColumnData');function onRelatedEditResourcesChange(_0x3a80e8,_0xd78e80){const [_0xbec67d,_0x39b990]=_0x3a80e8;_0xd78e80['has'](_0x39b990)&&_0xd78e80['delete'](_0x39b990),_0xd78e80['set'](_0x39b990,_0xbec67d);}__name(onRelatedEditResourcesChange,'onRelatedEditResourcesChange');function updateLoginStateOnEditResourceSave(_0x5b4963,_0x56dd4c){const _0x2ee9a2=getLoginState(),_0x40b5e7=_0x5b4963['resourceName']===resourceNames['user']||_0x5b4963['resourceName']===resourceNames['userSetting'],_0x116403=_0x2ee9a2['userId']===getOptionalPropertyValue(_0x5b4963['resource'],userProps['userId'])||_0x2ee9a2['userId']===getOptionalPropertyValue(_0x5b4963['resource'],wellKnownMetaProps['id']);_0x40b5e7&&_0x116403&&setLoginState({..._0x2ee9a2,..._0x56dd4c});}__name(updateLoginStateOnEditResourceSave,'updateLoginStateOnEditResourceSave');async function tryUploadImage(_0x3bd0f7,_0x109efd,_0x5567b4){const _0x418959=getFormValues(_0x109efd),_0x617098=_0x3bd0f7[wellKnownMetaProps['imageUrl']],_0xf9e2dd=getFormField(_0x109efd,editResourceInternalFormKeys['imageFile']);let _0x50a3fa;if(_0xf9e2dd&&wellKnownMetaProps['imageUrl']in _0x418959){_0x50a3fa=getNewUuid()+'.'+getExtension(_0xf9e2dd['name']),_0x5567b4['resource'][wellKnownMetaProps['imageUrl']]=getBlobUrl(_0x50a3fa),await uploadBlob(_0x50a3fa,_0xf9e2dd);if(_0x617098&&isString(_0x617098))deleteBlob(_0x617098);}return updateLoginStateOnEditResourceSave(_0x5567b4,{'imageUrl':getOptionalPropertyValue(_0x5567b4['resource'],userProps['imageUrl'])}),_0x50a3fa;}__name(tryUploadImage,'tryUploadImage');function getRelatedResources(_0x1510f3,_0x41663b){const _0x3a5614=getOptionalPropertyValue(_0x1510f3,_0x41663b);if(!Array['isArray'](_0x3a5614))return [];return _0x3a5614['map'](_0x30b8a4=>({'id':_0x30b8a4['id'],'isNew':![]}));}__name(getRelatedResources,'getRelatedResources');

export { configureManageSection, createEditResourceForm, createOrUpdateEditResource, createOrUpdateResource, fillColumnData, getEditResourceForUpdate, isDefaultInputField, isImageField, onRelatedEditResourcesChange, splitPropertyMetasLeftAndRight, submitEditResource, tryUploadBlobs, updateLoginStateOnEditResourceSave };
