'use strict';
'use client';
import { getNowAsString } from '../../../../fd-toolbox/dates/date-time.js';
import { getLoginState } from '../../../../fd-toolbox/auth/internal-login-states.js';
import { nameOf } from '../../../../fd-toolbox-core/core/name-of.js';
import { withIdProps } from '../../../../fd-toolbox-core/types/resource-with-id.js';
import { isNullOrUndefined, ensureNotNullOrUndefined } from '../../../../fd-toolbox/functions/value-checking-functions.js';
import { propertyTypes, relationTypes, crudActions, oDataOperators } from '../../../../fd-toolbox/enums/enums.js';
import { propertyTypeFormats } from '../../constants/property-type-formats.js';
import { withUserIdProps } from '../../../../fd-toolbox/types/with-user-id.js';
import { apiDelete, apiGetWithOData } from '../../../../fd-toolbox/api/api-client.js';
import { getFlagsEnumNumber } from '../../../../fd-toolbox/collections/internal-enums.js';
import { isFlagsEnum, shouldBeAvailableForEditingOrCreate } from '../../core/property-types.js';
import { convertFromCamelToPascalCase } from '../../../../fd-toolbox/strings/casing-converter.js';
import { isManyToOneRelation, isOneToManyRelation } from '../../relations/meta-relations.js';
import { fetchResources } from '../../resource-providers.js';
import { isError, isString } from '../../../../fd-toolbox/types/ensure-type.js';
import { checkAndNotifyCrudPermission } from '../../crud/permissions/crud-permission-checker.js';
import { fillODataFromEditResourceConfig } from '../config/internal-edit-resource-configs.js';
import { getObjectValues } from '../../../../fd-toolbox/lib/utils.js';
import { getOptionalPropertyValue } from '../../../../fd-toolbox/objects/property-value.js';
import { oDataFilterTypes } from '../../../../fd-toolbox/odata/odata-enums.js';
import { hasCrudPermission } from '../../auth/permissions/permission-checker.js';
import { firstOrUndefined } from '../../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x5cb594,_0xf44122)=>__defProp(_0x5cb594,'name',{'value':_0xf44122,'configurable':!![]});function handleValidationError(_0x1b4fcb,_0xd2ea49){if(isError(_0x1b4fcb)&&Object['hasOwn'](_0x1b4fcb,nameOf('details')))for(let _0x4e6630 of _0x1b4fcb['details']){_0xd2ea49['setError'](_0x4e6630['propertyName'],{'message':_0x4e6630['userFriendlyMessage']});}}__name(handleValidationError,'handleValidationError');function prepareEditedResource(_0xbf162f,_0x54e5c1,_0x173679){for(let _0x5b0b7a of _0xbf162f){let _0x2278c1=_0x5b0b7a['name'];if(isManyToOneRelation(_0x5b0b7a)||isOneToManyRelation(_0x5b0b7a)){if(_0x2278c1['endsWith'](convertFromCamelToPascalCase(withIdProps['id']))&&_0x2278c1['length']>withIdProps['id']['length']){const _0x132dde=getResourceId(_0x2278c1,_0x173679);(_0x5b0b7a['required']||_0x132dde)&&(_0x54e5c1[_0x2278c1]=_0x132dde);}}else {if(isFlagsEnum(_0x5b0b7a['type'],_0x5b0b7a['typeFormat'])&&Array['isArray'](_0x173679[_0x2278c1]))_0x54e5c1[_0x2278c1]=getFlagsEnumNumber(_0x173679[_0x2278c1]);else {let _0x511d23=_0x173679;_0x54e5c1[_0x2278c1]=_0x511d23[_0x2278c1];}}}}__name(prepareEditedResource,'prepareEditedResource');async function deleteResource(_0x5b091b,_0x3b300c,_0x56d30b,_0x335f2e){_0x5b091b&&(await apiDelete(_0x3b300c,_0x56d30b['id']),_0x335f2e?.());}__name(deleteResource,'deleteResource');function prepareMetaRelationsToUpdate(_0x33d3d3){let _0x14ab76=[];for(let [_0x5b150c,_0x38eed4]of _0x33d3d3){_0x14ab76['push']({'propertyName':_0x5b150c['name'],'relatedResourceIds':_0x38eed4['map'](_0x348cc4=>_0x348cc4['id'])});}return _0x14ab76;}__name(prepareMetaRelationsToUpdate,'prepareMetaRelationsToUpdate');function ensureDefaultValueForPropertyType(_0x176c26,_0x1f83a9,_0x2095dc=![]){let _0x120ebd=getOptionalPropertyValue(_0x1f83a9,_0x176c26['name']);_0x176c26['type']===propertyTypes['boolean']&&(_0x120ebd=![]);if(isNullOrUndefined(_0x1f83a9[_0x176c26['name']])&&_0x176c26['required']){if(_0x176c26['typeFormat']===propertyTypeFormats['dateTime'])_0x120ebd=getNowAsString();else _0x176c26['typeFormat']===propertyTypeFormats['enum']&&(_0x120ebd=firstOrUndefined(ensureNotNullOrUndefined(_0x176c26['enumerationValues'])));}return _0x176c26['name']===withUserIdProps['userId']&&_0x2095dc&&(_0x1f83a9[_0x176c26['name']]=getLoginState()['userId'],_0x120ebd=getLoginState()['userId']),_0x120ebd;}__name(ensureDefaultValueForPropertyType,'ensureDefaultValueForPropertyType');function compareFormValue(_0x52a542,_0x194b73,_0x58a659,_0x9ff443){const _0x3e121d=_0x9ff443['properties'][_0x58a659],_0x2daa76=_0x52a542[_0x58a659],_0x5192b8=_0x194b73[_0x58a659];let _0x4c0a9a=_0x2daa76===_0x5192b8;if(_0x3e121d){const _0x4c7f00=_0x3e121d['relation']?.['type']===relationTypes['oneToMany']||_0x3e121d['relation']?.['type']===relationTypes['manyToMany'];isString(_0x2daa76)&&isString(_0x5192b8)&&_0x4c7f00&&(_0x4c0a9a=_0x2daa76['split'](',')['sort']((_0x2bb679,_0x491021)=>_0x2bb679['localeCompare'](_0x491021))['join'](',')===_0x5192b8['split'](',')['sort']((_0x52173c,_0x1227c8)=>_0x52173c['localeCompare'](_0x1227c8))['join'](','));}return _0x4c0a9a;}__name(compareFormValue,'compareFormValue');async function getStringifiedResourceRelations(_0x2b3535,_0x32ff96){const _0x89e2bd={},_0x8689b8=await getResourceWithSelectedManySideRelationProperties(_0x32ff96,_0x2b3535);if(!_0x8689b8)return _0x89e2bd;for(const _0x52c762 of Object['values'](_0x32ff96['properties'])){if(_0x52c762['relation']?.['type']===relationTypes['oneToMany']||_0x52c762['relation']?.['type']===relationTypes['manyToMany']){const _0x52f701=getRelatedIdValuesForResources(_0x8689b8,_0x52c762);_0x89e2bd[_0x52c762['name']]=_0x52f701;}}return _0x89e2bd;}__name(getStringifiedResourceRelations,'getStringifiedResourceRelations');function checkPermission(_0x3224ee,_0x23b09a,_0x4cbe72){let _0x3ec0cf=_0x3224ee?crudActions['create']:crudActions['view'];checkAndNotifyCrudPermission(_0x3ec0cf,_0x23b09a['resourceMeta'],_0x4cbe72);}__name(checkPermission,'checkPermission');async function fetchAndSetResource(_0x47174e,_0x53e488,_0x4390b5,_0x459549,_0x1fbde4,_0x462d6e){if(!_0x459549){const _0x9f7531=await apiGetWithOData(_0x53e488['resourceName'],getODataConfig(_0x462d6e),_0x4390b5);return _0x9f7531['data']&&_0x47174e(_0x9f7531['data']),_0x1fbde4(await getStringifiedResourceRelations(_0x4390b5,_0x53e488)),_0x9f7531['data'];}return void(-0x47*-0x49+0x754+-0x1*0x1b93);}__name(fetchAndSetResource,'fetchAndSetResource');function getODataConfig(_0x3dc6f0){let _0x4eeb0b={'expands':[]};return fillODataFromEditResourceConfig(_0x4eeb0b,_0x3dc6f0),_0x4eeb0b;}__name(getODataConfig,'getODataConfig');function createFormLikeObject(_0x3dac5,_0x979ccd){const _0x548848={};for(const _0x519fc7 of Object['keys'](_0x979ccd)){_0x548848[_0x519fc7]=_0x979ccd[_0x519fc7]['join'](',');}return {..._0x3dac5,..._0x548848};}__name(createFormLikeObject,'createFormLikeObject');function getAllPropertyMetas(_0x13129d,_0xda5ccd){let _0x1fbf22=getObjectValues(_0xda5ccd['properties'])['filter'](_0x39fd54=>{const _0x166ed5=_0xda5ccd['resourceMeta']['properties'][_0x39fd54['name']];return shouldBeAvailableForEditingOrCreate(_0x39fd54,_0x166ed5);});for(const _0x4781ca of _0x1fbf22){_0x13129d[_0x4781ca['name']]=_0x4781ca;}return [_0x13129d];}__name(getAllPropertyMetas,'getAllPropertyMetas');function shouldEditResourceShowDeleteButton(_0x3a8401,_0x459ded){return !!_0x459ded&&!_0x3a8401&&hasCrudPermission(crudActions['delete'],_0x459ded);}__name(shouldEditResourceShowDeleteButton,'shouldEditResourceShowDeleteButton');function shouldEditResourceShowSaveCancelButtons(_0x4ebb0a){return !!_0x4ebb0a&&hasCrudPermission(crudActions['update'],_0x4ebb0a);}__name(shouldEditResourceShowSaveCancelButtons,'shouldEditResourceShowSaveCancelButtons');function shouldEditResourceShowCreateButton(_0x50d857){return !!_0x50d857&&hasCrudPermission(crudActions['create'],_0x50d857);}__name(shouldEditResourceShowCreateButton,'shouldEditResourceShowCreateButton');function shouldShowTabs(_0xf901c1){const _0x434597=_0xf901c1['resourceMeta']['properties'];let _0x5e2185=![];if(_0x434597){let _0x121ee0=0x1*-0x35+-0x1e0+0x215;for(const _0x2f9dd5 of getObjectValues(_0x434597)){!isNullOrUndefined(_0x2f9dd5['relation'])&&_0x2f9dd5['isDisplayable']&&_0x121ee0++;}_0x5e2185=_0x121ee0>=0x1*-0x15c1+0x4*-0x518+0x1d*0x174;}return _0x5e2185;}__name(shouldShowTabs,'shouldShowTabs');function getResourceId(_0x401cdb,_0x4a30d6){let _0x527dd5=_0x4a30d6;return _0x527dd5[_0x401cdb];}__name(getResourceId,'getResourceId');function getRelatedIdValuesForResources(_0x396f75,_0x5d109e){const _0x38c4fe=getOptionalPropertyValue(_0x396f75,_0x5d109e['name']),_0xbe71e2=[];if(!Array['isArray'](_0x38c4fe))return _0xbe71e2;if(_0x5d109e['relation']?.['type']===relationTypes['oneToMany'])for(const _0x17e426 of _0x38c4fe){_0x17e426[withIdProps['id']]&&isString(_0x17e426[withIdProps['id']])&&_0xbe71e2['push'](_0x17e426[withIdProps['id']]);}else {if(_0x5d109e['relation']?.['type']===relationTypes['manyToMany']){const _0x10d198=_0x5d109e['relation']['manyToManyInfo']['manyToManySecondPropertyName'];for(const _0x32bc90 of _0x38c4fe){_0x32bc90[_0x10d198]&&isString(_0x32bc90[_0x10d198])&&_0xbe71e2['push'](_0x32bc90[_0x10d198]);}}}return _0xbe71e2;}__name(getRelatedIdValuesForResources,'getRelatedIdValuesForResources');async function getResourceWithSelectedManySideRelationProperties(_0x2f4142,_0x12214f){let _0x1f50c4;const _0x4fafb1=[];_0x4fafb1['push'](withIdProps['id']);const _0xeb0f17={'type':oDataFilterTypes['guid'],'leftSide':withIdProps['id'],'rightSide':_0x12214f,'operator':oDataOperators['equal']};for(const _0x4f82c2 of Object['values'](_0x2f4142['properties'])){_0x4f82c2['relation']&&_0x4f82c2['relation']['isExpandProperty']&&_0x4f82c2['relation']['type']!==relationTypes['manyToOne']&&_0x4fafb1['push'](_0x4f82c2['name']);}const _0x273029=await fetchResources(_0x2f4142['resourceName'],{'selects':_0x4fafb1,'filters':[_0xeb0f17]});return _0x273029&&(_0x1f50c4=firstOrUndefined(_0x273029)),_0x1f50c4;}__name(getResourceWithSelectedManySideRelationProperties,'getResourceWithSelectedManySideRelationProperties');

export { checkPermission, compareFormValue, createFormLikeObject, deleteResource, ensureDefaultValueForPropertyType, fetchAndSetResource, getAllPropertyMetas, getODataConfig, getStringifiedResourceRelations, handleValidationError, prepareEditedResource, prepareMetaRelationsToUpdate, shouldEditResourceShowCreateButton, shouldEditResourceShowDeleteButton, shouldEditResourceShowSaveCancelButtons, shouldShowTabs };
