'use strict';
import { getInternalResourceMeta } from '../resources/internal-resource-meta.js';
import { isDefaultValue } from '../value-checker.js';
import _0x20e13f from 'lodash.isequal';
import { getResource } from '../db/db.js';
import { getEmptyUuid } from '../../../../fd-toolbox/uuids.js';
import { relationTypes, propertyTypes } from '../../../../fd-toolbox/enums/enums.js';
import { propertyTypeFormats } from '../../constants/property-type-formats.js';
import { withIdProps } from '../../../../fd-toolbox-core/types/resource-with-id.js';
import { metaValidationMessages } from '../constants/meta-validation-messages.js';
import { getPropertyMeta } from '../services/metas.js';
import { httpStatusCodes } from '../../../../fd-toolbox/constants/http-status-codes.js';
import { getObjectValues } from '../../../../fd-toolbox/lib/utils.js';
import { getOptionalPropertyValue } from '../../../../fd-toolbox/objects/property-value.js';
import { isNumber, isString } from '../../../../fd-toolbox/types/ensure-type.js';
import { emptyUuid } from '../../../../fd-toolbox/constants/constants.js';
import { firstOrUndefined } from '../../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x3c8ff9,_0x3528b7)=>__defProp(_0x3c8ff9,'name',{'value':_0x3528b7,'configurable':!![]});const emptyResourceId=emptyUuid;async function validateResource(_0x3e4897,_0x33c369){let _0x242783={'id':_0x3e4897['id'],'isValid':!![],'details':[]};for(let _0x4d6832 of getObjectValues(_0x33c369['properties'])){const _0x41794e=getOptionalPropertyValue(_0x3e4897,_0x4d6832['name']);_0x4d6832['isDisplayable']&&(checkEnumPropertyType(_0x4d6832,_0x242783,_0x41794e),await checkRequired(_0x33c369,_0x3e4897,_0x4d6832,_0x242783,_0x41794e),await checkManyToOneRelationId(_0x4d6832,_0x242783,_0x41794e));}return _0x242783;}__name(validateResource,'validateResource');function validateId(_0x4303e1){const _0x281b80={'id':_0x4303e1,'isValid':!![],'details':[]};if(_0x4303e1===emptyResourceId){const _0x26f47a=createValidationDetailResult(metaValidationMessages['requiredFieldCantBeDefault']['replace']('{0}',withIdProps['id']),metaValidationMessages['requiredFieldCantBeDefaultUserFriendly']['replace']('{0}',withIdProps['id']),withIdProps['id'],httpStatusCodes['internalServerError']);return invalidateValidationResult(_0x26f47a,_0x281b80);}return _0x281b80;}__name(validateId,'validateId');function createValidationDetailResult(_0x5252d8,_0x2fd7fd,_0x272749,_0xe607ce){const _0x50a37e={'message':_0x5252d8,'userFriendlyMessage':_0x2fd7fd,'propertyName':_0x272749,'code':String(_0xe607ce)};return _0x50a37e;}__name(createValidationDetailResult,'createValidationDetailResult');function checkEnumPropertyType(_0x3fd1c1,_0x264e0c,_0x1e31ca){if(isNumber(_0x1e31ca)&&_0x3fd1c1['typeFormat']===propertyTypeFormats['enum']&&_0x3fd1c1['enumerationValues']&&!_0x3fd1c1['enumerationValues']['includes'](_0x1e31ca)){const _0x226e0e=createValidationDetailResult(metaValidationMessages['enumOutOfRange']['replace']('{0}',_0x3fd1c1['name']),metaValidationMessages['enumOutOfRangeUserFriendly'],_0x3fd1c1['name'],httpStatusCodes['internalServerError']);invalidateValidationResult(_0x226e0e,_0x264e0c);}}__name(checkEnumPropertyType,'checkEnumPropertyType');async function checkManyToOneRelationId(_0x4b332a,_0xc210c9,_0x3e17bf){if(isString(_0x3e17bf)&&_0x4b332a['relation']&&_0x4b332a['relation']?.['type']===relationTypes['manyToOne']&&!_0x4b332a['relation']['isExpandProperty']&&_0x3e17bf!==_0xc210c9['id']){const _0x2a4d12=await getInternalResourceMeta(_0x4b332a['relation']['relatedResourceName']);let _0x266341;if(_0x2a4d12)_0x266341=await getResource({'resourceName':_0x2a4d12['name'],'id':_0x3e17bf},![]);if(!_0x266341){const _0x6af0c7=createValidationDetailResult(metaValidationMessages['invalidValue']['replace']('{0}',_0x4b332a['name']),metaValidationMessages['invalidUserFriendly']['replace']('{0}',_0x4b332a['displayName']),_0x4b332a['name'],httpStatusCodes['internalServerError']);invalidateValidationResult(_0x6af0c7,_0xc210c9);}}}__name(checkManyToOneRelationId,'checkManyToOneRelationId');async function checkRequired(_0x1544b6,_0x127272,_0x2a8bd3,_0x48346e,_0x5830db){if(_0x2a8bd3['required'])checkRequiredForValueTypesOrObjects(_0x5830db,_0x48346e,_0x2a8bd3);else {if(_0x2a8bd3['requiredIfOtherArePresent']){const _0x259703=await getInternalResourceMeta(_0x1544b6['resourceName']),_0x5647e7=_0x259703?.['properties'],_0x1d12cc=_0x5647e7?.[firstOrUndefined(_0x2a8bd3['requiredIfOtherArePresent']??[])??'']?.['name'],_0x2bb32b=(await getPropertyMeta(_0x1544b6))[_0x1d12cc],_0xe920a2=getOptionalPropertyValue(_0x127272,_0x1d12cc);!isDefaultValue(_0x2bb32b,_0xe920a2)&&_0xe920a2!==![]&&checkRequiredForValueTypesOrObjects(_0x5830db,_0x48346e,_0x2a8bd3);}else {if(_0x2a8bd3['requiredIfPropHasValue']){const [_0x413e3d]=_0x2a8bd3['requiredIfPropHasValue'],_0xae19a4=getOptionalPropertyValue(_0x127272,_0x413e3d);_0x20e13f(_0xae19a4,_0x2a8bd3['requiredIfPropHasValue'][-0x11b5+-0xbeb+0x5*0x5ed])&&checkRequiredForValueTypesOrObjects(_0x5830db,_0x48346e,_0x2a8bd3);}}}}__name(checkRequired,'checkRequired');function checkRequiredForValueTypesOrObjects(_0x42a922,_0x913a27,_0x530951){if(_0x42a922===void(0xf3c+0x2*-0x8da+-0x4*-0x9e)||isNumber(_0x42a922)&&_0x42a922<-0x137*0x19+-0x22d2+0x4131||_0x42a922===''||isDefaultValue(_0x530951,_0x42a922)&&_0x42a922!==-0x80e*0x1+-0x9f*0xf+0x1*0x115f&&_0x42a922!==![]||_0x42a922===getEmptyUuid())defineInvalidValidationResult(_0x42a922,_0x913a27,_0x530951);}__name(checkRequiredForValueTypesOrObjects,'checkRequiredForValueTypesOrObjects');function defineInvalidValidationResult(_0x19f3a2,_0x6785ae,_0x1cad10){let _0x11989d;if(!isValueType(_0x1cad10)&&_0x19f3a2===void(0x13*-0x1d3+0x1a27+0x6*0x16b))_0x11989d=createValidationDetailResult(metaValidationMessages['requiredFieldCantBeNull']['replace']('{0}',_0x1cad10['name']),metaValidationMessages['requiredFieldCantBeNullUserFriendly']['replace']('{0}',_0x1cad10['name']),_0x1cad10['name'],httpStatusCodes['internalServerError']);else {if(_0x19f3a2==='')_0x11989d=createValidationDetailResult(metaValidationMessages['requiredStringFieldCantBeEmpty']['replace']('{0}',_0x1cad10['name']),metaValidationMessages['requiredStringFieldCantBeEmptyUserFriendly'],_0x1cad10['name'],httpStatusCodes['internalServerError']);else {if(isNumber(_0x19f3a2)&&_0x19f3a2<-0xc9*-0x2b+-0x19da+0x2d*-0x2d)_0x11989d=createValidationDetailResult(metaValidationMessages['numberCantBeNegative']['replace']('{0}',_0x1cad10['name']),metaValidationMessages['numberCantBeNegativeUserFriendly'],_0x1cad10['name'],httpStatusCodes['internalServerError']);else _0x11989d=createValidationDetailResult(metaValidationMessages['requiredFieldCantBeDefault']['replace']('{0}',_0x1cad10['name']),metaValidationMessages['requiredFieldCantBeDefaultUserFriendly']['replace']('{0}',_0x1cad10['name']),_0x1cad10['name'],httpStatusCodes['internalServerError']);}}invalidateValidationResult(_0x11989d,_0x6785ae);}__name(defineInvalidValidationResult,'defineInvalidValidationResult');function isValueType(_0x552c6b){return _0x552c6b['type']===propertyTypes['boolean']||_0x552c6b['type']===propertyTypes['integer']||_0x552c6b['type']===propertyTypes['number'];}__name(isValueType,'isValueType');function invalidateValidationResult(_0x1eeb3b,_0x388303){const _0x2b28e5=_0x388303??{'isValid':!![],'details':[]};return _0x2b28e5['isValid']=![],_0x2b28e5['details']['push'](_0x1eeb3b),_0x2b28e5;}__name(invalidateValidationResult,'invalidateValidationResult');

export { checkEnumPropertyType, checkManyToOneRelationId, createValidationDetailResult, validateId, validateResource };
