'use strict';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { eq, inArray } from 'drizzle-orm';
import { dbInsert, dbUpdate, dbDelete } from '../../../db/db-connection.js';
import { getDbSchema } from '../../../db/db-schema.js';
import { executeQuery, getQuery } from '../web/drizzle.js';
import { applyFilterToQuery } from '../../../db/drizzle-queries.js';
import { toolboxErrorMessages } from '../../../../fd-toolbox/constants/toolbox-error-messages-constants.js';
import { convertFromPascalToCamelCase } from '../../../../fd-toolbox/strings/casing-converter.js';
import { getWithOData } from '../api/odata/odata-expressions.js';
import { oDataComparisonTypes, oDataFilterTypes } from '../../../../fd-toolbox/odata/odata-enums.js';
import { apiNotFoundError, apiConflictError } from '../../../../fd-toolbox/server/web/errors.js';
import { singleOrUndefinedWithWarning, first, firstOrUndefined, singleOrUndefined } from '../../../../fd-toolbox/server/collections/single-funcs.js';
import { getInternalResourceMeta } from '../resources/internal-resource-meta.js';
import { getOptionalPropertyValue } from '../../../../fd-toolbox/objects/property-value.js';

var __defProp=Object['defineProperty'],__name=(_0x2c157c,_0x1064a2)=>__defProp(_0x2c157c,'name',{'value':_0x1064a2,'configurable':!![]});async function getResourcesByName(_0x579e05){const _0x561735=await getTable(_0x579e05),_0x16f8a1=executeQuery(_0x561735);return _0x16f8a1;}__name(getResourcesByName,'getResourcesByName');async function getResource(_0x595af7,_0x4576ea=!![]){const _0x497ea7=await getResourcesWithFilter(_0x595af7['resourceName'],_0x2da19b=>eq(_0x2da19b['id'],_0x595af7['id']),_0x595af7['disableGlobalWorkspaceFilter']),_0x264121=singleOrUndefinedWithWarning(_0x497ea7);if(_0x4576ea&&!_0x264121)throw apiNotFoundError(toolboxErrorMessages['resourceNotFound']['replace']('{0}',_0x595af7['resourceName'])['replace']('{1}',_0x595af7['id']));return _0x264121;}__name(getResource,'getResource');async function getResourcesWithOData(_0x5cdc32,_0x9628c8){const _0x3c4c0b=await getTable(_0x5cdc32);return await getWithOData(_0x3c4c0b,_0x9628c8,_0x5cdc32);}__name(getResourcesWithOData,'getResourcesWithOData');async function getResourcesByFieldId(_0x3a6842,_0xd032f0,_0x493605){const _0x439209={'filters':[{'leftSide':_0xd032f0,'rightSide':_0x493605,'type':oDataFilterTypes['guid'],'comparisonType':oDataComparisonTypes['equal']}],'orderBys':[],'expands':[],'selects':[]};return getResourcesWithOData(_0x3a6842,_0x439209);}__name(getResourcesByFieldId,'getResourcesByFieldId');async function findResourceById(_0x4c7794,_0xf99f9b){const _0x325e0d=await getResourcesWithFilter(_0x4c7794,_0xca88b2=>eq(_0xca88b2['id'],_0xf99f9b));let _0x31f50d;if(_0x325e0d['length']===0x16f+0x33a*-0x2+0x506)_0x31f50d=first(_0x325e0d);return _0x31f50d;}__name(findResourceById,'findResourceById');async function hasAnyResource(_0x4767a9){const _0x1b1773=await getTable(_0x4767a9),{query:_0x19801c}=await getQuery(_0x1b1773),_0xd7461b=await execQuery(_0x19801c['limit'](0x95f*0x3+-0x1ec8+-0x3*-0xe4));return _0xd7461b['length']>-0xaa6+0x44*0x4c+-0x98a;}__name(hasAnyResource,'hasAnyResource');async function getResourcesByField(_0x5dbd01,_0x4220e4,_0x4ae672){const _0x444b91=await getTable(_0x5dbd01),{query:_0x31441d}=await getQuery(_0x444b91);return applyFilterToQuery(_0x31441d,eq(_0x4220e4,_0x4ae672)),execQuery(_0x31441d);}__name(getResourcesByField,'getResourcesByField');async function createResourceDirectlyInDb(_0x24d4e4,_0x149445,_0x1377cf=!![]){const _0x27840e=await getTable(_0x149445),_0xa04e4d=await dbInsert(_0x27840e)['values'](_0x24d4e4)['onConflictDoNothing']()['returning']();if(!firstOrUndefined(_0xa04e4d)&&_0x1377cf)throw apiConflictError(toolboxErrorMessages['resourceDatabaseInsertionConflictFound']['replace']('{0}',_0x24d4e4['id'])['replace']('{1}',_0x149445));return first(_0xa04e4d);}__name(createResourceDirectlyInDb,'createResourceDirectlyInDb');async function updateResource(_0x4e0fde,_0x37c2cb){const _0x2f891b=Object['fromEntries'](Object['entries'](_0x4e0fde)['map'](([_0x3f2273,_0x4a3149])=>[_0x3f2273,_0x4a3149===void(0x53b+0xc6d*-0x1+0x732)?null:_0x4a3149])),_0x4c7de4=await getTable(_0x37c2cb),_0x5e7705=await dbUpdate(_0x4c7de4)['set'](_0x2f891b)['where'](eq(_0x4c7de4['id'],_0x4e0fde['id']))['returning']();return first(_0x5e7705);}__name(updateResource,'updateResource');async function deleteResource(_0x35c9d9,_0x1e0e8){const _0x35dee6=await getTable(_0x1e0e8),_0x31ac41=await dbDelete(_0x35dee6)['where'](eq(_0x35dee6['id'],_0x35c9d9))['returning']();return singleOrUndefined(_0x31ac41);}__name(deleteResource,'deleteResource');async function deleteResources(_0x3d3ce6,_0x3594d3){let _0x2708fa;if(!_0x3594d3['length'])return _0x2708fa;const _0x4c19be=await getTable(_0x3d3ce6);return _0x2708fa=await dbDelete(_0x4c19be)['where'](inArray(_0x4c19be['id'],_0x3594d3))['returning'](),_0x2708fa;}__name(deleteResources,'deleteResources');async function getTable(_0x1b37a0){const _0x53cc61=await getInternalResourceMeta(_0x1b37a0),_0x5a20fa=convertFromPascalToCamelCase(_0x53cc61?.['persistence']?.['tableName'])+'Schema',_0xa94188=getDbSchema();if(!_0x5a20fa)throw createError(toolboxErrorMessages['tableForResourceNotFound']['replace']('{0}',_0x1b37a0)['replace']('{1}',_0x5a20fa));return getOptionalPropertyValue(_0xa94188,_0x5a20fa);}__name(getTable,'getTable');async function execQuery(_0x2105d4){return await _0x2105d4;}__name(execQuery,'execQuery');async function getResourcesWithFilter(_0x30e606,_0x545b8f,_0x305d4d){const _0x3e66fd=await getTable(_0x30e606),{query:_0x83a563}=await getQuery(_0x3e66fd,void(-0x1*0x17ea+0x5*-0x7bb+0x3e91),_0x305d4d);return applyFilterToQuery(_0x83a563,_0x545b8f(_0x3e66fd)),execQuery(_0x83a563);}__name(getResourcesWithFilter,'getResourcesWithFilter');

export { createResourceDirectlyInDb, deleteResource, deleteResources, execQuery, findResourceById, getResource, getResourcesByField, getResourcesByFieldId, getResourcesByName, getResourcesWithOData, getTable, hasAnyResource, updateResource };
