'use strict';
import { createRelationConstructor } from '../../core/relation.js';
import { isPropertyRequired } from '../props/required-properties.js';
import { getInternalResourceMeta } from '../resources/internal-resource-meta.js';
import { metas } from '../meta-core/constants/metas.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { isResourceIdProperty, getResourceNameFromPropertyWithId, isResourceProperty, getResourceNameOrDefault, getResourceNameFromListProperty, cutOffSuffix, getResourceDescriptionPropertyNames } from '../props/property-names.js';
import { isManyToManyResource, getManyToManyInfo } from './many-to-many-relations.js';
import { propertyTypes } from '../../properties/property-type.js';
import { getInternalPropertyMeta } from '../props/property-meta-config-provider.js';
import { pluralizeStrict } from '../../../../fd-toolbox/server/strings/strings.js';
import { relationTypes } from '../../../../fd-toolbox/enums/enums.js';
import { metaErrorMessages } from '../../constants/meta-error-messages-constants.js';
import { getObjectValues } from '../../../../fd-toolbox/lib/utils.js';

var __defProp=Object['defineProperty'],__name=(_0x216f92,_0x25e483)=>__defProp(_0x216f92,'name',{'value':_0x25e483,'configurable':!![]});function isListRelationProperty(_0x16e294){return _0x16e294['endsWith'](metas['listSuffix'])&&_0x16e294['length']!==metas['listSuffix']['length'];}__name(isListRelationProperty,'isListRelationProperty');async function setRelation(_0x29d344,_0x21fa57){let _0x55a828=relationTypes['oneToMany'],_0x1d0aa3='',_0x66ae29=![];const _0x1fb21f=await getInternalResourceMeta(_0x21fa57['resourceName']);if(_0x1fb21f['properties'][_0x29d344['name']]?.['notRelation'])return;if(await isResourceIdProperty(_0x29d344['name']))_0x55a828=relationTypes['manyToOne'],_0x1d0aa3=await getResourceNameFromPropertyWithId(_0x29d344['name']);else {if(await isResourceProperty(_0x29d344['name'],_0x29d344['type']))_0x55a828=relationTypes['manyToOne'],_0x66ae29=!![],_0x1d0aa3=await getResourceNameOrDefault(_0x29d344['name'])??'';else {if(isListRelationProperty(_0x29d344['name'])){_0x55a828=await isManyToManyProperty(_0x29d344['name'])?relationTypes['manyToMany']:relationTypes['oneToMany'],_0x66ae29=!![];const _0x12dd95=await getResourceNameFromListProperty(_0x29d344['name']);_0x1d0aa3=await getResourceNameOrDefault(_0x12dd95)??'';}}}_0x1d0aa3!==''&&await createRelationWithMeta(_0x29d344,_0x55a828,_0x21fa57,_0x1d0aa3,_0x66ae29);}__name(setRelation,'setRelation');async function isManyToManyProperty(_0xb1339b){let _0x15bec7=![];if(isListRelationProperty(_0xb1339b)){const _0x2f035b=await getResourceNameFromListProperty(_0xb1339b);_0x15bec7=await isManyToManyResource(_0x2f035b);}return _0x15bec7;}__name(isManyToManyProperty,'isManyToManyProperty');function isRelationPropertyAnObjectOrArray(_0x3046f4){if(!_0x3046f4['relation'])return ![];return _0x3046f4['type']===propertyTypes['object']||_0x3046f4['type']===propertyTypes['array'];}__name(isRelationPropertyAnObjectOrArray,'isRelationPropertyAnObjectOrArray');async function createRelationWithMeta(_0x306f58,_0x4cbc61,_0x3b69ec,_0x317e9e,_0x2c0d94){const _0x21bd82=await createBaseRelation(_0x317e9e,_0x2c0d94,_0x306f58);await fillRelationByMetaConfig(_0x306f58,_0x4cbc61,_0x3b69ec,_0x21bd82),await handleExpandDescription(_0x306f58,_0x21bd82,_0x3b69ec),_0x306f58['relation']=_0x21bd82;}__name(createRelationWithMeta,'createRelationWithMeta');async function createBaseRelation(_0x4c246a,_0x305edc,_0x276d5c){const _0x289f09=createRelationConstructor();_0x289f09['relatedResourceName']=_0x4c246a;const _0x34b188=await getInternalResourceMeta(_0x4c246a);return _0x289f09['pluralName']=_0x34b188['pluralName']??pluralizeStrict(_0x4c246a),_0x289f09['isExpandProperty']=_0x305edc,_0x305edc&&(_0x289f09['expandPropertyName']=_0x276d5c['name']),_0x289f09;}__name(createBaseRelation,'createBaseRelation');async function fillRelationByMetaConfig(_0x37a025,_0x25e181,_0x1def5e,_0xc21635){const _0x5e8648=await getInternalResourceMeta(_0xc21635['relatedResourceName']);await fillRelationByRelationMetaConfig(_0x37a025,_0x25e181,_0x1def5e,_0xc21635,_0x5e8648);}__name(fillRelationByMetaConfig,'fillRelationByMetaConfig');async function handleExpandDescription(_0x459ed1,_0xd324d6,_0x1d8199){const _0x10b60e=await getInternalResourceMeta(_0x1d8199['resourceName']),_0x320662=await getInternalPropertyMeta(_0x1d8199['resourceName'],_0x459ed1['name']),_0x180ad6=!!_0x320662['expandDescription'],_0x2dd274=_0x10b60e['properties'][_0x459ed1['name']]?.['expandDescription'];(_0x180ad6||_0x2dd274)&&(_0xd324d6['expandForDescription']=!![]);if(_0xd324d6['expandForDescription']&&!_0xd324d6['isExpandProperty']){_0xd324d6['expandPropertyName']=cutOffSuffix(_0x459ed1['name'],metas['idSuffix']);const _0x543976=await getInternalResourceMeta(_0x1d8199['resourceName']),_0x120df1=_0x543976['properties'];if(!Object['keys'](_0x120df1)['some'](_0x1debfa=>_0x1debfa===_0xd324d6['expandPropertyName']))throw createError(metaErrorMessages['expandPropertyDoesNotExist']['replace']('{0}',_0xd324d6['expandPropertyName'])['replace']('{1}',_0x1d8199['resourceName']));}if(_0xd324d6['expandForDescription']){const _0x127fcd=await getInternalResourceMeta(_0xd324d6['relatedResourceName']);_0xd324d6['descriptionPropertyName']=_0x127fcd['description']?.['resourceDescriptionProperty']??'',_0xd324d6['descriptionPropertyNames']=getResourceDescriptionPropertyNames(_0x127fcd);}}__name(handleExpandDescription,'handleExpandDescription');async function fillOneToManyOrManyToManyRelationCommonProperties(_0x3c866e,_0x3a3ced,_0x2478fe){let _0x4872cf;for(const _0x333610 of getObjectValues(_0x2478fe['properties'])){if(await isResourceIdProperty(_0x333610['name'],_0x3a3ced)){_0x4872cf=_0x333610;break;}}if(!_0x4872cf)throw createError('Related\x20property\x20not\x20found');_0x3c866e['relatedPropertyName']=_0x4872cf['name'],_0x3c866e['relatedResourceRequiresRelation']=isPropertyRequired(_0x4872cf);}__name(fillOneToManyOrManyToManyRelationCommonProperties,'fillOneToManyOrManyToManyRelationCommonProperties');async function fillRelationByRelationMetaConfig(_0x19895d,_0x190513,_0xbd489b,_0x578278,_0x4f846f){if(_0x190513===relationTypes['oneToMany'])await fillOneToManyOrManyToManyRelationCommonProperties(_0x578278,_0xbd489b['resourceName'],_0x4f846f);else {if(_0x190513===relationTypes['manyToMany'])await fillManyToManyRelation(_0x578278,_0xbd489b['resourceName'],_0x4f846f);else _0x190513===relationTypes['manyToOne']&&(_0x578278['type']=_0x190513,_0x578278['relatedPropertyName']=metas['idConstant']);}if(_0x190513===relationTypes['manyToOne']){const _0x268236=await getInternalResourceMeta(_0xbd489b['resourceName']),_0x3602a7=_0x268236['properties'][_0x19895d['name']];!_0x3602a7?.['notDisplayable']&&(_0x578278['expandForDescription']=await isManyToManyResource(_0xbd489b['resourceName']));}_0x578278['type']=_0x190513;}__name(fillRelationByRelationMetaConfig,'fillRelationByRelationMetaConfig');async function fillManyToManyRelation(_0x552043,_0x542a4d,_0x505cf5){await fillOneToManyOrManyToManyRelationCommonProperties(_0x552043,_0x542a4d,_0x505cf5),_0x552043['manyToManyInfo']=await getManyToManyInfo(_0x505cf5,_0x552043['relatedPropertyName'],_0x542a4d);}__name(fillManyToManyRelation,'fillManyToManyRelation');

export { isListRelationProperty, isManyToManyProperty, isRelationPropertyAnObjectOrArray, setRelation };
