import { config } from '@things-factory/env'
import { getHeadlessPool as getHeadlessPoolForProcess } from './controllers/headless-pool-for-process'
import { internalProcessViewRouter } from './routers/internal-process-view-router'
import { standaloneProcessServiceRouter } from './routers/standalone-process-service-router'

const isPathBaseDomain = !config.get('subdomain') && !config.get('useVirtualHostBasedDomain')

process.on('bootstrap-module-global-public-route' as any, (app, domainGlobalPublicRouter) => {
  getHeadlessPoolForProcess()
  domainGlobalPublicRouter.use('', internalProcessViewRouter.routes(), internalProcessViewRouter.allowedMethods())
})

process.on('bootstrap-module-domain-private-route' as any, (app, domainPrivateRouter) => {
  if (isPathBaseDomain) {
    domainPrivateRouter.use(
      '/domain/:domain',
      standaloneProcessServiceRouter.routes(),
      standaloneProcessServiceRouter.allowedMethods()
    )
  } else {
    domainPrivateRouter.use(
      '',
      standaloneProcessServiceRouter.routes(),
      standaloneProcessServiceRouter.allowedMethods()
    )
  }
})
