/**
 * Copyright 2022 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
const pkg = require('../../package.json'),
  DiscoveryV2 = require('ibm-watson/discovery/v2'),
  { IamAuthenticator } = require('ibm-watson/auth');


function DiscoveryUtils2() {}
DiscoveryUtils2.prototype = {

  buildService: function(apikey, endpoint) {
    let authSettings = {};
    let serviceSettings = {
      version: '2020-08-30',
      headers: {
        'User-Agent': pkg.name + '-' + pkg.version
      }
    };

    if (apikey) {
      authSettings.apikey = apikey;
    }
    serviceSettings.authenticator = new IamAuthenticator(authSettings);

    if (endpoint) {
      serviceSettings.url = endpoint;
      serviceSettings.serviceUrl = endpoint;
    }

    return new DiscoveryV2(serviceSettings);
  },


  paramProjectCheck: function(params) {
    return this.paramFieldCheck(params, 'projectId');
  },

  paramCollectionCheck: function(params) {
    return this.paramFieldCheck(params, 'projectId')
              + this.paramFieldCheck(params, 'collectionId')  ;
  },


  paramNameCheck: function(params) {
    return this.paramFieldCheck(params, 'name');
  },

  paramTypeCheck: function(params) {
    return this.paramFieldCheck(params, 'type');
  },

  paramFieldCheck: function(params, field) {
    var response = '';
    if (!params[field]) {
      response = 'Missing ' + field + ' ';
    }
    return response;
  },

  buildParamsFor: function(msg, config, params, field) {
    if (msg.discoveryparams && msg.discoveryparams[field]) {
      params[field] = msg.discoveryparams[field];
    } else if (config[field]) {
      params[field] = config[field];
    }
    return params;
  },

  buildParamsForName: function(msg, config, params) {
    let name = '';
    if (msg.discoveryparams) {
      for (f of ['projectName', 'projectname',
        'collectionName', 'collectionname']) {
        if (msg.discoveryparams[f]) {
          name = msg.discoveryparams[f];
          break;
        }
      }
    }
    if (!name) {
      for (f of ['projectName', 'collectionName']) {
        if (config[f]) {
          name = config[f];
          break;
        }
      }
    }
    if (name) {
      params.name = name;
    }
    return params;
  },

  buildParamsForType: function(msg, config, params) {
    let type = '';
    if (msg.discoveryparams) {
      if (msg.discoveryparams.projectType) {
        type = msg.discoveryparams.projectType;
      } else if (msg.discoveryparams.projecttype) {
        type = msg.discoveryparams.projecttype;
      }
    }
    if (!type) {
      if (config.projectType) {
        type = config.projectType
      }
    }
    if (type) {
      params.type = type;
    }
    return params;
  },

  addOtherParams: function(msg, params) {
    if (msg.discoveryparams) {
      if (msg.discoveryparams.defaultQueryParameters) {
        params.defaultQueryParameters = msg.discoveryparams.defaultQueryParameters;
      }
      if (msg.discoveryparams.enrichments) {
        params.defaultQueryParameters = msg.discoveryparams.enrichments;
      }
    }
    return params
  },

  buildParams: function(msg, config) {
    var params = {},
      me = this;

    params = me.buildParamsForName(msg, config, params);
    params = me.buildParamsForType(msg, config, params);
    params = me.addOtherParams(msg, params);

    ['projectId', 'collectionId', 'queryId',
        'description', 'language'].forEach(function(f) {
      params = me.buildParamsFor(msg, config, params, f);
    });

    return params;
  }


};

var discoveryutils2 = new DiscoveryUtils2();

module.exports = discoveryutils2;

@@ -1559,18 +1559,354 @@
 tionId')
-  
+;%0A  %7D,%0A%0A  paramQueryIdCheck: function(params) %7B%0A    return this.paramFieldCheck(params, 'projectId')%0A              + this.paramFieldCheck(params, 'queryId');%0A  %7D,%0A%0A  paramQueryFieldCheck: function(params) %7B%0A    if (! params%5B'query'%5D && ! params%5B'naturalLanguageQuery'%5D)  %7B%0A      return %22No query found on msg.payload%22;%0A    %7D%0A    return ''
 ;%0A  %7D,%0A%0A

fix indentation