import { sendUnaryData as GrpcSendUnaryData } from 'grpc';
import { RpcApplication, WrappedHandler, GrpcServerCall } from 'sasdn';

{{#each infos}}
import {
  {{#each this.services}}
  {{@key}}Service,
  {{/each}}
} from '{{{this.protoServiceImportPath}}}';
{{#each this.protoMessageImportPath}}
import {
  {{#each this}}
  {{this}},
  {{/each}}
} from '{{{@key}}}';
{{/each}}
{{/each}}
{{#each infos}}

  {{#each this.services}}

    {{#each this}}
import {{curlyLeft}} {{{this.methodName}}}Handler {{curlyRight}} from './{{#eq ../../this.protoFile.relativePath '.'}}{{else}}{{../../this.protoFile.relativePath}}/{{/eq}}{{../../this.protoFile.svcNamespace}}/{{@../key}}/{{{this.methodName}}}';
    {{/each}}
  {{/each}}
{{/each}}

export const registerServices = function (app: RpcApplication) {
{{#each infos}}
  {{#each this.services}}

  app.server.addService({{@key}}Service, {
    {{#each this}}
    {{{this.methodName}}}: async (call: GrpcServerCall<{{{this.requestTypeStr}}}, {{{this.responseTypeStr}}}>{{#if this.hasCallback}}, callback: GrpcSendUnaryData<{{{this.responseTypeStr}}}>{{/if}}) => {
      let wrappedHandler: WrappedHandler = app.wrapGrpcHandler({{{this.methodName}}}Handler);
      wrappedHandler(call{{#if this.hasCallback}}, callback{{/if}}).then(_ => _);
    },
    {{/each}}
  });
  {{/each}}
{{/each}}

};