// require files in Node.js environment
var ${{#imports}}, {{import}}{{/imports}};
if (typeof module === 'object' && module.exports) {
  $ = require('jquery');{{#imports}}
  {{import}} = require('../model/{{import}}.js');{{/imports}}
}

// export module for AMD
if ( typeof define === "function" && define.amd ) {     
	define(['jquery'{{#imports}}, '{{import}}'{{/imports}}], function(${{#imports}}, {{import}}{{/imports}}) {
        return {{classname}};
	 });
}

var {{classname}} = function {{classname}}() {
	var self = this;
  {{#operations}}
  {{#operation}}
  /**
   * {{summary}}
   * {{notes}}
{{#allParams}}   * @param {{=<% %>=}}{<% dataType %>} <%={{ }}=%> {{paramName}} {{description}}
{{/allParams}}   * @param {function} callback the callback function
   * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
   */
  self.{{nickname}} = function({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}callback) {
    var {{localVariablePrefix}}postBody = {{#bodyParam}}{{^isBinary}}JSON.stringify({{paramName}}){{/isBinary}}{{#isBinary}}null{{/isBinary}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
    var {{localVariablePrefix}}postBinaryBody = {{#bodyParam}}{{#isBinary}}{{paramName}}{{/isBinary}}{{^isBinary}}null{{/isBinary}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
    {{#allParams}}{{#required}}
     // verify the required parameter '{{paramName}}' is set
     if ({{paramName}} == null) {
        //throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}");
        var errorRequiredMsg = "Missing the required parameter '{{paramName}}' when calling {{nickname}}";
        throw errorRequiredMsg;
     }
     {{/required}}{{/allParams}}
    // create path and map variables
    var basePath = '{{basePath}}';
    // if basePath ends with a /, remove it as path starts with a leading /
    if (basePath.substring(basePath.length-1, basePath.length)=='/') {
    	basePath = basePath.substring(0, basePath.length-1);
    }
    
    var {{localVariablePrefix}}path = basePath + replaceAll(replaceAll("{{{path}}}", "\\{format\\}","json"){{#pathParams}}
, "\\{" + "{{baseName}}" + "\\}", encodeURIComponent({{{paramName}}}.toString()){{/pathParams}});

    var queryParams = {};
    var headerParams =  {};
    var formParams =  {};

    {{#queryParams}}
    queryParams.{{baseName}} = {{paramName}};
    {{/queryParams}}
    {{#headerParams}}if ({{paramName}} != null)
    {{localVariablePrefix}}headerParams.put("{{baseName}}", {{paramName}});
    {{/headerParams}}
    {{#formParams}}if ({{paramName}} != null)
      {{localVariablePrefix}}formParams.put("{{baseName}}", {{paramName}});
    {{/formParams}}

    path += createQueryString(queryParams);

    var options = {type: "{{httpMethod}}", async: true, contentType: "application/json", dataType: "json", data: postBody};
    var request = $.ajax(path, options);

    request.fail(function(jqXHR, textStatus, errorThrown){
      if (callback) {
        var error = errorThrown || textStatus || jqXHR.statusText || 'error';
        callback(null, textStatus, jqXHR, error);
      }
    });
		
    request.done(function(response, textStatus, jqXHR){
      {{#returnType}}
      /**
        * @returns {{{returnType}}}
        */
      {{#returnTypeIsPrimitive}}var myResponse = response;{{/returnTypeIsPrimitive}}
      {{^returnTypeIsPrimitive}}var myResponse = new {{{returnType}}}();
      myResponse.constructFromObject(response);{{/returnTypeIsPrimitive}}
      if (callback) {
        callback(myResponse, textStatus, jqXHR);
      }
      {{/returnType}}{{^returnType}}
      if (callback) {
        callback(response, textStatus, jqXHR);
      }
      {{/returnType}}
    });
 
    return request;
  }
  {{/operation}}
  {{/operations}}

 	function replaceAll (haystack, needle, replace) {
		var result= haystack;
		if (needle !=null && replace!=null) {
			result= haystack.replace(new RegExp(needle, 'g'), replace);
		}
		return result;
	}

 	function createQueryString (queryParams) {
		var queryString ='';
		var i = 0;
		for (var queryParamName in queryParams) {
			if (i==0) {
				queryString += '?' ;
			} else {
				queryString += '&' ;
			}
			
			queryString +=  queryParamName + '=' + encodeURIComponent(queryParams[queryParamName]);
			i++;
		}
		
		return queryString;
	}
}

// export module for Node.js
if (typeof module === 'object' && module.exports) {
  module.exports = {{classname}};
}
