'use strict';
import { setPropertyValueFromAnotherResource } from '../services/property-metas.js';
import { crudActions } from '../../../../fd-toolbox/enums/enums.js';
import { checkPropertyPermission } from './helpers/cruds-permission-checker.js';
import { deleteResource, getTable, getResource } from '../db/db.js';
import { isRelationPropertyAnObjectOrArray } from '../relations/relations.js';
import { setWorkspaceId } from '../entities/edit-workspace.js';
import { getInternalResourceMeta } from '../resources/internal-resource-meta.js';
import { getEmptyUuid } from '../../../../fd-toolbox/uuids.js';
import { hasInternalPropertyMeta } from '../props/property-meta-config-provider.js';
import { prepareNewResource, setModifiedProperties } from '../entities/edit-base-resources.js';
import { propertyTypes } from '../../properties/property-type.js';
import { crudResultTypes } from '../../../../fd-toolbox/types/crud/crud-result-type.js';
import { createValidationDetailResult } from '../validations/meta-validations.js';
import { metaValidationMessages } from '../constants/meta-validation-messages.js';
import { setInvalidCrudResult } from '../../../../fd-toolbox/types/crud/crud-results.js';
import { metaPropertyNames } from '../constants/meta-property-names.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../constants/meta-error-messages-constants.js';
import { withWorkspaceIdProps } from '../../../../fd-toolbox-core/server/base-resources/props/with-workspace-id-props.js';
import { httpStatusCodes } from '../../../../fd-toolbox/constants/http-status-codes.js';
import { getObjectValues } from '../../../../fd-toolbox/lib/utils.js';
import { getCurrentWorkspaceId } from '../providers/current-workspace-id-provider.js';
import { hasPermission } from '../permission/permission-checker.js';
import { eq } from 'drizzle-orm';
import { dbDelete } from '../../../db/db-connection.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { getOneToManyRelationPropertyName } from '../meta-core/constants/metas.js';
import { isUuidSet } from '../../../../fd-toolbox/functions/value-checking-functions.js';
import { contentFormatNames } from '../../../../base-repo/constants/internal-common-strings.js';

var __defProp=Object['defineProperty'],__name=(_0x463d78,_0x12a78)=>__defProp(_0x463d78,'name',{'value':_0x12a78,'configurable':!![]});function checkMirrorManyToManyResource(_0x564ccd,_0x1425dd){if(!_0x564ccd['manyToManyInfo']?.['isMirrorManyToManyResource']||!(_0x564ccd['manyToManyInfo']['manyToManyFirstPropertyName']in _0x1425dd)||!(_0x564ccd['manyToManyInfo']['manyToManySecondPropertyName']in _0x1425dd))return;const _0x46a411=_0x1425dd,_0x5b726a=_0x46a411[_0x564ccd['manyToManyInfo']['manyToManyFirstPropertyName']],_0x4e6ce3=_0x46a411[_0x564ccd['manyToManyInfo']['manyToManySecondPropertyName']];if(_0x5b726a===_0x4e6ce3)throw createError(metaErrorMessages['mirrorManyToManyResourcesAreSame']);}__name(checkMirrorManyToManyResource,'checkMirrorManyToManyResource');async function validateAndDeleteResource(_0x386a6d,_0xba14b8,_0x163bc7){const _0x21aed7=getOneToManyRelationPropertyName(resourceNames['blob']);if(_0x163bc7['isPersistent']&&_0xba14b8['validationResult']?.['isValid'])try{const _0x192072=await deleteResource(_0x386a6d['resourceId'],_0x163bc7['resourceName']);if(_0x192072?.[_0x21aed7]&&Array['isArray'](_0x192072?.[_0x21aed7])){const _0x5cd224=await getTable(contentFormatNames['blob']);for(const _0x112839 of _0x192072[_0x21aed7]){await dbDelete(_0x5cd224)['where'](eq(_0x5cd224['id'],_0x112839));}}}catch(_0x1e3b4c){if(!_0x386a6d['ignoreDbUpdateConcurrencyException'])throw _0x1e3b4c;handleViolationForeignKeyConstraint(_0x1e3b4c,_0x386a6d);}}__name(validateAndDeleteResource,'validateAndDeleteResource');function handleViolationForeignKeyConstraint(_0xfdfddb,_0x5c82fa){if(_0xfdfddb instanceof Error&&_0xfdfddb['message']?.['includes']('violates\x20foreign\x20key\x20constraint')){const _0x2b0dc0=createValidationDetailResult(_0xfdfddb['message'],metaValidationMessages['violatesForeignKeyConstraint']['replace']('{0}',_0x5c82fa['resourceName']),_0x5c82fa['resourceName'],httpStatusCodes['internalServerError']);setInvalidCrudResult(_0x2b0dc0);}}__name(handleViolationForeignKeyConstraint,'handleViolationForeignKeyConstraint');async function getResourceForCreation(_0x5bbf27,_0x260991,_0x34742f,_0x333a32=![]){const _0x17c3e0={'id':_0x5bbf27['id']};for(const _0x27c6a2 of getObjectValues(_0x260991['properties'])){(!_0x333a32||_0x27c6a2['isOutsideEditable']&&!isRelationPropertyAnObjectOrArray(_0x27c6a2)||_0x27c6a2['name']===metaPropertyNames['workspaceId'])&&(_0x17c3e0[_0x27c6a2['name']]=_0x5bbf27[_0x27c6a2['name']]);}return await prepareNewResource(_0x17c3e0,_0x34742f,_0x260991['resourceName']),await setWorkspaceId(_0x17c3e0),_0x17c3e0;}__name(getResourceForCreation,'getResourceForCreation');async function doNotAllowToChangeWorkspaceId(_0x6296b9,_0x13d101,_0x830120){const _0x1fa885=_0x830120?.[withWorkspaceIdProps['workspaceId']];if(!isUuidSet(_0x13d101[metaPropertyNames['workspaceId']]))_0x13d101[metaPropertyNames['workspaceId']]=_0x1fa885??await getCurrentWorkspaceId();const _0x50931b=await getCurrentWorkspaceId();if(_0x50931b&&_0x1fa885&&_0x50931b!==_0x1fa885){const _0x8389cf='It\x20is\x20not\x20allowed\x20to\x20change\x20workspace',_0x42cb05={'message':_0x8389cf,'userFriendlyMessage':_0x8389cf,'code':'','propertyName':''};_0x6296b9['type']=crudResultTypes['notValid'],_0x6296b9['validationResult']={'isValid':![],'details':[_0x42cb05]};}_0x1fa885&&(_0x13d101[withWorkspaceIdProps['workspaceId']]=_0x1fa885);}__name(doNotAllowToChangeWorkspaceId,'doNotAllowToChangeWorkspaceId');async function modifyResourceFromDatabase(_0x216a21,_0x914bcc,_0x5b4649,_0xab9b3){const _0x351a85=await getInternalResourceMeta(_0x914bcc['resourceName']),_0x3fdb3a=await getResource({'resourceName':_0x351a85['name'],'id':_0x216a21['id']}),_0x5cc81c=await hasPermission(_0x914bcc['resourceName'],crudActions['update'],_0x5b4649);for(const _0xd05863 of Object['keys'](_0x216a21)){await checkSetPropertyValueFromAnotherResource(_0x216a21,_0x914bcc,_0x5b4649,_0xab9b3,_0x3fdb3a,_0x914bcc['properties'][_0xd05863],_0x5cc81c);}return setModifiedProperties(_0x3fdb3a,_0x5b4649,_0x351a85),_0x3fdb3a;}__name(modifyResourceFromDatabase,'modifyResourceFromDatabase');async function prepareResourceBeforeUpdate(_0x40e66c){!_0x40e66c['resource']['id']&&(_0x40e66c['resource']={..._0x40e66c['resource'],'id':getEmptyUuid()});const _0x5a549c=Object['hasOwn'](_0x40e66c['resource'],withWorkspaceIdProps['workspaceId']),_0x23174e=await hasInternalPropertyMeta(_0x40e66c['resourceName'],withWorkspaceIdProps['workspaceId']);!_0x5a549c&&_0x23174e&&(_0x40e66c['resource']={..._0x40e66c['resource'],'workspaceId':getEmptyUuid()});}__name(prepareResourceBeforeUpdate,'prepareResourceBeforeUpdate');function getCrudUpdateConfigFromProp(_0x37955b){return {..._0x37955b,'throwOnNonSuccess':_0x37955b['throwOnNonSuccess']??!![],'isCalledFromOutside':_0x37955b['isCalledFromOutside']??![]};}__name(getCrudUpdateConfigFromProp,'getCrudUpdateConfigFromProp');async function checkSetPropertyValueFromAnotherResource(_0x41c8c0,_0x5e9008,_0x1551e2,_0xbe1cc8,_0x16f9ee,_0x3d04a6,_0x1dd599=![]){const _0x182af5=_0x3d04a6['isOutsideEditable']||!_0xbe1cc8,_0x197418=_0x3d04a6['type']!==propertyTypes['object']&&_0x3d04a6['type']!==propertyTypes['array'];if(_0x182af5&&_0x197418){const _0x354298=_0x41c8c0[_0x3d04a6['name']],_0x88f1c1=_0x16f9ee[_0x3d04a6['name']],_0x5efa0c=Object['hasOwn'](_0x41c8c0,_0x3d04a6['name']);!_0x1dd599&&_0x5efa0c&&_0x354298!==_0x88f1c1&&await checkPropertyPermission(_0x5e9008,crudActions['update'],_0x1551e2,_0x41c8c0,_0x3d04a6['id']),!_0x3d04a6['isDynamic']&&setPropertyValueFromAnotherResource(_0x16f9ee,_0x41c8c0,_0x3d04a6);}}__name(checkSetPropertyValueFromAnotherResource,'checkSetPropertyValueFromAnotherResource');

export { checkMirrorManyToManyResource, doNotAllowToChangeWorkspaceId, getCrudUpdateConfigFromProp, getResourceForCreation, handleViolationForeignKeyConstraint, modifyResourceFromDatabase, prepareResourceBeforeUpdate, validateAndDeleteResource };
