/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 * Changes may cause incorrect behavior and will be lost if the code is
 * regenerated.
 */

'use strict';

const msRest = require('ms-rest');
const msRestAzure = require('ms-rest-azure');
const WebResource = msRest.WebResource;

/**
 * Gets the list of the Notification Recipient Emails subscribed to a
 * notification.
 *
 * @param {string} resourceGroupName The name of the resource group.
 *
 * @param {string} serviceName The name of the API Management service.
 *
 * @param {string} notificationName Notification Name Identifier. Possible
 * values include: 'RequestPublisherNotificationMessage',
 * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
 * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
 * 'QuotaLimitApproachingPublisherNotificationMessage'
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The callback.
 *
 * @returns {function} callback(err, result, request, response)
 *
 *                      {Error}  err        - The Error object if an error occurred, null otherwise.
 *
 *                      {object} [result]   - The deserialized result object if an error did not occur.
 *                      See {@link RecipientEmailCollection} for more
 *                      information.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _listByNotification(resourceGroupName, serviceName, notificationName, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  // Validate
  try {
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName === null || serviceName === undefined || typeof serviceName.valueOf() !== 'string') {
      throw new Error('serviceName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName !== null && serviceName !== undefined) {
      if (serviceName.length > 50)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MaxLength": 50');
      }
      if (serviceName.length < 1)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MinLength": 1');
      }
      if (serviceName.match(/^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/) === null)
      {
        throw new Error('"serviceName" should satisfy the constraint - "Pattern": /^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/');
      }
    }
    if (notificationName === null || notificationName === undefined || typeof notificationName.valueOf() !== 'string') {
      throw new Error('notificationName cannot be null or undefined and it must be of type string.');
    }
    if (this.client.apiVersion === null || this.client.apiVersion === undefined || typeof this.client.apiVersion.valueOf() !== 'string') {
      throw new Error('this.client.apiVersion cannot be null or undefined and it must be of type string.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientEmails';
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{serviceName}', encodeURIComponent(serviceName));
  requestUrl = requestUrl.replace('{notificationName}', encodeURIComponent(notificationName));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'GET';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorResponse']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    // Deserialize Response
    if (statusCode === 200) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['RecipientEmailCollection']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError.request = msRest.stripRequest(httpRequest);
        deserializationError.response = msRest.stripResponse(response);
        return callback(deserializationError);
      }
    }

    return callback(null, result, httpRequest, response);
  });
}

/**
 * Determine if Notification Recipient Email subscribed to the notification.
 *
 * @param {string} resourceGroupName The name of the resource group.
 *
 * @param {string} serviceName The name of the API Management service.
 *
 * @param {string} notificationName Notification Name Identifier. Possible
 * values include: 'RequestPublisherNotificationMessage',
 * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
 * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
 * 'QuotaLimitApproachingPublisherNotificationMessage'
 *
 * @param {string} email Email identifier.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The callback.
 *
 * @returns {function} callback(err, result, request, response)
 *
 *                      {Error}  err        - The Error object if an error occurred, null otherwise.
 *
 *                      {boolean} [result]   - The deserialized result object if an error did not occur.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _checkEntityExists(resourceGroupName, serviceName, notificationName, email, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  // Validate
  try {
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName === null || serviceName === undefined || typeof serviceName.valueOf() !== 'string') {
      throw new Error('serviceName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName !== null && serviceName !== undefined) {
      if (serviceName.length > 50)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MaxLength": 50');
      }
      if (serviceName.length < 1)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MinLength": 1');
      }
      if (serviceName.match(/^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/) === null)
      {
        throw new Error('"serviceName" should satisfy the constraint - "Pattern": /^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/');
      }
    }
    if (notificationName === null || notificationName === undefined || typeof notificationName.valueOf() !== 'string') {
      throw new Error('notificationName cannot be null or undefined and it must be of type string.');
    }
    if (email === null || email === undefined || typeof email.valueOf() !== 'string') {
      throw new Error('email cannot be null or undefined and it must be of type string.');
    }
    if (this.client.apiVersion === null || this.client.apiVersion === undefined || typeof this.client.apiVersion.valueOf() !== 'string') {
      throw new Error('this.client.apiVersion cannot be null or undefined and it must be of type string.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientEmails/{email}';
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{serviceName}', encodeURIComponent(serviceName));
  requestUrl = requestUrl.replace('{notificationName}', encodeURIComponent(notificationName));
  requestUrl = requestUrl.replace('{email}', encodeURIComponent(email));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'HEAD';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 204 && statusCode !== 404) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorResponse']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    result = (statusCode === 204);

    return callback(null, result, httpRequest, response);
  });
}

/**
 * Adds the Email address to the list of Recipients for the Notification.
 *
 * @param {string} resourceGroupName The name of the resource group.
 *
 * @param {string} serviceName The name of the API Management service.
 *
 * @param {string} notificationName Notification Name Identifier. Possible
 * values include: 'RequestPublisherNotificationMessage',
 * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
 * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
 * 'QuotaLimitApproachingPublisherNotificationMessage'
 *
 * @param {string} email Email identifier.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The callback.
 *
 * @returns {function} callback(err, result, request, response)
 *
 *                      {Error}  err        - The Error object if an error occurred, null otherwise.
 *
 *                      {object} [result]   - The deserialized result object if an error did not occur.
 *                      See {@link RecipientEmailContract} for more
 *                      information.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _createOrUpdate(resourceGroupName, serviceName, notificationName, email, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  // Validate
  try {
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName === null || serviceName === undefined || typeof serviceName.valueOf() !== 'string') {
      throw new Error('serviceName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName !== null && serviceName !== undefined) {
      if (serviceName.length > 50)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MaxLength": 50');
      }
      if (serviceName.length < 1)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MinLength": 1');
      }
      if (serviceName.match(/^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/) === null)
      {
        throw new Error('"serviceName" should satisfy the constraint - "Pattern": /^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/');
      }
    }
    if (notificationName === null || notificationName === undefined || typeof notificationName.valueOf() !== 'string') {
      throw new Error('notificationName cannot be null or undefined and it must be of type string.');
    }
    if (email === null || email === undefined || typeof email.valueOf() !== 'string') {
      throw new Error('email cannot be null or undefined and it must be of type string.');
    }
    if (this.client.apiVersion === null || this.client.apiVersion === undefined || typeof this.client.apiVersion.valueOf() !== 'string') {
      throw new Error('this.client.apiVersion cannot be null or undefined and it must be of type string.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientEmails/{email}';
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{serviceName}', encodeURIComponent(serviceName));
  requestUrl = requestUrl.replace('{notificationName}', encodeURIComponent(notificationName));
  requestUrl = requestUrl.replace('{email}', encodeURIComponent(email));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'PUT';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200 && statusCode !== 201) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorResponse']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    // Deserialize Response
    if (statusCode === 200) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['RecipientEmailContract']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError.request = msRest.stripRequest(httpRequest);
        deserializationError.response = msRest.stripResponse(response);
        return callback(deserializationError);
      }
    }
    // Deserialize Response
    if (statusCode === 201) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['RecipientEmailContract']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError1 = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError1.request = msRest.stripRequest(httpRequest);
        deserializationError1.response = msRest.stripResponse(response);
        return callback(deserializationError1);
      }
    }

    return callback(null, result, httpRequest, response);
  });
}

/**
 * Removes the email from the list of Notification.
 *
 * @param {string} resourceGroupName The name of the resource group.
 *
 * @param {string} serviceName The name of the API Management service.
 *
 * @param {string} notificationName Notification Name Identifier. Possible
 * values include: 'RequestPublisherNotificationMessage',
 * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
 * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
 * 'QuotaLimitApproachingPublisherNotificationMessage'
 *
 * @param {string} email Email identifier.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The callback.
 *
 * @returns {function} callback(err, result, request, response)
 *
 *                      {Error}  err        - The Error object if an error occurred, null otherwise.
 *
 *                      {null} [result]   - The deserialized result object if an error did not occur.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _deleteMethod(resourceGroupName, serviceName, notificationName, email, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  // Validate
  try {
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName === null || serviceName === undefined || typeof serviceName.valueOf() !== 'string') {
      throw new Error('serviceName cannot be null or undefined and it must be of type string.');
    }
    if (serviceName !== null && serviceName !== undefined) {
      if (serviceName.length > 50)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MaxLength": 50');
      }
      if (serviceName.length < 1)
      {
        throw new Error('"serviceName" should satisfy the constraint - "MinLength": 1');
      }
      if (serviceName.match(/^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/) === null)
      {
        throw new Error('"serviceName" should satisfy the constraint - "Pattern": /^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/');
      }
    }
    if (notificationName === null || notificationName === undefined || typeof notificationName.valueOf() !== 'string') {
      throw new Error('notificationName cannot be null or undefined and it must be of type string.');
    }
    if (email === null || email === undefined || typeof email.valueOf() !== 'string') {
      throw new Error('email cannot be null or undefined and it must be of type string.');
    }
    if (this.client.apiVersion === null || this.client.apiVersion === undefined || typeof this.client.apiVersion.valueOf() !== 'string') {
      throw new Error('this.client.apiVersion cannot be null or undefined and it must be of type string.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}/recipientEmails/{email}';
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{serviceName}', encodeURIComponent(serviceName));
  requestUrl = requestUrl.replace('{notificationName}', encodeURIComponent(notificationName));
  requestUrl = requestUrl.replace('{email}', encodeURIComponent(email));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'DELETE';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200 && statusCode !== 204) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorResponse']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;

    return callback(null, result, httpRequest, response);
  });
}

/** Class representing a NotificationRecipientEmail. */
class NotificationRecipientEmail {
  /**
   * Create a NotificationRecipientEmail.
   * @param {ApiManagementClient} client Reference to the service client.
   */
  constructor(client) {
    this.client = client;
    this._listByNotification = _listByNotification;
    this._checkEntityExists = _checkEntityExists;
    this._createOrUpdate = _createOrUpdate;
    this._deleteMethod = _deleteMethod;
  }

  /**
   * Gets the list of the Notification Recipient Emails subscribed to a
   * notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<RecipientEmailCollection>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listByNotificationWithHttpOperationResponse(resourceGroupName, serviceName, notificationName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listByNotification(resourceGroupName, serviceName, notificationName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Gets the list of the Notification Recipient Emails subscribed to a
   * notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {RecipientEmailCollection} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object if an error did not occur.
   *                      See {@link RecipientEmailCollection} for more
   *                      information.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  listByNotification(resourceGroupName, serviceName, notificationName, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._listByNotification(resourceGroupName, serviceName, notificationName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listByNotification(resourceGroupName, serviceName, notificationName, options, optionalCallback);
    }
  }

  /**
   * Determine if Notification Recipient Email subscribed to the notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {string} email Email identifier.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<Boolean>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  checkEntityExistsWithHttpOperationResponse(resourceGroupName, serviceName, notificationName, email, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._checkEntityExists(resourceGroupName, serviceName, notificationName, email, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Determine if Notification Recipient Email subscribed to the notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {string} email Email identifier.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {Boolean} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {boolean} [result]   - The deserialized result object if an error did not occur.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  checkEntityExists(resourceGroupName, serviceName, notificationName, email, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._checkEntityExists(resourceGroupName, serviceName, notificationName, email, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._checkEntityExists(resourceGroupName, serviceName, notificationName, email, options, optionalCallback);
    }
  }

  /**
   * Adds the Email address to the list of Recipients for the Notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {string} email Email identifier.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<RecipientEmailContract>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  createOrUpdateWithHttpOperationResponse(resourceGroupName, serviceName, notificationName, email, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._createOrUpdate(resourceGroupName, serviceName, notificationName, email, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Adds the Email address to the list of Recipients for the Notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {string} email Email identifier.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {RecipientEmailContract} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object if an error did not occur.
   *                      See {@link RecipientEmailContract} for more
   *                      information.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  createOrUpdate(resourceGroupName, serviceName, notificationName, email, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._createOrUpdate(resourceGroupName, serviceName, notificationName, email, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._createOrUpdate(resourceGroupName, serviceName, notificationName, email, options, optionalCallback);
    }
  }

  /**
   * Removes the email from the list of Notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {string} email Email identifier.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<null>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  deleteMethodWithHttpOperationResponse(resourceGroupName, serviceName, notificationName, email, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._deleteMethod(resourceGroupName, serviceName, notificationName, email, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Removes the email from the list of Notification.
   *
   * @param {string} resourceGroupName The name of the resource group.
   *
   * @param {string} serviceName The name of the API Management service.
   *
   * @param {string} notificationName Notification Name Identifier. Possible
   * values include: 'RequestPublisherNotificationMessage',
   * 'PurchasePublisherNotificationMessage', 'NewApplicationNotificationMessage',
   * 'BCC', 'NewIssuePublisherNotificationMessage', 'AccountClosedPublisher',
   * 'QuotaLimitApproachingPublisherNotificationMessage'
   *
   * @param {string} email Email identifier.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {null} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {null} [result]   - The deserialized result object if an error did not occur.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  deleteMethod(resourceGroupName, serviceName, notificationName, email, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._deleteMethod(resourceGroupName, serviceName, notificationName, email, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._deleteMethod(resourceGroupName, serviceName, notificationName, email, options, optionalCallback);
    }
  }

}

module.exports = NotificationRecipientEmail;
