'use strict';
import { Subquery, aliasedTable, or, isNull, inArray, eq, and } from 'drizzle-orm';
import { getTableByName, runQuery, getColumnsFromTable, getTableColumn } from '../../../db/drizzle-queries.js';
import { createDynamicSelect, dbWhere, dbInnerJoin } from '../../../db/db-connection.js';
import { isTableWithWorkspaceId } from './drizzle-guards.js';
import { getCurrentWorkspaceIdFromCache } from '../providers/current-workspace-id-provider.js';
import { getInternalResourceMeta } from '../resources/internal-resource-meta.js';
import { drizzleNames } from '../db/drizzle-names.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { withWorkspaceIdProps } from '../../../../fd-toolbox-core/server/base-resources/props/with-workspace-id-props.js';
import { getUserIdFromCache } from '../auth/users/current-user-id-provider.js';
import { userVisibleWorkspaceProps } from '../../core/resources/users/props/user-visible-workspace-props.js';
import { tryGetFromCoreCache } from '../../../../fd-toolbox/server/cachings/core-cache.js';
import { getVisibleWorkspacesCacheKey } from '../workspace/get-visible-workspaces-cache-key.js';
import { convertFromPascalToCamelCase } from '../../../../fd-toolbox/strings/casing-converter.js';
import { metas } from '../meta-core/constants/metas.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { withIdProps } from '../../../../fd-toolbox-core/types/resource-with-id.js';
import { metaQueryParams } from '../../../../fd-toolbox/constants/meta-query-params.js';
import { isAnyColumn } from '../../../db/guards/db-type-guards.js';

var __defProp=Object['defineProperty'],__name=(_0x58aa16,_0x1cb534)=>__defProp(_0x58aa16,'name',{'value':_0x1cb534,'configurable':!![]});const subqueryAliasErrorMessage='Cannot\x20alias\x20a\x20subquery';function getAliasedTableByTableName(_0x45d0bf,_0x40274d){const _0x522dbe=getTableByName(_0x45d0bf);if(_0x522dbe instanceof Subquery)throw createError(subqueryAliasErrorMessage);return aliasedTable(_0x522dbe,_0x40274d);}__name(getAliasedTableByTableName,'getAliasedTableByTableName');async function getTableByResourceName(_0x533456){const _0x92444f=await getInternalResourceMeta(_0x533456);return getTableByName(convertFromPascalToCamelCase(_0x92444f['persistence']['tableName'])+'Schema');}__name(getTableByResourceName,'getTableByResourceName');async function getAliasedTableByResourceName(_0x2c7585,_0x4483ef){const _0x1764a0=await getTableByResourceName(_0x2c7585);if(_0x1764a0 instanceof Subquery)throw createError(subqueryAliasErrorMessage);return aliasedTable(_0x1764a0,_0x4483ef);}__name(getAliasedTableByResourceName,'getAliasedTableByResourceName');async function getTableNameByResourceName(_0x429794){const _0x67497f=await getInternalResourceMeta(_0x429794);return convertFromPascalToCamelCase(_0x67497f['persistence']['tableName'])+drizzleNames['schemaSuffix'];}__name(getTableNameByResourceName,'getTableNameByResourceName');async function getQuery(_0x6c6a64,_0x437c5a,_0xeb13db){const _0x58012b=createDynamicSelect(_0x6c6a64,_0x437c5a),_0x542448=await applyWorkspaceFilterIfNeeded(_0x6c6a64,_0x58012b,_0xeb13db);return _0x542448;}__name(getQuery,'getQuery');async function executeQuery(_0x44ff01,_0x275298){const _0xd3a9a9=createDynamicSelect(_0x44ff01,_0x275298),_0x62952b=await applyWorkspaceFilterIfNeeded(_0x44ff01,_0xd3a9a9);return runQuery(_0x62952b['query']);}__name(executeQuery,'executeQuery');function getForeignColumn(_0x4c0d0a,_0xc4f8b2){const _0x368f76=getForeignColumnName(_0xc4f8b2);return getColumnsFromTable(_0x4c0d0a)[_0x368f76];}__name(getForeignColumn,'getForeignColumn');function getForeignColumnName(_0x31fac4){return convertFromPascalToCamelCase(_0x31fac4)+metas['idSuffix'];}__name(getForeignColumnName,'getForeignColumnName');async function addVisibleWorkspacesFilter(_0x50bdd2,_0xf5a2e7){const _0x32c509=await getCurrentWorkspaceIdFromCache(),_0x26df2d=await getUserIdFromCache(),_0x2abe63=getVisibleWorkspacesCacheKey(_0x26df2d,_0x32c509),_0x4c82ed=await tryGetFromCoreCache(_0x2abe63);if(_0x4c82ed){const _0x16ec8b=or(isNull(_0xf5a2e7),inArray(_0xf5a2e7,_0x4c82ed['concat'](_0x32c509)));_0x16ec8b&&dbWhere(_0x50bdd2['query'],_0x16ec8b);const _0x419417={'query':_0x50bdd2['query']};return _0x419417;}const _0x4f9cc6=await getTableByResourceName(resourceNames['userVisibleWorkspace']),_0x29bacd=await getTableByResourceName(resourceNames['workspace']),_0x2ca973=getTableColumn(_0x4f9cc6,userVisibleWorkspaceProps['visibleWorkspaceId']),_0x30efde=createDynamicSelect(_0x4f9cc6,{'id':_0x2ca973}),_0x2270ae=getTableColumn(_0x29bacd,withIdProps['id']);dbInnerJoin(_0x30efde,_0x29bacd,eq(_0x2ca973,_0x2270ae));const _0x23f19d=getTableColumn(_0x4f9cc6,metaQueryParams['userId']),_0x107bc4=getTableColumn(_0x29bacd,'parentWorkspaceId'),_0x2e7ed9=_0x32c509?eq(_0x107bc4,_0x32c509):isNull(_0x107bc4),_0x46c0c6=and(eq(_0x23f19d,_0x26df2d),_0x2e7ed9);_0x46c0c6&&dbWhere(_0x30efde,_0x46c0c6);const _0x21fc58=_0x30efde['as'](userVisibleWorkspaceProps['visibleWorkspace']+drizzleNames['subquerySuffix']),_0x2efc72=createDynamicSelect(_0x21fc58,{'id':getSubqueryColumn(_0x21fc58,withIdProps['id'])}),_0x2e1367=or(isNull(_0xf5a2e7),inArray(_0xf5a2e7,_0x2efc72),eq(_0xf5a2e7,_0x32c509));_0x2e1367&&dbWhere(_0x50bdd2['query'],_0x2e1367);const _0x4584ed={'query':_0x50bdd2['query']};return _0x4584ed;}__name(addVisibleWorkspacesFilter,'addVisibleWorkspacesFilter');async function applyWorkspaceFilterIfNeeded(_0x430427,_0x8a52c6,_0x4d7b68){const _0x36cbd7=Object['hasOwn'](_0x430427,withWorkspaceIdProps['workspaceId']);let _0x19a410={'query':_0x8a52c6};return !_0x4d7b68&&_0x36cbd7&&isTableWithWorkspaceId(_0x430427)&&(_0x19a410=await addVisibleWorkspacesFilter(_0x19a410,_0x430427['workspaceId'])),_0x19a410;}__name(applyWorkspaceFilterIfNeeded,'applyWorkspaceFilterIfNeeded');function getSubqueryColumn(_0x553c79,_0x1eb2dc){const _0x25604e=_0x553c79['_']['selectedFields'][_0x1eb2dc];if(!isAnyColumn(_0x25604e))throw createError('Column\x20\x22'+_0x1eb2dc+'\x22\x20was\x20not\x20selected\x20in\x20the\x20subquery');return _0x25604e;}__name(getSubqueryColumn,'getSubqueryColumn');

export { executeQuery, getAliasedTableByResourceName, getAliasedTableByTableName, getForeignColumn, getForeignColumnName, getQuery, getTableByResourceName, getTableNameByResourceName };
