'use strict';
import { Project, ts, ClassDeclaration } from 'ts-morph';
import _0x1869d0__default from 'path';
import { setInternalResourceMeta, getInternalResourceMeta } from './internal-resource-meta.js';
import { resourceIds } from '../../../../fd-toolbox/constants/resource-id-constants.js';
import { propertyTypes } from '../../properties/property-type.js';
import { internalPropertyMetas } from './internal-property-metas.js';
import { convertFromKebabToPascalCase } from '../../../../fd-toolbox/server/strings/casing.js';
import { metaPropertyNames } from '../constants/meta-property-names.js';
import { getEmptyResourceDescription } from '../../core/resource-description/resource-description.js';
import { baseResourceProps } from '../../../../fd-toolbox/base-resource/props/base-resource-props.js';
import { isExtendedResource } from './resource-checker.js';
import { formatRelativePathForImport } from '../modules/dynamic-imports.js';
import { resourceMetaPlugins } from './resource-meta-plugin-constants.js';
import { workspaceResourceProps } from '../../multitenancy/props/workspace-resource-props.js';
import { crudHandlerNames } from '../constants/crud-handler-types.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { dynamicMetaProviderTypes } from '../constants/meta-handler-types.js';
import { withIdProps } from '../../../../fd-toolbox-core/types/resource-with-id.js';
import { wellKnownMetaProps } from '../../../../fd-toolbox-core/constants/meta-constants.js';
import { getObjectValues } from '../../../../fd-toolbox/lib/utils.js';
import { oDataFilterTypes } from '../../../../fd-toolbox/odata/odata-enums.js';
import { files } from '../../../../base-repo/app-constants/project-paths.js';
import { jsBasicTypes } from '../../../../base-repo/constants/basic-types.js';

var __defProp=Object['defineProperty'],__name=(_0x42f445,_0x58ada6)=>__defProp(_0x42f445,'name',{'value':_0x58ada6,'configurable':!![]});let project,sourceFiles;function getSourceFiles(){project??=new Project({'tsConfigFilePath':_0x1869d0__default['join'](process['cwd'](),files['tsconfigResources'])});if(!sourceFiles)sourceFiles=project['getSourceFiles']();return sourceFiles;}__name(getSourceFiles,'getSourceFiles');function isDecoratedWithResourceId(_0x2326a2){return _0x2326a2['getDecorator'](resourceMetaPlugins['resourceIdDecoratorName'])!==void(0x1770+0x1*-0x9f1+0x1*-0xd7f);}__name(isDecoratedWithResourceId,'isDecoratedWithResourceId');const resourceNameRegex=/([\w-]{1,64})\.(handlers|resource)\.ts$/;async function initInternalResourceMetaHandlers(_0x24c85c){let _0x1ce4cb;const _0x352336=_0x24c85c['getFilePath']()['match'](resourceNameRegex)?.[-0x242b*-0x1+0x15d*0xa+-0x31cc*0x1];if(!_0x352336)return _0x1ce4cb;const _0x378710=convertFromKebabToPascalCase(_0x352336),_0x4077a0=Array['from'](_0x24c85c['getExportedDeclarations']()['keys']()),_0x10f974=_0x4077a0['filter'](_0x23eb63=>_0x23eb63 in crudHandlerNames),_0xf6b7f9=_0x4077a0['filter'](_0x755d0b=>_0x755d0b in dynamicMetaProviderTypes);let _0x345bf3={'name':_0x378710,'crudHandlers':_0x10f974,'dynamicMetaProviders':_0xf6b7f9};return await setInternalResourceMeta(Object['assign'](await getInternalResourceMeta(_0x378710,![]),_0x345bf3)),_0x1ce4cb=_0x378710,_0x1ce4cb;}__name(initInternalResourceMetaHandlers,'initInternalResourceMetaHandlers');async function initInternalResourceMeta(_0x4613cc,_0x5be4bf=![]){const _0x29b1ff=_0x4613cc['getName']();let _0x5b7b4b=_0x4613cc['getImplements']()['some'](_0x298107=>_0x298107['getExpression']()['getText']()===internalPropertyMetas['baseResourceName']);const _0x5e86e2=_0x4613cc['getProperties']();let _0x1bce1b,_0x3425fc=_0x4613cc['getBaseClass']();while(_0x3425fc!==void(-0x7*0x397+0x1263*-0x2+0x12b*0x35)){if(_0x5be4bf&&!_0x1bce1b&&!isExtendedResource(_0x3425fc))_0x1bce1b=_0x3425fc['getName']();if(_0x3425fc['getName']()===internalPropertyMetas['baseResourceName'])_0x5b7b4b=!![];if(!_0x5be4bf)_0x5e86e2['push'](..._0x3425fc['getProperties']());_0x3425fc=_0x3425fc['getBaseClass']();}const _0x58a915=await getInternalResourceMeta(_0x29b1ff,![]),_0x1b8e71=_0x58a915?.['properties']??{},_0x2a1a30=_0x58a915?.['description']??getEmptyResourceDescription(),_0x8627c1=resourceIds,_0x9b7372=generateInternalPropertiesMap(_0x5e86e2,_0x1b8e71,_0x2a1a30,_0x5b7b4b),_0xf7c8ae={..._0x58a915,'id':_0x8627c1[_0x29b1ff],'name':_0x29b1ff,'properties':{..._0x1b8e71,..._0x9b7372},'persistence':{'tableName':_0x29b1ff},'description':_0x2a1a30,'relativePath':formatRelativePathForImport(_0x4613cc['getSourceFile']()['getFilePath']())};if(_0x5be4bf)_0xf7c8ae['extendedFrom']=_0x1bce1b;const _0x529c5e=await getInternalResourceMeta(_0x29b1ff,![]),_0x1327a7=_0x529c5e['crudHandlers'],_0x3afc2d=_0x529c5e['dynamicMetaProviders'],_0x37013b={..._0xf7c8ae,'crudHandlers':_0x1327a7,'dynamicMetaProviders':_0x3afc2d};return await setInternalResourceMeta(_0x37013b),_0x29b1ff;}__name(initInternalResourceMeta,'initInternalResourceMeta');function createInternalPropertyMeta(_0x56fc11,_0x1b50db=![]){const _0x2bdc06=initializePropertyMeta(_0x56fc11);return _0x1b50db&&setBaseResourcePropertyConfigs(_0x2bdc06),configureSpecialProperties(_0x2bdc06),configurePropertyType(_0x56fc11,_0x2bdc06),setIsNavigationProperty(_0x56fc11,_0x2bdc06),_0x2bdc06;}__name(createInternalPropertyMeta,'createInternalPropertyMeta');function initializePropertyMeta(_0x37839a){const _0x3af2c0=_0x37839a['getType']()['getNonNullableType'](),_0x822df3={'required':!_0x37839a['getType']()['isNullable'](),'name':_0x37839a['getName'](),'type':oDataFilterTypes['default']};if(_0x3af2c0['isArray']()){const _0x5d2323=_0x3af2c0['getArrayElementType'](),_0xd0b459=_0x5d2323?.['getSymbol']()?.['getName']();if(_0xd0b459)_0x822df3['declaredWithType']=_0xd0b459;}else {const _0xa8f457=_0x3af2c0?.['getSymbol']()?.['getName']();if(_0xa8f457)_0x822df3['declaredWithType']=_0xa8f457;}return _0x822df3;}__name(initializePropertyMeta,'initializePropertyMeta');function configureSpecialProperties(_0x268bbb){_0x268bbb['name']===workspaceResourceProps['workspaceId']&&setWorkSpaceIdPropertyConfig(_0x268bbb);}__name(configureSpecialProperties,'configureSpecialProperties');function configurePropertyType(_0x3b541e,_0x10d7df){const _0x4faaa1=_0x3b541e['getType']()['getNonNullableType']();_0x10d7df['type']=setPropertyConfigType(_0x4faaa1,_0x10d7df['name']);if(_0x10d7df['type']===propertyTypes['number']){const _0x22c4b9=_0x3b541e['getTypeNode']();if(!_0x22c4b9||_0x22c4b9['getText']()===resourceMetaPlugins['number'])_0x10d7df['type']=propertyTypes['number'];else _0x22c4b9['getText']()===resourceMetaPlugins['Integer']?_0x10d7df['type']=propertyTypes['integer']:configureEnum(_0x22c4b9,_0x10d7df);}}__name(configurePropertyType,'configurePropertyType');function getTypeAliasByName(_0x3d0fb1){return getSourceFiles()['flatMap'](_0x536b47=>_0x536b47['getTypeAliases']())['find'](_0x5b6c43=>_0x5b6c43['getName']()===_0x3d0fb1);}__name(getTypeAliasByName,'getTypeAliasByName');function getVariableNameFromTypeAliasFullText(_0x221234){const _0x289700=/typeof\s+(\w+)/,_0x5f042a=_0x289700['exec'](_0x221234);return _0x5f042a?_0x5f042a[-0xfbe+-0x5*-0xf0+0xb0f]:void(0x19*-0x167+-0x188a+0xb*0x56b);}__name(getVariableNameFromTypeAliasFullText,'getVariableNameFromTypeAliasFullText');function getVariableDeclarationByName(_0x557e14){return getSourceFiles()['find'](_0x3064f8=>!!_0x3064f8['getVariableDeclaration'](_0x557e14))?.['getVariableDeclaration'](_0x557e14);}__name(getVariableDeclarationByName,'getVariableDeclarationByName');function setPropertyConfigType(_0xd9d92d,_0x12f26d){let _0x568d2c;if(_0x12f26d['endsWith'](metaPropertyNames['idEndingConst'])||_0x12f26d===withIdProps['id'])_0x568d2c=propertyTypes['uuid'];else {if(_0xd9d92d['isString']())_0x568d2c=propertyTypes['string'];else {if(_0xd9d92d['isNumber']())_0x568d2c=propertyTypes['number'];else {if(_0xd9d92d['isBoolean']())_0x568d2c=propertyTypes['boolean'];else {if(_0xd9d92d['isArray']())_0x568d2c=propertyTypes['array'];else _0xd9d92d['isObject']()&&(_0xd9d92d['getSymbol']()?.['getName']()===jsBasicTypes['date']?_0x568d2c=propertyTypes['dateTime']:_0x568d2c=propertyTypes['object']);}}}}return _0x568d2c;}__name(setPropertyConfigType,'setPropertyConfigType');function configureEnum(_0x17518d,_0x2160d1){const _0x2aa47b=getTypeAliasByName(_0x17518d['getText']());if(!_0x2aa47b)throw createError(resourceMetaPlugins['typeAliasNotSupported']);const _0x7685=getVariableNameFromTypeAliasFullText(_0x2aa47b['getFullText']());if(!_0x7685)throw createError(resourceMetaPlugins['enumVarNameNotFound']);const _0x4cc5b2=getVariableDeclarationByName(_0x7685);if(!_0x4cc5b2)throw createError(resourceMetaPlugins['enumVarDeclarationNotFound']);_0x2160d1['type']=propertyTypes['integer'],_0x2160d1['colorful']=!![],_0x2160d1['enumerationDisplayName']=[],_0x2160d1['enumerationValues']=[],_0x4cc5b2['getInitializer']()?.['getChildAtIndex'](0x59c+0x2*0x83a+-0x160f)['getChildren']()['forEach'](_0x36fe97=>{_0x36fe97['isKind'](ts['SyntaxKind']['PropertyAssignment'])&&(_0x2160d1['enumerationDisplayName']['push'](_0x36fe97['getChildAtIndexIfKindOrThrow'](0x19*0x8f+0x1c00+-0x29f7,ts['SyntaxKind']['Identifier'])['getText']()),_0x2160d1['enumerationValues']['push'](parseInt(_0x36fe97['getChildAtIndexIfKindOrThrow'](0x13*-0xe3+0x1*-0xe21+0xa54*0x3,ts['SyntaxKind']['NumericLiteral'])['getText']())));});}__name(configureEnum,'configureEnum');function setBaseResourcePropertyConfigs(_0x59fcd6){const _0x2e50e0=getObjectValues(baseResourceProps);_0x2e50e0['includes'](_0x59fcd6['name'])&&(_0x59fcd6['notEditableFromOutside']=!![],_0x59fcd6['notDisplayable']=!![]);}__name(setBaseResourcePropertyConfigs,'setBaseResourcePropertyConfigs');function setWorkSpaceIdPropertyConfig(_0xa4afed){_0xa4afed['isViewVisible']=!![],_0xa4afed['notDisplayable']=!![];}__name(setWorkSpaceIdPropertyConfig,'setWorkSpaceIdPropertyConfig');function setIsNavigationProperty(_0x53bbc8,_0xa052ed){let _0x57539f=![];const _0x5bdde8=_0x53bbc8['getType']()['getNonNullableType']();if(_0xa052ed['type']===propertyTypes['array']){const _0x420ccc=_0x5bdde8['getArrayElementType']();if(_0x420ccc){const _0x51e00c=_0x420ccc['getSymbol']()?.['getDeclarations']()['find'](_0x5e80cc=>_0x5e80cc instanceof ClassDeclaration);_0x51e00c&&isDecoratedWithResourceId(_0x51e00c)&&(_0x57539f=!![]);}}else {const _0x482c25=_0x5bdde8['getSymbol']()?.['getDeclarations']()['find'](_0x4ca5bf=>_0x4ca5bf instanceof ClassDeclaration);_0x482c25&&isDecoratedWithResourceId(_0x482c25)&&(_0x57539f=!![]);}if(_0x57539f)_0xa052ed['isNavigationProperty']=!![];}__name(setIsNavigationProperty,'setIsNavigationProperty');function generateInternalPropertiesMap(_0x5ba2cc,_0x340a17,_0x184009,_0x599ac0){return _0x5ba2cc['reduce']((_0x230f7b,_0x48c987)=>{const _0x5ed01d=_0x48c987['getName'](),_0x2d2a66=Object['assign'](createInternalPropertyMeta(_0x48c987,_0x599ac0),_0x340a17[_0x5ed01d]);return _0x2d2a66['name']===wellKnownMetaProps['imageUrl']&&(_0x184009['useImage']=!![],_0x2d2a66['notDisplayable']=!![],_0x2d2a66['type']===propertyTypes['string']&&(_0x2d2a66['isUrl']=!![])),_0x230f7b[_0x5ed01d]=_0x2d2a66,_0x230f7b;},{});}__name(generateInternalPropertiesMap,'generateInternalPropertiesMap');

export { createInternalPropertyMeta, getSourceFiles, initInternalResourceMeta, initInternalResourceMetaHandlers, isDecoratedWithResourceId };
