// @flow
{{#isClassy}}

import ApiClient from '../ApiClient';

{{#imports}}import {{import}} from '../{{#modelPackage}}{{&modelPackage}}/{{/modelPackage}}{{import}}';
{{/imports}}
{{/isClassy}}

export default class {{{baseName}}}Requests {
  _bridge: Object;

  constructor(bridge: Object) {
    this._bridge = bridge;
  }
{{#operations}}
{{#operation}}
{{^isEvent}}

  /**
   * Registers a handler for a particular api. This allows JavaScript to handle a request from native.
   * @param handler The handler function, taking a single parameter being the data of the request and returning a Promise. Implementer of the handler should either resolve the promise with an object being the response data (if any) or reject the promise with an Error
   */
  register{{{camelizedNickName}}}RequestHandler(handler: Function) {
    this._bridge.registerRequestHandler(
      '{{package}}.request.{{operationId}}',
      handler,
    );
  }
{{/isEvent}}
{{/operation}}
{{/operations}}
{{#operations}}
{{#operation}}
{{^isEvent}}
{{#emitJSDoc}}

  /**{{#summary}}
   * {{&summary}}{{/summary}}{{#notes}}
   * {{&notes}}{{/notes}}{{#allParams}}{{#required}}
   * @param {{{&_jsDocType}}} {{paramName}} {{&description}}{{/required}}{{/allParams}}{{#hasOptionalParams}}
   * @param {Object} opts Optional parameters{{#allParams}}{{^required}}
   * @param {{{&_jsDocType}}}opts.{{paramName}} {{&description}}{{#defaultValue}} (default to {{.}}){{/defaultValue}}{{/required}}{{/allParams}}{{/hasOptionalParams}}
   * @param timeout timeout in milliseconds
   * @return {Promise} a {@link https://www.promisejs.org/|Promise}{{#returnType}}, with data of type {@link {{&_jsDocType}} }{{/returnType}}
   */
{{/emitJSDoc}}
  {{nickname}}({{_argList}}{{#_argList}}, {{/_argList}}timeout: number): Promise<any> {
{{^hasParams}}
    return this._bridge.sendRequest('{{package}}.request.{{operationId}}', {
      timeout,
    });
{{/hasParams}}
{{#hasSingleParam}}
{{#allParams}}
    return this._bridge.sendRequest('{{package}}.request.{{operationId}}', {
      data{{#required}}: {{paramName}}{{/required}}{{^required}}: opts{{/required}},
      timeout,
    });
{{/allParams}}
{{/hasSingleParam}}
{{^hasSingleParam}}
{{#hasParams}}
{{>_methodBody}}
    return this._bridge.sendRequest('{{package}}.request.{{operationId}}', {
      data,
      timeout,
    }){{^isClassy}};{{/isClassy}}
{{#isClassy}}
{{#returnType}}
    .then(ApiClient.deserializer({{returnType}}));
{{/returnType}}
{{/isClassy}}
{{/hasParams}}
{{/hasSingleParam}}
  }
{{/isEvent}}
{{/operation}}
{{/operations}}
}
