// @flow

{{#isClassy}}
import ApiClient from '../ApiClient';

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

{{/isClassy}}

export default class {{{baseName}}}Requests {
    constructor(bridge) {
     this._bridge = bridge;
    }

    {{#operations}}
    {{#operation}}
    {{^isEvent}}
        /**
         * Registers a handler for a particular api.  This allows javascript to handle a request from native.
         * @param 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): Promise<any> {
        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 integer 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}}
            {{#returnType}}
            .then(ApiClient.deserializer({{returnType}}));
            {{/returnType}}
        {{/isClassy}}
        {{/hasParams}}
        {{/hasSingleParam}}

    }
    {{/isEvent}}
    {{/operation}}
    {{/operations}}
}
