'use strict';
import { dbInsert } from '../../db/db-connection.js';
import { eq, and } from 'drizzle-orm';
import { getResourcesByField } from '../../meta/server/db/db.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { getNewUuid } from '../../../fd-toolbox/uuids.js';
import { getDbSchema } from '../../db/db-schema.js';
import { checkValidPassword } from './auth.js';
import { workspaceConstants } from '../../../fd-toolbox/server/constants/workspace-constants.js';
import { getQuery } from '../../meta/server/web/drizzle.js';
import { getTableColumn, applyFilterToQuery, runQuery } from '../../db/drizzle-queries.js';
import { getOAuthProviderByAuthKey } from '../auth/oauth-service-db.js';
import { singleOrUndefined } from '../../../fd-toolbox/server/collections/single-funcs.js';
import { getUserById } from '../../fd-app-toolbox/server/users/user-db.js';
import { createResource } from '../../meta/server/resources/crud-resources.js';
import { schemaNames, columnNames } from '../../db/constants/schema-names.js';
import { removePermissionsFromCoreCache } from '../../meta/server/permission/permissions.js';

var __defProp=Object['defineProperty'],__name=(_0x42112b,_0x2ee79b)=>__defProp(_0x42112b,'name',{'value':_0x2ee79b,'configurable':!![]});async function getUserByEmail(_0x19763f){const _0x408431=getDbSchema(),_0xdf2138=_0x408431[schemaNames['userSchema']],_0x3ed46d=await getResourcesByField(resourceNames['user'],getTableColumn(_0xdf2138,columnNames['email']),_0x19763f);return singleOrUndefined(_0x3ed46d);}__name(getUserByEmail,'getUserByEmail');async function getUserByEmailAndPassword(_0x2cca59,_0xb60948){const _0x23b02f=await getUserByEmail(_0x2cca59);if(_0x23b02f){const _0x2c8dd8=await checkValidPassword(_0xb60948,_0x23b02f['passwordHash']??'');if(_0x2c8dd8)return _0x23b02f;}return void(0x2*-0xf38+0x5*-0x6a3+0x3f9f);}__name(getUserByEmailAndPassword,'getUserByEmailAndPassword');async function getUserByOAuth(_0x5e25e7,_0x4cbcba){const _0x3357a7=await getOAuthProviderByAuthKey(_0x4cbcba,_0x5e25e7);if(!_0x3357a7)return void(0x62b*-0x4+0x5*0x5d1+-0x469);const _0x1e9f40=await getUserById(_0x3357a7['userId']);return _0x1e9f40;}__name(getUserByOAuth,'getUserByOAuth');async function createUserResource(_0x10495a){return createResource(_0x10495a,resourceNames['user']);}__name(createUserResource,'createUserResource');async function addRoleToUser(_0x5a6903,_0x2fdd6f){const _0x4f68ff=getDbSchema(),_0x4c1e81=_0x4f68ff[schemaNames['userRoleSchema']],_0x388f3f=await dbInsert(_0x4c1e81)['values']({'id':getNewUuid(),'userId':_0x5a6903,'roleId':_0x2fdd6f})['onConflictDoNothing']()['returning']();return await removePermissionsFromCoreCache(),_0x388f3f;}__name(addRoleToUser,'addRoleToUser');async function getUserHomeWorkspaceId(_0x54bf6f){const _0x2547ef=await getUserById(_0x54bf6f);if(!_0x2547ef['homeWorkspaceId'])return workspaceConstants['defaultWorkspaceId'];return _0x2547ef['homeWorkspaceId'];}__name(getUserHomeWorkspaceId,'getUserHomeWorkspaceId');async function getUserDtosOrSingle(_0x5e599e){const _0x5e8403=getDbSchema(),_0x44e605=_0x5e8403[schemaNames['userSchema']],_0x3b91cd=[eq(getTableColumn(_0x44e605,columnNames['isDisabled']),![])];if(_0x5e599e)_0x3b91cd['push'](eq(getTableColumn(_0x44e605,columnNames['id']),_0x5e599e));const {query:_0x4f0629}=await getQuery(_0x44e605);applyFilterToQuery(_0x4f0629,and(..._0x3b91cd));const _0x12bb95=await runQuery(_0x4f0629),_0x55eeb9=_0x12bb95['map'](_0xbb51d0=>({'id':_0xbb51d0['id'],'email':_0xbb51d0['email']??void(-0x3*0x147+-0x1*0xf6d+-0x9a1*-0x2)}));return _0x55eeb9;}__name(getUserDtosOrSingle,'getUserDtosOrSingle');

export { addRoleToUser, createUserResource, getUserByEmail, getUserByEmailAndPassword, getUserByOAuth, getUserDtosOrSingle, getUserHomeWorkspaceId };
