'use strict';
import { getUserId } from '../../auth/users/current-user-id-provider.js';
import { crudUpdate, crudDelete, crudCreate } from '../../basic-crud/cruds.js';
import { crudResultTypes } from '../../../../../fd-toolbox/types/crud/crud-result-type.js';
import { getODataConfigFromRequest } from '../../api/odata/odata.js';
import { getResources, tryGetResourceById } from '../../services/resource-provider.js';
import { apiOkResponse, getJsonResponse, apiCreatedResponse, apiNoContentResponse } from '../../web/responses.js';
import { propertyTypes } from '../../../properties/property-type.js';
import { apiInternalServerError, apiNotFoundError, apiBadRequestError } from '../../../../../fd-toolbox/server/web/errors.js';
import { triggerIntegrationEvent, subscriptionEventStrings, subscriptionEventTypes } from '../../integration-platforms/integration-events.js';
import { httpStatusCodes } from '../../../../../fd-toolbox/constants/http-status-codes.js';
import { metaErrorMessages } from '../../../constants/meta-error-messages-constants.js';
import { parseJsonBodyFromRequest, getObjectValues, hasObjectOwnProperty } from '../../../../../fd-toolbox/lib/utils.js';
import { parseUtcStringToDate } from '../../../../../fd-toolbox/dates/date-time.js';
import { getInternalResourceMeta } from '../../resources/internal-resource-meta.js';
import { isNullOrUndefined } from '../../../../../fd-toolbox/functions/value-checking-functions.js';
import { jsBasicTypes } from '../../../../../base-repo/constants/basic-types.js';

var __defProp=Object['defineProperty'],__name=(_0x262c2c,_0x4e25df)=>__defProp(_0x262c2c,'name',{'value':_0x4e25df,'configurable':!![]});async function crudGet(_0x307d33,_0x169dce){let _0x55f761=await getODataConfigFromRequest(_0x307d33);const _0x234af6=await getUserId(),_0x9bd65e=await getResources(_0x169dce,_0x234af6,_0x55f761);return apiOkResponse(_0x9bd65e);}__name(crudGet,'crudGet');async function crudPut(_0x2c327b,_0x24be6f){const _0x490c8e=await getEditResourceFromRequest(_0x2c327b,_0x24be6f),_0x1781cf={'resourceName':_0x490c8e['resourceName'],'resource':_0x490c8e['resource'],'userId':await getUserId(),'propertyRelationSyncInfos':_0x490c8e['propertyRelationSyncInfos'],'throwOnNonSuccess':![],'isCalledFromOutside':!![]},_0x3dcc81=await crudUpdate(_0x1781cf);switch(_0x3dcc81['type']){case crudResultTypes['success']:{return await triggerIntegrationEvent(subscriptionEventStrings[subscriptionEventTypes['resourceEdited']],_0x24be6f),apiOkResponse(_0x3dcc81);}case crudResultTypes['notValid']:{return getJsonResponse({'data':_0x3dcc81['validationResult']},httpStatusCodes['badRequest']);}default:{throw apiInternalServerError();}}}__name(crudPut,'crudPut');async function controllerDelete(_0x4b3772,_0x494f2b){const _0x21a390={'resourceId':_0x494f2b,'userId':await getUserId(),'resourceName':_0x4b3772,'ignoreDbUpdateConcurrencyException':![]},_0x10da7f=await crudDelete(_0x21a390),_0x51b0fe=handleCrudResult(_0x10da7f);return _0x51b0fe['status']===httpStatusCodes['noContent']&&await triggerIntegrationEvent(subscriptionEventStrings[subscriptionEventTypes['resourceRemoved']],_0x4b3772,_0x494f2b),_0x51b0fe;}__name(controllerDelete,'controllerDelete');async function crudGetById(_0x1cb5ab,_0xefc106,_0x33e98d){const _0xc5ce75=await tryGetResourceById(_0xefc106,_0x33e98d,await getUserId(),![]);if(isNullOrUndefined(_0xc5ce75))throw apiNotFoundError();return apiOkResponse(_0xc5ce75);}__name(crudGetById,'crudGetById');async function crudPost(_0x529924,_0x2a9fef){const _0x466edc=await getEditResourceFromRequest(_0x529924,_0x2a9fef);if(_0x466edc instanceof Response)return _0x466edc;const _0x572de5={'resourceName':_0x466edc['resourceName'],'resource':_0x466edc['resource'],'userId':await getUserId(),'propertyRelationSyncInfos':_0x466edc['propertyRelationSyncInfos'],'throwOnNonSuccess':![],'isCalledFromOutside':!![]},_0x12ffd2=await crudCreate(_0x572de5);switch(_0x12ffd2['type']){case crudResultTypes['success']:{return await triggerIntegrationEvent(subscriptionEventStrings[subscriptionEventTypes['resourceAdded']],_0x2a9fef),apiCreatedResponse(_0x12ffd2);}case crudResultTypes['notValid']:{return getJsonResponse({'data':_0x12ffd2['validationResult']},httpStatusCodes['badRequest']);}default:{throw apiInternalServerError();}}}__name(crudPost,'crudPost');async function crudUpsert(_0x48b138,_0x5e5099){const _0x5afb8e=await getEditResourceFromRequest(_0x48b138,_0x5e5099);if(_0x5afb8e instanceof Response)return _0x5afb8e;const _0x1ac7f8={'resourceName':_0x5afb8e['resourceName'],'resource':_0x5afb8e['resource'],'userId':await getUserId(),'propertyRelationSyncInfos':_0x5afb8e['propertyRelationSyncInfos'],'throwOnNonSuccess':![],'isCalledFromOutside':!![],'isUpsert':!![]},_0x418330=await crudUpdate(_0x1ac7f8);switch(_0x418330['type']){case crudResultTypes['success']:{return apiOkResponse(_0x418330);}case crudResultTypes['notValid']:{return getJsonResponse({'data':_0x418330['validationResult']},httpStatusCodes['badRequest']);}default:{throw apiInternalServerError();}}}__name(crudUpsert,'crudUpsert');function handleCrudResult(_0x244404){let _0x31d929;switch(_0x244404['type']){case crudResultTypes['success']:return apiNoContentResponse();case crudResultTypes['notValid']:{_0x31d929={'data':_0x244404['validationResult']};throw apiBadRequestError(_0x31d929['message']);}default:throw apiInternalServerError();}}__name(handleCrudResult,'handleCrudResult');async function getEditResourceFromRequest(_0x2c845b,_0x56ab4c){const _0x43b58f=await getInternalResourceMeta(_0x56ab4c),_0x3b9629=await parseJsonBodyFromRequest(_0x2c845b);if(!_0x3b9629?.['resource'])throw apiBadRequestError(metaErrorMessages['resourceBodyNotFound']['replace']('{0}',_0x56ab4c));const _0x53f628=createNewResource(_0x43b58f,_0x3b9629['resource']),_0x16faaa=_0x3b9629['propertyRelationSyncInfos'],_0x1cb66f=[];if(_0x16faaa)for(const _0x4ee0ce of _0x16faaa){_0x1cb66f['push']({'propertyName':_0x4ee0ce['propertyName'],'relatedResourceIds':_0x4ee0ce['relatedResourceIds']});}const _0x1023bd={'resourceName':_0x56ab4c,'resource':_0x53f628,'propertyRelationSyncInfos':_0x1cb66f};return _0x1023bd;}__name(getEditResourceFromRequest,'getEditResourceFromRequest');function createNewResource(_0x268562,_0x5269f5){const _0x1ffb3a={'id':_0x5269f5['id']};for(const _0x2df061 of getObjectValues(_0x268562['properties'])){if(hasObjectOwnProperty(_0x5269f5,_0x2df061['name'])){let _0x5e00b9=_0x5269f5[_0x2df061['name']];if(_0x2df061['type']){if(typeof _0x5e00b9===jsBasicTypes['string']&&(_0x2df061['type']===propertyTypes['date']||_0x2df061['type']===propertyTypes['dateTime']||_0x2df061['type']===propertyTypes['time']))_0x5e00b9=parseUtcStringToDate(_0x5e00b9);else typeof _0x5e00b9===jsBasicTypes['string']&&(_0x2df061['type']===propertyTypes['enum']||_0x2df061['type']===propertyTypes['integer'])&&(_0x5e00b9=parseInt(_0x5e00b9,0x1e0f+0x132c+0x707*-0x7));}_0x1ffb3a[_0x2df061['name']]=_0x5e00b9;}}return _0x1ffb3a;}__name(createNewResource,'createNewResource');

export { controllerDelete, crudGet, crudGetById, crudPost, crudPut, crudUpsert };
