(function(root, factory) {

if (typeof module === 'object' && module.exports) {
    // CommonJS-like environments that support module.exports, like Node.
    module.exports = factory(require('logtown').getLogger('/src/handlers/UpdateCustomerHandler'), require('../ApiClient'), require('../api/CustomerApi'), require('../OrbipayApiError'), require('../CommonUtil'));
  } 
}(this, function(logger, ApiClient, CustomerApi, OrbipayApiError, CommonUtil) {
  'use strict';

  /**
   * @module ApiClient
   */

  /**
   * <h3 style="color:red"> This class subject to change without prior notice, Please dont use this class directly. </h3>

   * @module handlers/UpdateCustomerHandler
   * @class
   */

  var exports = function() {
  };

  exports.prototype.process = function (customer, callback) {
        
        var secret = customer.invocation_context.secret;
		var host = customer.api_end_point;
        var api_key = customer.invocation_context.api_key;
        var auth_scheme = customer.invocation_context.auth_scheme;
        var _this=this;
		var response = {};
        var exception;
        try {
            var api_client = new ApiClient(secret, host,api_key,auth_scheme);
			var api_instance = new CustomerApi(api_client);
			var channel = customer.invocation_context['channel'];
			var client_key = customer.invocation_context['client_key'];
			var product = customer.invocation_context['product'];
			var timestamp;
			if (customer.invocation_context['timestamp'])
			{
				timestamp = customer.invocation_context['timestamp'];
			}		
			else
			{
				timestamp = CommonUtil.getTimeStamp();
			}
			var idempotent_request_key = customer.invocation_context['idempotent_request_key'];
			var requestor_type = customer.invocation_context['requestor_type'];
			var id_customer = customer['ID_CUSTOMER'];
			var update_customer_request = _this.populate_body(customer);
            var opts = _this.getOptions(customer);
			var result = api_instance.updateCustomer(channel, client_key, product, timestamp, idempotent_request_key, requestor_type, id_customer, update_customer_request,  opts, callback);
			response['request'] = result;
			return response;
        } catch (e) {
            exception = OrbipayApiError.getDefaultException(e);
            if (callback) {
                callback(null, null, null, exception);
            }
            response['exception'] = exception;
            return response;
        }
    };

    exports.prototype.getOptions = function (customer) {
        return {
		    'requestor': customer['invocation_context']['requestor'], 
		    'x_opay_headers': customer['invocation_context']['X-OPAY-Headers']
        };
    };  

    exports.prototype.populate_body = function(customer){
		var UpdateCustomerRequest = require('../models/UpdateCustomerRequest');
		if(customer['customer_accounts'] && customer.customer_accounts.list ) {
		       customer['customer_accounts']=customer.customer_accounts.list
		}
		return  UpdateCustomerRequest.constructFromObject(customer);
	};


  return exports;
}));