'use strict';
import { withIdProps } from '../../../../fd-toolbox-core/types/resource-with-id.js';
import { emptyUuid } from '../../../../fd-toolbox/constants/constants.js';
import { oDataFilterTypes } from '../../../../fd-toolbox/odata/odata-enums.js';
import { isManyToManyBrowserField, getViewMultiListFiltersForManyToManyBrowserField, isOneToManyBrowserField, isManyToOneBrowserField } from '../../services/property-fields.js';
import { getObjectValues } from '../../../../fd-toolbox/lib/utils.js';
import { getEmptyUuid } from '../../../../fd-toolbox/uuids.js';
import { wellKnownMetaProps } from '../../../../fd-toolbox-core/constants/meta-constants.js';
import { fetchUiResourceMeta } from '../../ui-meta-providers.js';
import { isManyToOneRelation } from '../../relations/meta-relations.js';

var __defProp=Object['defineProperty'],__name=(_0x4bba34,_0x1b679c)=>__defProp(_0x4bba34,'name',{'value':_0x1b679c,'configurable':!![]});async function getRelatedResourceMultiListItems(_0x581ec5,_0x1da7a1,_0x3938cd,_0x588886){const _0x3da82d=_0x581ec5['resourceMeta'];if(!_0x3da82d['properties'])return [];let _0x5a15eb={};for(const _0x3aabbd of getObjectValues(_0x3da82d['properties'])){if(shouldRelationBeDisplayed(_0x3aabbd,_0x5a15eb)&&_0x3aabbd['name']!==wellKnownMetaProps['workspaceId']){const _0x34fc96=await generateMultiListFilters(_0x3aabbd,_0x1da7a1,_0x588886);_0x34fc96&&_0x3aabbd['relation']&&await addResourceToMap(_0x5a15eb,_0x3aabbd,_0x34fc96,_0x3938cd);}}const _0x10902c=filterMultiListItems(getObjectValues(_0x5a15eb),_0x581ec5);return sortMultiListItems(_0x10902c,_0x581ec5),_0x10902c;}__name(getRelatedResourceMultiListItems,'getRelatedResourceMultiListItems');function shouldRelationBeDisplayed(_0x493f43,_0x5065d6){return (_0x493f43['isDisplayable']||_0x493f43['isViewVisible'])&&_0x493f43['relation']&&!_0x5065d6[_0x493f43['relation']?.['relatedResourceName']];}__name(shouldRelationBeDisplayed,'shouldRelationBeDisplayed');async function generateMultiListFilters(_0x457dc8,_0x25607f,_0x444a7b){if(isManyToManyBrowserField(_0x457dc8))return getViewMultiListFiltersForManyToManyBrowserField(_0x457dc8,_0x25607f);else {if(isOneToManyBrowserField(_0x457dc8))return getMultiListFiltersForMultiBrowserField(_0x457dc8,_0x25607f,_0x444a7b);else {if(isManyToOneBrowserField(_0x457dc8))return getMultiListFiltersForManyToOneBrowserField(_0x457dc8,_0x444a7b);}}return void(0x6e2*0x2+-0x1895+0xad1);}__name(generateMultiListFilters,'generateMultiListFilters');async function getMultiListFiltersForMultiBrowserField(_0x340bb8,_0x18423e,_0x4ff61c){const _0x32ecc0=await fetchUiResourceMeta(_0x340bb8['relation']?.['relatedResourceName']??'');return _0x340bb8['relation']?.['relatedPropertyName']&&_0x32ecc0['properties'][_0x340bb8['relation']['relatedPropertyName']]?[{'leftSide':_0x340bb8['relation']['relatedPropertyName'],'rightSide':_0x18423e,'type':oDataFilterTypes['guid']}]:[{'leftSide':withIdProps['id'],'rightSide':_0x4ff61c?_0x4ff61c[_0x340bb8['name']]?.['toString']()??emptyUuid:emptyUuid,'type':oDataFilterTypes['guid']}];}__name(getMultiListFiltersForMultiBrowserField,'getMultiListFiltersForMultiBrowserField');async function getMultiListFiltersForManyToOneBrowserField(_0x46f951,_0x5e9410){let _0x58f430;return _0x5e9410&&(_0x58f430=[{'leftSide':withIdProps['id'],'rightSide':_0x5e9410[_0x46f951['name']],'type':oDataFilterTypes['guid']}]),_0x58f430;}__name(getMultiListFiltersForManyToOneBrowserField,'getMultiListFiltersForManyToOneBrowserField');async function addResourceToMap(_0xd70167,_0x20fdc0,_0x40bf7e,_0x2658df){const _0xcdeba9=Object['fromEntries'](_0x40bf7e['map']((_0x551d96,_0x33bdab)=>[_0x33bdab,_0x551d96]));_0xd70167[_0x20fdc0['displayName']]={'relatedResourceElementId':'multiListResource'+_0x20fdc0['relation']['relatedResourceName'],'resourceId':getEmptyUuid(),'displayName':_0x20fdc0['displayName'],'resourceName':_0x20fdc0['relation']['relatedResourceName'],'filters':_0xcdeba9,'propertyName':_0x20fdc0['name'],'isRequiredRelation':_0x20fdc0['required']||_0x20fdc0['relation']['relatedResourceRequiresRelation'],'type':_0x2658df},isManyToOneRelation(_0x20fdc0)&&(_0xd70167[_0x20fdc0['displayName']]['parentResourcePropertyMeta']=_0x20fdc0);}__name(addResourceToMap,'addResourceToMap');function sortMultiListItems(_0x156b85,_0x51604e){_0x156b85['sort']((_0x43a5b5,_0x1d4673)=>{const _0x5cd85c=_0x51604e['properties'][_0x43a5b5['propertyName']]['uiRelation']['uiRelationPriority']??-0x155d+0xfff*-0x1+0x255c,_0x5e1e22=_0x51604e['properties'][_0x1d4673['propertyName']]['uiRelation']['uiRelationPriority']??-0x1b97*-0x1+0x6*-0x337+0x19*-0x55;return _0x5e1e22-_0x5cd85c;});}__name(sortMultiListItems,'sortMultiListItems');function filterMultiListItems(_0x49bda3,_0x4fee46){return _0x49bda3['filter'](_0x7bf184=>(_0x4fee46['properties'][_0x7bf184['propertyName']]['uiRelation']['uiRelationPriority']??0x1e74+-0x26b6+0x7*0x12e)<0x5*-0x7b1+-0x1877+0x42d4);}__name(filterMultiListItems,'filterMultiListItems');

export { getRelatedResourceMultiListItems };
