import { Global, Module } from '@nestjs/common';
import { createQueueAdapter, type QueueAdapter } from '@mosaicstack/queue';
import type { MosaicConfig } from '@mosaicstack/config';
import { MOSAIC_CONFIG } from '../config/config.module.js';
import { QueueService } from './queue.service.js';

export const QUEUE_ADAPTER = 'QUEUE_ADAPTER';

@Global()
@Module({
  providers: [
    QueueService,
    {
      provide: QUEUE_ADAPTER,
      useFactory: (config: MosaicConfig): QueueAdapter => createQueueAdapter(config.queue),
      inject: [MOSAIC_CONFIG],
    },
  ],
  exports: [QueueService, QUEUE_ADAPTER],
})
export class QueueModule {}
