import type * as mediasoup from 'mediasoup'
import fp from 'fastify-plugin'

declare module 'fastify' {
  interface FastifyInstance {
    mediasoupRouter: mediasoup.types.Router
  }
}

export default fp<mediasoup.types.RouterOptions>(
  async (fastify, opts) => {
    const router = await fastify.mediasoupWorker.createRouter(opts)

    fastify.decorate('mediasoupRouter', router)
  },
  { name: 'mediasoup-router', dependencies: ['mediasoup-worker'] },
)

export const autoConfig: mediasoup.types.RouterOptions = {
  mediaCodecs: [
    {
      kind: 'audio',
      mimeType: 'audio/opus',
      clockRate: 48000,
      channels: 2,
      parameters: { useinbandfec: 1, stereo: 1 },
    },
  ],
}
