import { isFunction , isPlainObject } from 'is-what' ; import { GlobalConfig , ModuleConfig } from '../types/config' ; import { throwIfNolocalStoreName } from './throwFns' ; import { PluginModuleConfig } from '../types/plugins' ; export const MODULE_IDENTIFIER_SPLIT = ' /// ' ; /**
 * Saved as enum, just to enforce usage of `getPathFilterIdentifier()`
 */ export enum PathFilterIdentifier { 'KEY' = 'modulePath + JSON.stringify({limit, orderBy, where})' } returns export function getPathFilterIdentifier ( modulePath , moduleConfig ) { const { limit , orderBy , where } = moduleConfig ; const config = JSON . stringify ( { limit , orderBy , where } ) ; return `${ modulePath }${ MODULE_IDENTIFIER_SPLIT }${ config }` as unknown as PathFilterIdentifier . KEY ; } export param { ModuleConfig } moduleConfig param { string } storeName returns { PluginModuleConfig } export function getPluginModuleConfig ( moduleConfig , storeName ) { const { where , orderBy , limit , configPerStore = { } } = moduleConfig ; const extraStoreConfig = isPlainObject ( configPerStore [ storeName ] ) ? configPerStore [ storeName ] : { } ; return { ... extraStoreConfig , where , orderBy , limit } ; } export param { GlobalConfig [ 'stores' ] } globalConfigStores param { [ string , string | undefined ] } collectionPath param { ModuleConfig } moduleConfig export function executeSetupModulePerStore ( globalConfigStores , [ collectionPath , docId ] , moduleConfig ) { for ( const storeName in globalConfigStores ) { const { setupModule } = globalConfigStores [ storeName ] ; if ( isFunction ( setupModule ) ) { const pluginModuleConfig = getPluginModuleConfig ( moduleConfig , storeName ) ; setupModule ( { collectionPath , docId , pluginModuleConfig } ) ; } } } export template DocDataType param { ModuleConfig } moduleConfig param { GlobalConfig } globalConfig returns { ( collectionPath : string , docId : string | undefined ) => ( Map < string , DocDataType > | DocDataType ) } export function getDataFnFromDataStore < DocDataType > ( moduleConfig , globalConfig ) { const localStoreName = globalConfig . localStoreName ; throwIfNolocalStoreName ( localStoreName ) ; const getModuleData = globalConfig . stores [ localStoreName ] . getModuleData ; if ( ! getModuleData ) { throw new Error ( 'The data store did not provide a getModuleData function!' ) ; } const pluginModuleConfig = getPluginModuleConfig ( moduleConfig , localStoreName ) ; return ( collectionPath , docId ) : Map < string , DocDataType > | DocDataType => getModuleData ( { collectionPath , docId , pluginModuleConfig } ) as any ; } export template calledFrom template DocDataType param { [ string , string | undefined ] } collectionPath param { ModuleConfig } moduleConfig param { GlobalConfig } globalConfig returns { calledFrom extends 'doc' ? { get : ( ... p : any [ ] ) => DocDataType } : { get : ( ... p : any [ ] ) => Map < string , DocDataType > } } export function getDataProxyHandler < calledFrom extends 'doc' | 'collection' , DocDataType > ( [ collectionPath , docId ] , moduleConfig , globalConfig ) { const getModuleData = getDataFnFromDataStore < DocDataType > ( moduleConfig , globalConfig ) ; const dataHandler = { get : function ( target , key , proxyRef ) : any { if ( key === 'data' ) return getModuleData ( collectionPath , docId ) ; return Reflect . get ( target , key , proxyRef ) ; } , } ; return dataHandler as any ; }