'use strict';
import { getCurrentWorkspaceId } from '../../../meta/server/providers/current-workspace-id-provider.js';
import { getChildWorkspaces } from '../infra/workspaces/workspace-db.js';
import { getUserId } from '../../../meta/server/auth/users/current-user-id-provider.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { deleteResources, getResourcesWithOData } from '../../../meta/server/db/db.js';
import { getODataFilterUuid } from '../../../../fd-toolbox/odata/odata-builder-funcs.js';
import { withUserIdProps } from '../../../../fd-toolbox/types/with-user-id.js';
import { userVisibleWorkspaceProps } from '../../../meta/core/resources/users/props/user-visible-workspace-props.js';
import { UserVisibleWorkspace } from '../../../meta/core/resources/users/user-visible-workspace.resource.js';
import { crudCreate } from '../../../meta/server/basic-crud/cruds.js';
import { getCrudUpdateConfig } from '../../../meta/server/meta-core/crud/crud-update-config.js';
import { withIdProps } from '../../../../fd-toolbox-core/types/resource-with-id.js';
import { workspaceProps } from '../multitenancy/props/workspace-props.js';
import { fillBaseResourceProps } from '../base-resources/props/fill-base-resource-props.js';
import { tryGetObjectFromCoreCache, setCoreCache } from '../../../../fd-toolbox/server/cachings/core-cache.js';
import { oDataSeparators } from '../../../../fd-toolbox/odata/odata-filter-constants.js';
import { getVisibleWorkspacesCacheKey } from '../../../meta/server/workspace/get-visible-workspaces-cache-key.js';

var __defProp=Object['defineProperty'],__name=(_0x19764a,_0x2ec0bd)=>__defProp(_0x19764a,'name',{'value':_0x2ec0bd,'configurable':!![]});async function getUserVisibleWorkspaces(){const _0x1f332b=await getCurrentWorkspaceId(),_0xfa80e0=await getVisibleWorkspaces(),_0x5adf4b=await getChildWorkspaces(_0x1f332b),_0x56a57d=_0x5adf4b['map'](({id:_0x2970f7,name:_0x405ad3})=>({'id':_0x2970f7,'name':_0x405ad3})),_0x39d62d={'visibleWorkspacesId':_0xfa80e0,'currentWorkspaceChildren':_0x56a57d};return _0x39d62d;}__name(getUserVisibleWorkspaces,'getUserVisibleWorkspaces');async function getVisibleWorkspaces(){const _0x1bbee8=await getUserId(),_0xd6f27c=await getCurrentWorkspaceId(),_0x2913d0=getVisibleWorkspacesCacheKey(_0x1bbee8,_0xd6f27c),_0xa423a2=await tryGetObjectFromCoreCache(_0x2913d0);if(_0xa423a2)return _0xa423a2;const _0x13d1bf=await getUserToVisibleWorkspaceRecords(_0x1bbee8,_0xd6f27c),_0x425afd=_0x13d1bf['map'](_0x5590bf=>_0x5590bf['visibleWorkspaceId']);return await setVisibleWorkspacesToCache(_0x425afd,_0x1bbee8,_0xd6f27c),_0x425afd;}__name(getVisibleWorkspaces,'getVisibleWorkspaces');async function setVisibleWorkspaces(_0x4277fd){const _0x5d3114=await getUserId(),_0x3260fb=await getCurrentWorkspaceId(),_0x21357a=await getChildWorkspaces(_0x3260fb);if(!shouldWorkspacesBeVisible(_0x21357a,_0x4277fd))return;await deletePreviousVisibleWorkspaces(_0x5d3114,_0x3260fb);for(const _0x1df91d of _0x4277fd){const _0x168649=await fillBaseResourceProps(new UserVisibleWorkspace());_0x168649['visibleWorkspaceId']=_0x1df91d,_0x168649['userId']=_0x5d3114;const _0x2c93a8=await getCrudUpdateConfig(_0x168649,resourceNames['userVisibleWorkspace'],_0x5d3114);await crudCreate(_0x2c93a8);}await setVisibleWorkspacesToCache(_0x4277fd,_0x5d3114,_0x3260fb);}__name(setVisibleWorkspaces,'setVisibleWorkspaces');async function deletePreviousVisibleWorkspaces(_0x42aa7a,_0x5519ba){const _0x153a12=await getUserToVisibleWorkspaceRecords(_0x42aa7a,_0x5519ba);await deleteResources(resourceNames['userVisibleWorkspace'],_0x153a12['map'](_0xe4750b=>_0xe4750b['id']));}__name(deletePreviousVisibleWorkspaces,'deletePreviousVisibleWorkspaces');async function getUserToVisibleWorkspaceRecords(_0x30bb26,_0x231a55){const _0x557588=getODataFilterUuid(withUserIdProps['userId'],_0x30bb26);_0x557588['selectArray']([withIdProps['id'],userVisibleWorkspaceProps['visibleWorkspaceId'],userVisibleWorkspaceProps['visibleWorkspace']+oDataSeparators['propertySeparator']+workspaceProps['parentWorkspaceId']]),_0x557588['expand'](userVisibleWorkspaceProps['visibleWorkspace']);const _0x21f2de=await getResourcesWithOData(resourceNames['userVisibleWorkspace'],_0x557588['build']());return _0x21f2de['filter'](_0x1cb126=>{return _0x1cb126['visibleWorkspace']?.['parentWorkspaceId']===_0x231a55;});}__name(getUserToVisibleWorkspaceRecords,'getUserToVisibleWorkspaceRecords');async function setVisibleWorkspacesToCache(_0x4d63f6,_0x697e74,_0x1832ae){const _0x238b2b=getVisibleWorkspacesCacheKey(_0x697e74,_0x1832ae);await setCoreCache(_0x238b2b,_0x4d63f6);}__name(setVisibleWorkspacesToCache,'setVisibleWorkspacesToCache');function shouldWorkspacesBeVisible(_0x1354ba,_0x1715b2){const _0x5754ea=new Set(_0x1354ba['map'](_0x857273=>_0x857273['id']));return _0x1715b2['every'](_0x5754ea['has']['bind'](_0x5754ea));}__name(shouldWorkspacesBeVisible,'shouldWorkspacesBeVisible');

export { deletePreviousVisibleWorkspaces, getUserToVisibleWorkspaceRecords, getUserVisibleWorkspaces, getVisibleWorkspaces, setVisibleWorkspaces };
