'use strict';
import { validateResource, createValidationDetailResult } from '../validations/meta-validations.js';
import { crudResultTypes } from '../../../../fd-toolbox/types/crud/crud-result-type.js';
import { setInvalidCrudResult } from '../../../../fd-toolbox/types/crud/crud-results.js';
import { getResourceMeta } from '../services/metas.js';
import { singleWithWarning } from '../../../../fd-toolbox/server/collections/single-funcs.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { getRelatedOneToManyResources } from './related-resource-db.js';
import { getResource, getResourcesByFieldId, updateResource, deleteResources } from '../db/db.js';
import { getManyToManySecondResourceId } from '../relations/many-to-many-relations.js';
import { getInternalResourceMeta } from '../resources/internal-resource-meta.js';
import { metaValidationMessages } from '../constants/meta-validation-messages.js';
import { metaErrorMessages } from '../../constants/meta-error-messages-constants.js';
import { httpStatusCodes } from '../../../../fd-toolbox/constants/http-status-codes.js';
import { getUserId } from '../auth/users/current-user-id-provider.js';
import { prepareManyToManyResource } from './helpers/prepare-many-to-many-resource.js';
import { getOptionalPropertyValue } from '../../../../fd-toolbox/objects/property-value.js';
import { createResource } from '../resources/crud-resources.js';
import { relationTypes } from '../../../../fd-toolbox/enums/enums.js';

var __defProp=Object['defineProperty'],__name=(_0x229d72,_0x29fbad)=>__defProp(_0x229d72,'name',{'value':_0x29fbad,'configurable':!![]});async function syncInfosWithResourceMeta(_0x124e0e,_0x5267ab,_0x24d2e0){const _0x37e92a=metaValidationMessages['resourceNotFoundUserFriendly']['replace']('{0}',_0x24d2e0['id']),_0x1ac4bb=metaValidationMessages['resourceNotFound'],_0x1c7956=await validateResource(_0x24d2e0,_0x5267ab);let _0x4584b5;if(!_0x1c7956['isValid'])_0x4584b5={'type':crudResultTypes['notValid'],'validationResult':_0x1c7956};else for(let _0x5d059e of _0x124e0e){const _0x1b0abc=_0x5267ab['properties'][_0x5d059e['propertyName']],_0x220e76=await getResourceMeta(_0x1b0abc['relation']['relatedResourceName']),_0x5d4847=_0x220e76['properties'][_0x1b0abc['relation']['relatedPropertyName']],_0x521a26=await getResource({'resourceName':_0x5267ab['resourceName'],'id':_0x24d2e0['id']});if(!_0x521a26){const _0x4c68c8=createValidationDetailResult(_0x1ac4bb,_0x37e92a,_0x220e76['resourceName'],httpStatusCodes['notFound']);_0x4584b5=setInvalidCrudResult(_0x4c68c8);break;}await syncRelations(_0x1b0abc,_0x521a26,_0x5d059e,_0x5d4847,_0x220e76,_0x5267ab);}return _0x4584b5??{'type':crudResultTypes['success']};}__name(syncInfosWithResourceMeta,'syncInfosWithResourceMeta');async function syncManyToManyRelations(_0x47c225,_0x31017b,_0x36378f,_0x5d8f3c){if(!_0x31017b['relation']?.['manyToManyInfo'])throw createError(metaErrorMessages['propertyManyToManyMetaRelationNotFound']['replace']('{0}',_0x31017b['name']));const _0x15ce2b=await getInternalResourceMeta(_0x5d8f3c['resourceName']),_0x55c746=await getResourcesByFieldId(_0x5d8f3c['resourceName'],_0x31017b['relation']['manyToManyInfo']['manyToManyFirstPropertyName'],_0x47c225['id']),_0x41ce1f=_0x15ce2b['properties'],_0x1f6440=_0x41ce1f[_0x31017b['relation']['manyToManyInfo']['manyToManySecondPropertyName']],_0x35e4f4=[];for(const _0x3f5a19 of _0x55c746){const _0x35f33b=getManyToManySecondResourceId(_0x3f5a19,_0x1f6440);_0x35e4f4['push'](_0x35f33b);}const _0x7a2dfc=_0x36378f['filter'](_0x346705=>!_0x35e4f4['includes'](_0x346705));await createManyToManyRelations(_0x7a2dfc,_0x47c225,_0x5d8f3c,_0x31017b),await deleteManyToManyRelations(_0x55c746,_0x1f6440,_0x36378f,_0x5d8f3c);}__name(syncManyToManyRelations,'syncManyToManyRelations');async function getManyToManyResourcesToCreate(_0x228317,_0x2bdb2b,_0x5e0e4f,_0x59647b){const _0x1b603c=[],_0x221a56=await getUserId();for(const _0x2b5114 of _0x228317){const _0x35e625=await prepareManyToManyResource(_0x2bdb2b,_0x2b5114,_0x5e0e4f,_0x59647b,_0x221a56);_0x1b603c['push'](_0x35e625);}return _0x1b603c;}__name(getManyToManyResourcesToCreate,'getManyToManyResourcesToCreate');async function syncRelations(_0x4fb709,_0x178731,_0x1260ab,_0x5049ba,_0x43795e,_0x1a6e06){if(!_0x4fb709['relation'])throw createError(metaErrorMessages['propertyMetaRelationNotFound']['replace']('{0}',_0x4fb709['name']));if(_0x4fb709['relation']['type']===relationTypes['manyToMany'])await syncManyToManyRelations(_0x178731,_0x4fb709,_0x1260ab['relatedResourceIds'],_0x43795e);else {if(_0x4fb709['relation']['type']===relationTypes['oneToMany'])await syncOneToManyRelations(_0x178731,_0x4fb709,_0x1260ab['relatedResourceIds'],_0x43795e,_0x5049ba);else _0x4fb709['relation']['type']===relationTypes['manyToOne']&&await syncManyToOneRelations(_0x178731,_0x4fb709,_0x1260ab['relatedResourceIds'],_0x43795e,_0x1a6e06);}}__name(syncRelations,'syncRelations');async function syncManyToOneRelations(_0x1025e4,_0xcfd130,_0x4d38f2,_0x1c9127,_0x2e682b){if(_0x4d38f2['length']>0x12bf+0x17*0x77+-0x1d70){const _0x6bf821=await getResource({'resourceName':_0x1c9127['resourceName'],'id':singleWithWarning(_0x4d38f2)});_0x1025e4[_0xcfd130['name']]=_0x6bf821['id'];}else {if(_0xcfd130['required']){const _0x1687c6=metaErrorMessages['resourceRequiresRelation']['replace']('{0}',_0x2e682b['resourceName'])['replace']('{1}',_0xcfd130['relation']['relatedResourceName'])['replace']('{2}',_0xcfd130['name']);throw createError(_0x1687c6);}_0x1025e4[_0xcfd130['name']]=void(-0x1c9c+0x1*0x1253+0x1*0xa49);}await updateResource(_0x1025e4,_0x2e682b['resourceName']);}__name(syncManyToOneRelations,'syncManyToOneRelations');async function createManyToManyRelations(_0x3dc96d,_0x5289f4,_0x53c4e8,_0x5f9f19){const _0x4bb183=await getManyToManyResourcesToCreate(_0x3dc96d,_0x5289f4,_0x53c4e8,_0x5f9f19);for(const _0x77e8c7 of _0x4bb183){await createResource(_0x77e8c7,_0x53c4e8['resourceName']);}}__name(createManyToManyRelations,'createManyToManyRelations');async function deleteManyToManyRelations(_0x3b1425,_0x5a16da,_0x26ef5d,_0x524ddc){const _0x5d81eb=getManyToManyResourcesToDelete(_0x3b1425,_0x5a16da,_0x26ef5d),_0x5bf84a=[];for(const _0x361290 of _0x5d81eb){_0x5bf84a['push'](_0x361290['id']);}await deleteResources(_0x524ddc['resourceName'],_0x5bf84a);}__name(deleteManyToManyRelations,'deleteManyToManyRelations');function getManyToManyResourcesToDelete(_0x2bb2af,_0x5bff27,_0x1aecb9){const _0x199d9a=[];for(const _0x123a97 of _0x2bb2af){const _0x1d01ed=getManyToManySecondResourceId(_0x123a97,_0x5bff27);!_0x1aecb9['includes'](_0x1d01ed)&&_0x199d9a['push'](_0x123a97);}return _0x199d9a;}__name(getManyToManyResourcesToDelete,'getManyToManyResourcesToDelete');async function syncOneToManyRelations(_0x180c19,_0x5b76b1,_0x453efa,_0x4faa1b,_0x44befb){let _0x24c4a1=[];const _0x40b3e9=_0x5b76b1['relation']['relatedResourceName'];_0x24c4a1['push'](...await getResourcesWithClearedOneToManyRelation(_0x4faa1b['resourceName'],_0x44befb,_0x180c19['id'],_0x5b76b1,_0x453efa)),_0x24c4a1['push'](...await getResourcesWithAddedOneToManyRelation(_0x180c19['id'],_0x453efa,_0x4faa1b,_0x44befb));for(const _0x252321 of _0x24c4a1){await updateResource(_0x252321,_0x40b3e9);}}__name(syncOneToManyRelations,'syncOneToManyRelations');async function getResourcesWithClearedOneToManyRelation(_0x5c088a,_0x43174a,_0x2a4e30,_0x309120,_0x5cdf03){const _0x38b77a=await getRelatedOneToManyResources(_0x5c088a,_0x43174a,_0x2a4e30),_0x17ee65=[];for(const _0x4266cb of _0x38b77a){if(!_0x5cdf03['includes'](_0x4266cb['id'])){if(_0x309120['relation']?.['relatedResourceRequiresRelation'])throw createError(metaErrorMessages['resourceRequiresRelation']['replace']('{0}',_0x309120['relation']['relatedResourceName'])['replace']('{1}',_0x43174a['relation']['relatedResourceName'])['replace']('{2}',_0x309120['relation']['relatedPropertyName']));const _0x162bc3={..._0x4266cb,[_0x43174a['name']]:void(0x4*-0x8a8+-0xe15*0x1+0x30b5)};_0x17ee65['push'](_0x162bc3);}}return _0x17ee65;}__name(getResourcesWithClearedOneToManyRelation,'getResourcesWithClearedOneToManyRelation');async function getResourcesWithAddedOneToManyRelation(_0x444a72,_0x14d860,_0x2639d9,_0x3e6f4a){const _0x4ecc08=[];for(const _0x34f79e of _0x14d860){const _0x3d491f=await getResource({'resourceName':_0x2639d9['resourceName'],'id':_0x34f79e}),_0x52fda2=_0x3e6f4a['name'],_0x9d382b=_0x2639d9['properties'],_0x392473=getOptionalPropertyValue(_0x9d382b,_0x52fda2);if(_0x392473!==_0x444a72){const _0x14f036={..._0x3d491f,[_0x52fda2]:_0x444a72};_0x4ecc08['push'](_0x14f036);}}return _0x4ecc08;}__name(getResourcesWithAddedOneToManyRelation,'getResourcesWithAddedOneToManyRelation');

export { getManyToManyResourcesToCreate, syncInfosWithResourceMeta, syncManyToManyRelations };
