'use strict';
import { asc, desc, aliasedTable, inArray, eq } from 'drizzle-orm';
import { getQuery, getTableByResourceName, getAliasedTableByResourceName } from '../../web/drizzle.js';
import { getColumnsFromTable, getQueryConfig, compareTableByName, getTableColumn } from '../../../../db/drizzle-queries.js';
import { propertyTypes } from '../../../properties/property-type.js';
import { dbOrderBy, createDynamicSelect, dbWhere, dbOffset, dbLimit, dbLeftJoin } from '../../../../db/db-connection.js';
import { cutOffSuffix } from '../../props/property-names.js';
import { isPropertyExpandable } from '../../services/property-metas.js';
import { metas } from '../../meta-core/constants/metas.js';
import { getInternalResourceMeta } from '../../resources/internal-resource-meta.js';
import { paginationTypes } from '../../../../../fd-toolbox/server/api-enums/pagination-type.js';
import { createError } from '../../../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../../constants/meta-error-messages-constants.js';
import { withIdProps } from '../../../../../fd-toolbox-core/types/resource-with-id.js';
import { oDataOrderByDirections } from '../../../../../fd-toolbox/enums/enums.js';
import { convertFromPascalToCamelCase } from '../../../../../fd-toolbox/strings/casing-converter.js';

var __defProp=Object['defineProperty'],__name=(_0x439f23,_0x1f8e87)=>__defProp(_0x439f23,'name',{'value':_0x1f8e87,'configurable':!![]});async function select(_0x20cde7,_0x3cd5a6,_0x18b858){let _0x3e82f1={};const _0xb0ac74=_0x3cd5a6['children']??{};!Object['keys'](_0xb0ac74)['includes'](withIdProps['id'])&&(_0x3e82f1[withIdProps['id']]=_0x20cde7['id']);for(const [_0x3d66d0,_0x2dcf7b]of Object['entries'](_0xb0ac74)){const _0x489cd3=await buildSelectedProperty(_0x20cde7,_0x18b858,_0x3d66d0,_0x2dcf7b);_0x3e82f1[_0x3d66d0]=_0x489cd3;}let {query:_0xb72e6}=await getQuery(_0x20cde7,_0x3e82f1);return await joinRelatedTables(_0xb72e6,_0x20cde7,_0x3cd5a6,_0x18b858),{'query':_0xb72e6};}__name(select,'select');async function expand(_0x440fc9,_0x3b1d3b,_0x3262f2){let _0x5ce3f9={...getColumnsFromTable(_0x440fc9)};const _0xfa9ac2=_0x3b1d3b['children']??{};for(const [_0x1fbdd2,_0xd1b8f7]of Object['entries'](_0xfa9ac2)){const _0x2286a6=await buildExpandedProperty(_0x1fbdd2,_0x3262f2,_0xd1b8f7);_0x5ce3f9[_0x1fbdd2]=_0x2286a6;}let {query:_0xfd360}=await getQuery(_0x440fc9,_0x5ce3f9);return await joinRelatedTables(_0xfd360,_0x440fc9,_0x3b1d3b,_0x3262f2),{'query':_0xfd360};}__name(expand,'expand');async function offset(_0x5151a6,_0x3a5cec,_0x2b9bf1,_0xc1c645,_0x169fcd){return applyPagination(_0x5151a6,_0x3a5cec,_0x2b9bf1,_0xc1c645,_0x169fcd,paginationTypes['offset']);}__name(offset,'offset');async function limit(_0x3834e0,_0x3a28eb,_0x49e64b,_0x39e0fa,_0x47f10e){return applyPagination(_0x3834e0,_0x3a28eb,_0x49e64b,_0x39e0fa,_0x47f10e,paginationTypes['limit']);}__name(limit,'limit');function sort(_0x45b1a9,_0x389702){const _0x5503d2=dbOrderBy(_0x45b1a9,..._0x389702['map'](_0x214a06=>_0x214a06['ascending']?asc(_0x214a06['column']):desc(_0x214a06['column'])));return {'query':_0x5503d2};}__name(sort,'sort');async function getSortColumns(_0x3ff321,_0x334cf6,_0xb675a4,_0x3989f9){const _0x51d1f4=getQueryConfig(_0x3ff321)['joins']?.['map'](_0x2ae5be=>_0x2ae5be['table']),_0x4cc0fc=[],_0x2678a0=_0x3989f9??[];for(const _0x14e0cd of _0x2678a0['toSorted']((_0x2f97a8,_0x56b247)=>_0x2f97a8['priority']-_0x56b247['priority'])){let _0x2b827b=_0x14e0cd['propertyName'],_0x370314=_0x334cf6,_0x1a7901=![];const _0x3877be=_0xb675a4['properties'][_0x2b827b];if(_0x3877be?.['relation']&&_0x3877be['relation']['relatedPropertyName']===withIdProps['id']){const _0x5d3bf9=await getRelatedResourceName(_0xb675a4['resourceName'],_0x2b827b,_0xb675a4),_0x231fc8=await getTableByResourceName(_0x5d3bf9);if(_0x51d1f4?.['find'](_0x3abb27=>compareTableByName(_0x3abb27,_0x231fc8))){_0x370314=aliasedTable(_0x231fc8,convertFromPascalToCamelCase(_0x5d3bf9));const _0x39bdf4=await getInternalResourceMeta(_0x5d3bf9);_0x2b827b=_0x39bdf4['description']['resourceDescriptionProperty']??'',_0x1a7901=!![];}}if(!(_0x2b827b in _0x370314))throw createError(metaErrorMessages['resourceCannotBeOrderedByNavigationProperty']['replace']('{0}',_0xb675a4['resourceName'])['replace']('{1}',_0x2b827b));_0x4cc0fc['push']({'joined':_0x1a7901,'ascending':_0x14e0cd['direction']===oDataOrderByDirections['asc'],'column':getTableColumn(_0x370314,_0x2b827b),'basePropertyName':_0x14e0cd['propertyName']});}return _0x4cc0fc;}__name(getSortColumns,'getSortColumns');async function buildSelectedProperty(_0x9b7692,_0x437dc8,_0x5a0c5d,_0x31bdf8={}){const _0xb80e77=_0x437dc8['properties'][_0x5a0c5d];let _0x1a4c4e;if(!isPropertyExpandable(_0xb80e77))_0x1a4c4e=getColumnsFromTable(_0x9b7692)[_0x5a0c5d];else {const _0x1081e5=await getRelatedResourceName(_0x437dc8['resourceName'],_0x5a0c5d,_0x437dc8),_0x1f9833=await getAliasedTableByResourceName(_0x1081e5,_0x5a0c5d),_0xa72659={[withIdProps['id']]:getColumnsFromTable(_0x1f9833)[withIdProps['id']]};if(_0x31bdf8['children']&&Object['keys'](_0x31bdf8['children'])['length']&&_0x31bdf8['resourceMeta']){for(const [_0x534a08,_0x23cc34]of Object['entries'](_0x31bdf8['children'])){const _0xf16922=await buildSelectedProperty(_0x1f9833,_0x31bdf8['resourceMeta'],_0x534a08,_0x23cc34);_0xa72659[_0x534a08]=_0xf16922;}_0x1a4c4e=_0xa72659;}else _0x1a4c4e=getColumnsFromTable(_0x1f9833);}return _0x1a4c4e;}__name(buildSelectedProperty,'buildSelectedProperty');async function getRelatedResourceName(_0x1b02e3,_0x1b0a2f,_0x77f344){const _0x4a43e7=_0x77f344;let _0x1de2bc;if(_0x4a43e7){const _0x189624=_0x4a43e7['properties'][_0x1b0a2f];_0x189624?.['relation']?.['relatedResourceName']&&(_0x1de2bc=_0x189624['relation']['relatedResourceName']);}if(!_0x1de2bc){const _0x59b8f6=await getInternalResourceMeta(_0x1b02e3),_0x3730d3=_0x59b8f6['properties'][_0x1b0a2f];_0x3730d3?.['declaredWithType']&&(_0x1de2bc=_0x3730d3['declaredWithType']);}if(!_0x1de2bc)throw createError('Could\x20not\x20determine\x20related\x20resource\x20for\x20property\x20\x27'+_0x1b0a2f+'\x27\x20on\x20resource\x20\x27'+_0x1b02e3+'\x27.\x20It\x20was\x20not\x20found\x20in\x20ResourceMeta\x20relations\x20nor\x20in\x20InternalResourceMeta\x20declaredWithType.');return _0x1de2bc;}__name(getRelatedResourceName,'getRelatedResourceName');async function buildExpandedProperty(_0x4c06e8,_0x57a7f9,_0x3bc159={}){const _0x584a1f=await getRelatedResourceName(_0x57a7f9['resourceName'],_0x4c06e8,_0x57a7f9),_0x10670e=await getAliasedTableByResourceName(_0x584a1f,_0x4c06e8),_0x507d46=getColumnsFromTable(_0x10670e);if(_0x3bc159['children']&&_0x3bc159['resourceMeta'])for(const [_0x552646,_0x542876]of Object['entries'](_0x3bc159['children'])){const _0x3e1898=await buildExpandedProperty(_0x552646,_0x3bc159['resourceMeta'],_0x542876);_0x507d46[_0x552646]=_0x3e1898;}return _0x507d46;}__name(buildExpandedProperty,'buildExpandedProperty');async function applyPagination(_0x39aacc,_0x304842,_0x3c988f,_0xec8286,_0x29452e,_0x996b81){let _0x3724d3=createDynamicSelect(_0x304842,{'id':_0x304842['id']});if(_0xec8286['length']>-0xc1c+0x8*-0x20b+0x1c74){for(const _0x563827 of _0xec8286){let _0x128112=_0x563827['basePropertyName'];if(_0x128112['endsWith'](metas['idSuffix'])&&_0x128112!==withIdProps['id']){_0x128112=cutOffSuffix(_0x128112,metas['idSuffix']);const _0x46ba2b=_0x29452e['properties'][_0x128112];isPropertyExpandable(_0x46ba2b)&&await joinOneSideRelation(_0x3724d3,_0x304842,_0x29452e,_0x128112);}}sort(_0x3724d3,_0xec8286);}const _0x53ef9d=getQueryConfig(_0x39aacc)['where'];_0x53ef9d&&dbWhere(_0x3724d3,_0x53ef9d);_0x996b81===paginationTypes['offset']?dbOffset(_0x3724d3,_0x3c988f):dbLimit(_0x3724d3,_0x3c988f);const _0x40bb9b=inArray(_0x304842['id'],_0x3724d3),_0x4d32ff=dbWhere(_0x39aacc,_0x40bb9b);return {'query':_0x4d32ff};}__name(applyPagination,'applyPagination');async function joinRelatedTables(_0x228865,_0x56317a,_0x586b24,_0xbcd217){await Promise['all'](Object['entries'](_0x586b24['children']??{})['map'](([_0x21e37b,_0x5a2a75])=>joinRelatedTable(_0x228865,_0x56317a,_0xbcd217,_0x21e37b,_0x5a2a75)));}__name(joinRelatedTables,'joinRelatedTables');async function joinRelatedTable(_0xa5cc81,_0xf21f9f,_0x550132,_0xc48769,_0x49355){const _0x3ea654=_0x550132['properties'][_0xc48769];if(!isPropertyExpandable(_0x3ea654))return;let _0x5a3a57;if(_0x3ea654['type']===propertyTypes['array']){const _0x1958e1=await getRelatedResourceName(_0x550132['resourceName'],_0xc48769,_0x550132);_0x5a3a57=await joinManySideRelation(_0xa5cc81,_0xf21f9f,_0xc48769,_0x1958e1,_0x3ea654['relation']['relatedPropertyName']);}else _0x5a3a57=await joinOneSideRelation(_0xa5cc81,_0xf21f9f,_0x550132,_0xc48769);await joinChildRelatedTables(_0xa5cc81,_0x5a3a57,_0x49355);}__name(joinRelatedTable,'joinRelatedTable');async function joinChildRelatedTables(_0x12850e,_0x3a1a42,_0x2cb41e){if(!_0x2cb41e['resourceMeta']||!_0x2cb41e['children'])return;for(const [_0x4984b3,_0x5201c0]of Object['entries'](_0x2cb41e['children'])){await joinRelatedTable(_0x12850e,_0x3a1a42,_0x2cb41e['resourceMeta'],_0x4984b3,_0x5201c0);}}__name(joinChildRelatedTables,'joinChildRelatedTables');async function joinManySideRelation(_0x4a9fb7,_0x369a8c,_0x2cb538,_0x474cc8,_0x5d2b4a){const _0x21de44=await getAliasedTableByResourceName(_0x474cc8,_0x2cb538),_0x3e05d4=_0x5d2b4a['endsWith'](metas['idSuffix'])?_0x5d2b4a:_0x5d2b4a+metas['idSuffix'],_0x31e964=eq(_0x369a8c['id'],getTableColumn(_0x21de44,_0x3e05d4));return dbLeftJoin(_0x4a9fb7,_0x21de44,_0x31e964),_0x21de44;}__name(joinManySideRelation,'joinManySideRelation');async function joinOneSideRelation(_0x436d55,_0x7034b2,_0x31f37f,_0x43cf41){const _0x309df3=await getRelatedResourceName(_0x31f37f['resourceName'],_0x43cf41,_0x31f37f),_0x10e077=await getAliasedTableByResourceName(_0x309df3,_0x43cf41),_0x2e1248=_0x43cf41['endsWith'](metas['idSuffix'])?_0x43cf41:''+_0x43cf41+metas['idSuffix'],_0x1007ca=eq(_0x10e077['id'],getTableColumn(_0x7034b2,_0x2e1248));return dbLeftJoin(_0x436d55,_0x10e077,_0x1007ca),_0x10e077;}__name(joinOneSideRelation,'joinOneSideRelation');

export { expand, getSortColumns, limit, offset, select, sort };
