'use strict';

var ServiceSDK = module.require('bi-service-sdk').BIServiceSDK;

module.exports = {{{moduleName}}};


/**
 * @constructor
 * @name {{{moduleName}}}
 */
function {{{moduleName}}}(options) {
    options = options || {};

    if (!options.baseURL) {
        {{#host}}
        options.baseURL = "{{{host}}}";
        {{/host}}
        {{^host}}
        throw new Error('`baseURL` string option is required');
        {{/host}}
    }

    this.version = "{{{version}}}";
    options.baseURL += "{{{basePath}}}";

    //options object must be ready before we call the parent constructor
    ServiceSDK.call(this, options);
}

{{{moduleName}}}.prototype = Object.create(ServiceSDK.prototype);
{{{moduleName}}}.prototype.constructor = {{{moduleName}}};

{{#paths}}
/**
 * @method
 * @name {{{moduleName}}}#{{{sdkMethodName}}}
 * @operationId {{{operationId}}}
 * @summary {{{summary}}}
{{#routeDesc}}
 *
 * @description {{{routeDesc}}}
 {{/routeDesc}}
 *
{{#pathParams}}
 * @param {{openbrace}}{{schema.type}}{{closebrace}} [{{name}}]{{#description}} - {{{description}}}{{/description}}
{{/pathParams}}
 * @param {Object} options
 * @param {Object} options.data - request body payload in case of PUT|POST|DELETE, query parameters otherwise
 * @param {Object} [options.query]
 {{#queryParams}}
 * @param {{openbrace}}{{schema.type}}{{closebrace}} [options.params.{{name}}]{{#description}} - {{{description}}}{{/description}}
 {{/queryParams}}
 * @param {Object} [options.headers]
 {{#headerParams}}
 * @param {{openbrace}}{{schema.type}}{{closebrace}} [options.headers.{{name}}]{{#description}} - {{{description}}}{{/description}}
 {{/headerParams}}
 * @param {Object} options.path
 {{#pathParams}}
 * @param {{openbrace}}{{schema.type}}{{closebrace}} [options.path.{{name}}]{{#description}} - {{{description}}}{{/description}}
 {{/pathParams}}
 * @return {Promise<Object>}
 */
{{{moduleName}}}.prototype.{{{sdkMethodName}}} = function {{{sdkMethodName}}}({{methodPathArgs}}options) {

    {{#pathParams}}
    if (typeof {{name}} === 'object' && {{name}} !== null && typeof options === 'undefined') {
        options = {{name}};
        {{name}} = undefined;
    }

    if (typeof {{name}} === 'undefined') {
        {{name}} = options && options.path && options.path['{{name}}'];
    }
    {{/pathParams}}

    var opt = {
        url     : "{{{url}}}",
        method  : "{{{method}}}",
        data    : (options && options.data) !== undefined ? options.data : {},
        params  : (options && options.query) !== undefined ? options.query : {},
        headers : (options && options.headers) !== undefined ? options.headers : {}
    };

    {{#pathParams}}
    opt.url = opt.url.replace(/{{openbrace}}{{name}}{{closebrace}}/, {{name}});
    {{/pathParams}}

    return this.$request(opt);
};
{{/paths}}
