'use strict';
import { createResourceMeta } from '../../core/resource-meta.js';
import { fdMetaCachePrefixes } from '../constants/fd-meta-cache-prefixes.js';
import { getResourcePermissionChecker } from '../auth/permissions/resource-permission-checker-provider.js';
import { isManyToManyResource, getManyToManyInfo } from '../relations/many-to-many-relations.js';
import { getMetaOrderBys } from '../api/odata/odata-metas.js';
import { getResourceDescription, getUserFriendlyName } from '../props/property-names.js';
import { getInternalResourceMeta } from '../resources/internal-resource-meta.js';
import { getNewUuid } from '../../../../fd-toolbox/uuids.js';
import { tryGetFromCoreCache, setCoreCache } from '../../../../fd-toolbox/server/cachings/core-cache.js';
import { createPropertyMeta } from '../../core/property-meta.js';
import { setBasePropertyTypeInfo } from './property-metas.js';
import { generateDisplayName } from '../meta-core/display-name.js';
import { getDynamicMetaProvider } from '../basic-crud/resource-meta-handlers.js';
import { dynamicMetaProviderTypes } from '../constants/meta-handler-types.js';
import { pluralizeStrict } from '../../../../fd-toolbox/server/strings/strings.js';
import { crudActions } from '../../../../fd-toolbox/enums/enums.js';
import { getResourceMetaIdServer, saveResourceMetaIdToCacheServer } from './resource-name-converter.js';
import { sortProperties } from '../../meta-sort.js';

var __defProp=Object['defineProperty'],__name=(_0xee4a58,_0x46c0de)=>__defProp(_0xee4a58,'name',{'value':_0x46c0de,'configurable':!![]});function getResourceCacheKey(_0x166db1){return fdMetaCachePrefixes['resourceCacheKeyPrefix']['concat'](_0x166db1);}__name(getResourceCacheKey,'getResourceCacheKey');async function getResourceMetas(_0x1f4a4c){const _0x34e2ef=_0x1f4a4c['map'](getResourceMeta),_0x5cf037=await Promise['all'](_0x34e2ef),_0x224125=Object['fromEntries'](_0x5cf037['map'](_0x65de42=>[_0x65de42['resourceName'],_0x65de42]));return _0x224125;}__name(getResourceMetas,'getResourceMetas');async function getResourceMeta(_0x3361fe){const _0xf82ccb=await getResourceMetaIdServer(_0x3361fe);if(_0xf82ccb){const _0x3bbc57=await tryGetFromCoreCache(getResourceCacheKey(_0xf82ccb));if(_0x3bbc57)return _0x3bbc57;}const _0x57f52b=await getInternalResourceMeta(_0x3361fe),_0x54511c=createResourceMeta();_0x54511c['resourceName']=_0x57f52b['name'],_0x54511c['display']['displayName']=await getDisplayName(_0x57f52b),_0x54511c['icon']=_0x57f52b['icon'],_0x54511c['id']=_0x57f52b['id'],_0x54511c['description']=getResourceDescription(_0x57f52b),_0x54511c['properties']=await getPropertyMeta(_0x54511c),_0x54511c['orderBys']=await getMetaOrderBys(_0x54511c),_0x54511c['display']['pluralName']=_0x57f52b['pluralName']??pluralizeStrict(_0x54511c['display']['displayName']),_0x54511c['display']['preferPluralName']=_0x57f52b['preferPluralName'],_0x54511c['display']['preferSingularName']=_0x57f52b['preferSingularName'];if(_0x54511c['singleton']){if(_0x57f52b['userSingleton'])_0x54511c['singleton']['userSingleton']=!![];if(_0x57f52b['workspaceSingleton'])_0x54511c['singleton']['workspaceSingleton']=!![];if(_0x57f52b['systemSingleton'])_0x54511c['singleton']['systemSingleton']=!![];}const _0x274977=await fillFromDynamicMetaConfig(_0x54511c);await fillSupportedCrudActions(_0x54511c,_0x274977),await fillResourceMetaByInternalMeta(_0x54511c);const _0x317e70=await getResourcePermissionChecker(_0x54511c['resourceName']);return _0x317e70&&(_0x54511c['customPermissionChecker']=!![]),await isManyToManyResource(_0x57f52b['name'])&&(_0x54511c['manyToManyInfo']=await getManyToManyInfo(await getInternalResourceMeta(_0x54511c['resourceName']))),await saveResourceMetaIdToCacheServer(_0x54511c['resourceName'],_0x54511c['id']),await setCoreCache(getResourceCacheKey(_0x54511c['id']),_0x54511c),_0x54511c;}__name(getResourceMeta,'getResourceMeta');async function getPropertyMeta(_0xcc146a){const _0x41c8ca={},_0x48f7cf=await getInternalResourceMeta(_0xcc146a['resourceName']),_0x5c54ee=_0x48f7cf['properties']??{};for(const [_0x2dac39,_0x4696e1]of Object['entries'](_0x5c54ee)){const _0x4a30b1=_0x4696e1['displayName']??await getUserFriendlyName(_0x2dac39),_0x41f489=createPropertyMeta(_0x4696e1['resourcePropertyId']?.['oldResourcePropertyId']??getNewUuid(),_0x4a30b1,_0x2dac39);if(_0x4696e1['isBlobImage'])_0x41f489['isBlobImage']=!![];if(_0x4696e1['priority'])_0x41f489['ui']={'priority':_0x4696e1['priority']};if(_0x4696e1['fileUpload']!==void(0x1a*0xc5+0xd*0x29+-0x1617))_0x41f489['fileUploadType']=_0x4696e1['fileUpload'];if(_0x4696e1['isDynamic'])_0x41f489['isDynamic']=_0x4696e1['isDynamic'];await setBasePropertyTypeInfo(_0x41f489,_0xcc146a),_0x41c8ca[_0x41f489['name']]=_0x41f489;}return sortProperties(_0x41c8ca,![]);}__name(getPropertyMeta,'getPropertyMeta');async function fillSupportedCrudActions(_0x570a7d,_0x25e811){if(_0x25e811?.['allowedCrudActions'])for(const _0x319b9e of _0x25e811['allowedCrudActions']){_0x570a7d['supportedCrudActions']['push'](_0x319b9e);}else await fillSupportedCrudActionByMetaConfig(_0x570a7d);}__name(fillSupportedCrudActions,'fillSupportedCrudActions');async function fillSupportedCrudActionByMetaConfig(_0x19d60a){const _0x26c57f=await getInternalResourceMeta(_0x19d60a['resourceName']);if(_0x26c57f['createOnlyResource'])_0x19d60a['supportedCrudActions']['push'](crudActions['create']);else {if(_0x26c57f['getOnlyResource'])_0x19d60a['supportedCrudActions']['push'](crudActions['view']);else _0x26c57f['allowedCrudActions']?_0x19d60a['supportedCrudActions']['push'](..._0x26c57f['allowedCrudActions']):_0x19d60a['supportedCrudActions']['push'](crudActions['create'],crudActions['view'],crudActions['update'],crudActions['delete']);}}__name(fillSupportedCrudActionByMetaConfig,'fillSupportedCrudActionByMetaConfig');async function fillFromDynamicMetaConfig(_0x23071f){const _0x1dc958=await getInternalResourceMeta(_0x23071f['resourceName']);if(!_0x1dc958['dynamicMetaProviders']?.['includes'](dynamicMetaProviderTypes['getDynamicMetaConfig']))return void(0xb*0x2df+0x141e+-0x33b3);const _0x24daf1=(await getDynamicMetaProvider(_0x23071f['resourceName'],dynamicMetaProviderTypes['getDynamicMetaConfig']))?.();if(!_0x24daf1)return void(0x5d5+-0xb*-0x362+-0x2b0b);if(_0x24daf1['auth'])for(const _0x427e23 of _0x24daf1['auth']['belowLevelAllowedCrudByUserId']){!_0x23071f['auth']['belowLevelAllowedCrudByUserId']['includes'](_0x427e23)&&_0x23071f['auth']['belowLevelAllowedCrudByUserId']['push'](_0x427e23);}for(const [_0x47bae4,_0x4ec603]of Object['entries'](_0x24daf1['properties'])){const _0x4f943b=_0x23071f['properties'][_0x47bae4];_0x4f943b['ui']['priority']??=_0x4ec603['priority'],_0x4f943b['isDisplayable']=_0x4ec603['isDisplayable']??_0x4f943b['isDisplayable'],_0x4f943b['isOutsideEditable']=_0x4ec603['isOutsideEditable']??_0x4f943b['isOutsideEditable'];}return _0x24daf1;}__name(fillFromDynamicMetaConfig,'fillFromDynamicMetaConfig');async function fillResourceMetaByInternalMeta(_0x1ea922){const _0x4e70b0=await getInternalResourceMeta(_0x1ea922['resourceName']);if(_0x4e70b0['accessByUserId']){for(const _0x369408 of _0x1ea922['supportedCrudActions']){!_0x1ea922['auth']['belowLevelAllowedCrudByUserId']['includes'](_0x369408)&&_0x1ea922['auth']['belowLevelAllowedCrudByUserId']['push'](_0x369408);}_0x1ea922['auth']['accessByUserId']=!![];}if(_0x4e70b0['accessByWorkspaceId']){for(const _0x27bcee of _0x1ea922['supportedCrudActions']){_0x1ea922['auth']['belowLevelAllowedCrudByWorkspaceId']['push'](_0x27bcee);}_0x1ea922['auth']['accessByWorkspaceId']=!![];}_0x4e70b0['transientResource']&&(_0x1ea922['isPersistent']=![]);}__name(fillResourceMetaByInternalMeta,'fillResourceMetaByInternalMeta');async function getDisplayName(_0x535660){return _0x535660['displayName']??generateDisplayName(_0x535660['name'],_0x535660['preferPluralName']);}__name(getDisplayName,'getDisplayName');

export { getPropertyMeta, getResourceCacheKey, getResourceMeta, getResourceMetas };
