'use strict';
import { getResourceMeta } from '../../services/metas.js';
import { propertyTypes } from '../../../properties/property-type.js';
import { isNullOrUndefined } from '../../../../../fd-toolbox/functions/value-checking-functions.js';
import { createError } from '../../../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../../constants/meta-error-messages-constants.js';
import { getSingleOrEmpty } from '../../../../../fd-toolbox/server/collections/single-funcs.js';
import { metas } from '../../meta-core/constants/metas.js';
import { isString } from '../../../../../fd-toolbox/types/ensure-type.js';
import { isWithId } from '../../../../../fd-toolbox/lib/utils.js';
import { isPropertyExpandable } from '../../services/property-metas.js';
import { logWarning } from '../../../../../fd-toolbox/server/logging/logger.js';
import { warningMessages } from '../../constants/warning-messages.js';
import { parseSelects, parseSorts, parseExpand, parseODataFilter } from './odata-parsers.js';
import { oDataQueryParams } from '../../../../../fd-toolbox/constants/odata-query-params.js';
import { relatedFilterRegex } from '../../../../../fd-toolbox/constants/odata-constants.js';
import { oDataFilterTypes } from '../../../../../fd-toolbox/odata/odata-enums.js';

var __defProp=Object['defineProperty'],__name=(_0x275b8c,_0x3d661b)=>__defProp(_0x275b8c,'name',{'value':_0x3d661b,'configurable':!![]});function getODataConfig(_0x2c8f5b,_0x188f7b,_0x548a29,_0x5d652b,_0x24cd95,_0x2b4535){if(!hasAnyParams(_0x2c8f5b,_0x188f7b,_0x548a29,_0x5d652b,_0x24cd95,_0x2b4535))return {};return {'filters':_0x2c8f5b?parseODataFilter(_0x2c8f5b):[],'expands':_0x188f7b?parseExpand(_0x188f7b):[],'orderBys':_0x548a29?parseSorts(_0x548a29):[],'selects':_0x2b4535?parseSelects(_0x2b4535):[],'skip':_0x5d652b,'top':_0x24cd95};}__name(getODataConfig,'getODataConfig');async function filterODataSelectOrExpandTree(_0x22081a,_0x436778,_0x5366c4){const _0x5dab6e=await getResourceMeta(_0x22081a),_0x269289={'resourceMeta':_0x5dab6e,'children':_0x5366c4?getNotExpandblePropertyChidren(_0x5dab6e):{}};if(_0x436778['children'])for(const [_0x567668,_0x58013e]of Object['entries'](_0x436778['children'])){await processProperty(_0x567668,_0x5dab6e,_0x58013e,_0x269289,_0x5366c4);}return _0x269289;}__name(filterODataSelectOrExpandTree,'filterODataSelectOrExpandTree');function groupODataResults(_0x5acf5e,_0x2e083c,_0x56ba61){const _0x5b6ef4=new Map();for(const _0x1b0c36 of _0x5acf5e){const _0x5f57d7=_0x1b0c36[metas['idConstant']];isString(_0x5f57d7)&&(!_0x5b6ef4['has'](_0x5f57d7)&&_0x5b6ef4['set'](_0x5f57d7,[]),_0x5b6ef4['get'](_0x5f57d7)['push'](_0x1b0c36));}const _0x22b772=[];for(const _0x3c6da6 of _0x5b6ef4['values']()){const _0x270c31=processGroupElements(_0x3c6da6,_0x2e083c,_0x56ba61);_0x22b772['push'](_0x270c31);}return _0x22b772;}__name(groupODataResults,'groupODataResults');function getEmptyODataConfig(){const _0xe03cb3={'filters':[],'expands':[],'orderBys':[],'selects':[]};return _0xe03cb3;}__name(getEmptyODataConfig,'getEmptyODataConfig');async function getODataConfigFromRequest(_0x1ef46c){const _0x4c1f15=new URL(_0x1ef46c['url']),_0x52c9db=_0x4c1f15['pathname']['split']('/'),_0x47301d=_0x4c1f15['searchParams']['get'](oDataQueryParams['filter']),_0x43777a=_0x4c1f15['searchParams']['get'](oDataQueryParams['expand']),_0x5d244c=_0x4c1f15['searchParams']['get'](oDataQueryParams['orderBys'])??await checkForDefaultSorting(_0x52c9db[_0x52c9db['length']-(-0x299+-0x4b5+0x74f)],_0x4c1f15['searchParams']),_0x322fc8=_0x4c1f15['searchParams']['get'](oDataQueryParams['select']),_0x3104f6=_0x4c1f15['searchParams']['get'](oDataQueryParams['skip']),_0x1acfd7=_0x4c1f15['searchParams']['get'](oDataQueryParams['top']),_0x213f71=_0x3104f6?Number(_0x3104f6):void(0x97*0x35+0x169*0xb+-0x2ec6*0x1),_0x588476=_0x1acfd7?Number(_0x1acfd7):void(-0xfe6+-0x10fc*0x1+-0x16e*-0x17),_0x59ce9a=getODataConfig(_0x47301d,_0x43777a,_0x5d244c,_0x213f71,_0x588476,_0x322fc8);return _0x59ce9a;}__name(getODataConfigFromRequest,'getODataConfigFromRequest');function getODataFilterParams(_0x893ef){if(!_0x893ef['filters']||_0x893ef['filters']['length']===-0x2*-0x4cf+-0x15cf+0xc31*0x1)return [];const _0x20da15={'name':oDataQueryParams['filter'],'value':buildFilterString(_0x893ef['filters'])};return [_0x20da15];}__name(getODataFilterParams,'getODataFilterParams');function getBaseAndRelatedFilters(_0x2c8068){const _0x2c29c5=_0x2c8068['filters'],{defaultFilters:_0x4cca14,relatedFilters:_0x500bab}=groupQueryFilters(_0x2c29c5);return {'defaultFilters':_0x4cca14,'relatedFilters':_0x500bab};}__name(getBaseAndRelatedFilters,'getBaseAndRelatedFilters');function buildFilterString(_0x282ca5){return _0x282ca5['map'](_0x139e39=>{const _0x275887=_0x139e39['leftSide'],_0x2be3e1=_0x139e39['rightSide'];return _0x139e39['type']===oDataFilterTypes['guid']?_0x275887+'\x20eq\x20guid\x27'+_0x2be3e1+'\x27':_0x275887+'\x20eq\x20\x27'+_0x2be3e1+'\x27';})['join']('\x20'+oDataQueryParams['and']+'\x20');}__name(buildFilterString,'buildFilterString');function groupQueryFilters(_0x3a8333){const _0x5ee2b9=[],_0x55acb2=[];for(const _0xca47b of _0x3a8333){relatedFilterRegex['test'](_0xca47b['leftSide'])?_0x55acb2['push'](_0xca47b):_0x5ee2b9['push'](_0xca47b);}return {'defaultFilters':_0x5ee2b9,'relatedFilters':_0x55acb2};}__name(groupQueryFilters,'groupQueryFilters');async function checkForDefaultSorting(_0x1a489b,_0x331d1e){const _0x36477f=await getResourceMeta(_0x1a489b),_0x180886=_0x331d1e['get'](oDataQueryParams['select']);let _0x35228a;return _0x36477f?.['orderBys']&&_0x36477f['orderBys']['forEach'](_0x5e2f67=>{if(_0x5e2f67['propertyName']===_0x180886)_0x35228a=_0x5e2f67['propertyName'];}),_0x35228a;}__name(checkForDefaultSorting,'checkForDefaultSorting');function hasAnyParams(..._0x25e678){return _0x25e678['some'](_0x57c50a=>_0x57c50a!==void(-0x1464+-0x201a+0x1a3f*0x2));}__name(hasAnyParams,'hasAnyParams');function getNotExpandblePropertyChidren(_0x21d2d4){const _0x378a9e={};return Object['keys'](_0x21d2d4['properties'])['forEach'](_0x1f4429=>{const _0x1d5876=_0x21d2d4['properties'][_0x1f4429];!isPropertyExpandable(_0x1d5876)&&!_0x1d5876['isDynamic']&&(_0x378a9e[_0x1f4429]={});}),_0x378a9e;}__name(getNotExpandblePropertyChidren,'getNotExpandblePropertyChidren');async function processProperty(_0x5c5170,_0x1cc46e,_0x78e6b7,_0x32e34d,_0x20311b){const _0x3e3fdb=_0x1cc46e['properties'][_0x5c5170];if(_0x3e3fdb){if(isPropertyExpandable(_0x3e3fdb)){const _0x493d5b=await filterODataSelectOrExpandTree(_0x3e3fdb['relation']['relatedResourceName'],_0x78e6b7,_0x20311b);_0x493d5b&&(_0x32e34d['children'][_0x5c5170]=_0x493d5b);}else _0x32e34d['children'][_0x5c5170]={};}else logWarning(warningMessages['odataSelectNoProperty']['replace']('{0}',_0x1cc46e['resourceName'])['replace']('{1}',_0x5c5170));}__name(processProperty,'processProperty');function processElement(_0x94dc66,_0x5b8596,_0x577996){let _0x4f559a;const _0x20140a=processGroupElements([_0x94dc66],_0x5b8596,_0x577996);return _0x4f559a=_0x20140a,_0x4f559a;}__name(processElement,'processElement');function processGroupElements(_0x37629b,_0x3b2465,_0x5b581f){const _0x79f88f=_0x3b2465['children']??{},_0x3aa12c={};for(const [_0x26b9d0,_0x21e9b2]of Object['entries'](_0x79f88f)){const _0x12bd9c=_0x5b581f['properties'][_0x26b9d0],_0x2a51f3=getGroupedPropertyValuesByName(_0x37629b,_0x26b9d0);if(Object['keys'](_0x21e9b2)['length']>0x163e+-0x8fd*0x3+-0x3*-0x193){const _0x516455=getUniqueObjectPropertyValues(_0x2a51f3);switch(_0x12bd9c['type']){case propertyTypes['array']:{_0x3aa12c[_0x26b9d0]=processArray(_0x516455,_0x21e9b2,_0x21e9b2['resourceMeta']);break;}case propertyTypes['object']:{_0x3aa12c[_0x26b9d0]=processObject(_0x516455,_0x21e9b2,_0x21e9b2['resourceMeta']);break;}default:{throw createError(metaErrorMessages['typeMismatch']);}}}else _0x3aa12c[_0x26b9d0]=getSingleOrEmpty(getUniqueSimplePropertyValues(_0x2a51f3));}return _0x3aa12c;}__name(processGroupElements,'processGroupElements');function processArray(_0x548631,_0x103bc6,_0x5eb58b){return _0x548631['map'](_0x1e48be=>{const _0x3e423a=processElement(_0x1e48be,_0x103bc6,_0x5eb58b);return _0x3e423a;});}__name(processArray,'processArray');function processObject(_0x4a2e4b,_0x192f32,_0x48c6ab){let _0x4818af;const _0x47074b=getSingleOrEmpty(_0x4a2e4b);return _0x47074b&&(_0x4818af=processElement(_0x47074b,_0x192f32,_0x48c6ab)),_0x4818af;}__name(processObject,'processObject');function getGroupedPropertyValuesByName(_0xab4322,_0x2797d1){const _0x412301=[];for(const _0x1a36cc of _0xab4322){!isNullOrUndefined(_0x1a36cc[_0x2797d1])&&_0x412301['push'](_0x1a36cc[_0x2797d1]);}return _0x412301;}__name(getGroupedPropertyValuesByName,'getGroupedPropertyValuesByName');function getUniqueObjectPropertyValues(_0x501fb4){const _0x2fbfee={};for(const _0x3b01e6 of _0x501fb4){if(isWithId(_0x3b01e6))_0x2fbfee[_0x3b01e6['id']]=_0x3b01e6;else throw createError(metaErrorMessages['valueIsNotObjectWithId']);}return Object['values'](_0x2fbfee);}__name(getUniqueObjectPropertyValues,'getUniqueObjectPropertyValues');function getUniqueSimplePropertyValues(_0x542f1c){const _0xbefc0f=[];for(const _0x850413 of _0x542f1c){const _0x349b35=_0xbefc0f['some'](_0x3a6d01=>{if(_0x850413 instanceof Date&&_0x3a6d01 instanceof Date)return _0x3a6d01['getTime']()===_0x850413['getTime']();return _0x3a6d01===_0x850413;});!_0x349b35&&_0xbefc0f['push'](_0x850413);}return _0xbefc0f;}__name(getUniqueSimplePropertyValues,'getUniqueSimplePropertyValues');

export { filterODataSelectOrExpandTree, getBaseAndRelatedFilters, getEmptyODataConfig, getODataConfig, getODataConfigFromRequest, getODataFilterParams, groupODataResults };
