import { NgModule } from '@angular/core';
import { RPCModule } from '@loafer/ng-rpc/ng-rpc.module';

import {
  RPC_CODEC,
  RPC_RWC,
} from '@loafer/ng-rpc/core';
import { JSONRPCClientCodec } from '@loafer/ng-rpc/protocol/json';
import {
  RPCClientWebsocketRWC,
  RxWebsocketSubjectConfig,
} from '@loafer/ng-rpc/client/rwc/websocket';

import { environment } from '../environments/environment';

export function getRPCClientCodec() {
  return new JSONRPCClientCodec();
}

export function getRPCClientWebsocketRWC(config: RxWebsocketSubjectConfig) {
  return new RPCClientWebsocketRWC(config);
}

@NgModule({
  exports: [
    RPCModule,
  ],
  imports: [
    RPCModule.forRoot({}),
  ],
  providers: [
    {provide: 'WEBAPP_RPC_CONFIG', useValue: environment.webappRPCConfig},

    {provide: RPC_CODEC, useFactory: getRPCClientCodec},
    {
      provide: RPC_RWC,
      useFactory: getRPCClientWebsocketRWC,
      deps: ['WEBAPP_RPC_CONFIG']
    },

  ]
})
export class AppRPCModule { }
