'use strict';
import { getUserId } from '../../meta/server/auth/users/current-user-id-provider.js';
import { getChildWorkspaces } from '../../fd-app-toolbox/server/infra/workspaces/workspace-db.js';
import { getSessionToken, getSession } from '../../meta/server/auth/session-access.js';
import { updateSessionData } from '../../meta/server/auth/session.js';
import { getCrudUpdateConfig } from '../../meta/server/meta-core/crud/crud-update-config.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { crudUpdate } from '../../meta/server/basic-crud/cruds.js';
import { setCurrentWorkspaceId } from '../../meta/server/providers/current-workspace-id-provider.js';

var __defProp=Object['defineProperty'],__name=(_0x32e000,_0x47b471)=>__defProp(_0x32e000,'name',{'value':_0x47b471,'configurable':!![]});async function changeCurrentWorkspace(_0x2b0e77){const _0x3cc5d5=await getUserId(),_0x388ac6=await getSessionToken();let _0x31b69b;if(!_0x388ac6)return void(-0x2*0x130+-0xe9e*0x1+0x10fe);const _0x3f6deb=await getSession(_0x388ac6);if(!_0x3f6deb?.['homeWorkspaceId']||!_0x3f6deb['currentWorkspaceId'])return void(0x1d05*0x1+0x1e9+-0x1eee);if(await isValidWorkspaceChange(_0x2b0e77,_0x3f6deb['currentWorkspaceId'],_0x3f6deb['homeWorkspaceId'])){const _0xa1f9fc=await getCrudUpdateConfig({'id':_0x3cc5d5,'currentWorkspaceId':_0x2b0e77,'userId':_0x3cc5d5},resourceNames['user'],_0x3cc5d5),_0x2d5a6d=await crudUpdate(_0xa1f9fc);_0x2d5a6d['validationResult']['isValid']&&(setCurrentWorkspaceId(_0x2b0e77),_0x31b69b=await updateSessionData(_0x388ac6,{'currentWorkspaceId':_0x2b0e77}));}return _0x31b69b;}__name(changeCurrentWorkspace,'changeCurrentWorkspace');async function isValidWorkspaceChange(_0x29f013,_0x588b75,_0x150d8e){if(_0x588b75===_0x29f013)return ![];if(_0x150d8e===_0x29f013)return !![];const _0x24ea6b=await getChildWorkspaces(_0x150d8e);return _0x24ea6b['some'](_0x42b9ea=>_0x42b9ea['id']===_0x29f013);}__name(isValidWorkspaceChange,'isValidWorkspaceChange');

export { changeCurrentWorkspace };
