text: /********************************************************* {COPYRIGHT-TOP} ***
   * Licensed Materials - Property of IBM
   * 5725-Z22, 5725-Z63, 5725-U33, 5725-Z63
   *
   * (C) Copyright IBM Corporation 2016, 2017
   *
   * All Rights Reserved.
   * US Government Users Restricted Rights - Use, duplication or disclosure
   * restricted by GSA ADP Schedule Contract with IBM Corp.
   ********************************************************** {COPYRIGHT-END} **/
  // Node module: apiconnect-cli-create
  
  var Promise = require('bluebird');
  var g = require('strong-globalize')();
  var getApi = require('./factory').getApi;
  var getProduct = require('./factory').getProduct;
  var inquirer = require('inquirer');
  var path = require('path');
  var util = require('./util/util');
  
  function getCommonInputs(options) {
    var inputs = [];
    var slugText = null;
  
    if (!options.title) {
      inputs.push({
        type: 'input',
        message: g.f('Title'),
        name: 'title',
        validate: function(title) {
          if (title) {
            return true;
          } else {
            return g.f('Please provide a title');
          }
        },
      });
    }
  
    if (!options.name) {
      inputs.push({
        type: 'input',
        message: g.f('Name'),
        name: 'name',
        default: function(answers) {
          slugText = util.generateSlugFromText(answers.title);
          return slugText;
        },
      });
    }
  
    if (!options.filename) {
      inputs.push({
        type: 'input',
        message: g.f('File'),
        name: 'filename',
        default: function(answers) {
          return util.generateSlugFromText(answers.name) + '.yaml';
        },
      });
    }
  
    if (!options.template) {
      inputs.push({
        type: 'input',
        message: g.f('Template'),
        name: 'template',
        default: function(answers) {
          return '';
        },
      });
    }
  
    return inputs;
  }
  
  function getApiInputs(options) {
    var inputs = [];
  
    if (!options.basepath) {
      inputs.push({
        type: 'input',
        message: g.f('Basepath'),
        name: 'basepath',
        default: function(answers) {
          return '/' + util.generateSlugFromText(answers.name);
        },
      });
    }
  
    if (!options.hostname) {
      inputs.push({
        type: 'input',
        message: g.f('Hostname'),
        name: 'hostname',
        default: function(answers) {
          return '$(catalog.host)';
        },
      });
    }
  
    if (!options.schemes) {
      inputs.push({
        type: 'checkbox',
        message: g.f('Schemes'),
        name: 'schemes',
        choices: [
          { name: 'http' },
          { name: 'https' },
          { name: 'ws' },
          { name: 'wss' },
        ],
      });
    }
  
    if (!options.target_url) {
      inputs.push({
        type: 'input',
        message: g.f('Target url'),
        name: 'target_url',
        default: function(answers) {
          return '';
        },
      });
    }
  
    if (!options.product) {
      inputs.push({
        type: 'list',
        message: g.f('Create product'),
        name: 'createproduct',
        choices: [
          {
            name: g.f('true'),
            value: 'true',
          },
          {
            name: g.f('false'),
            value: 'false',
          },
        ],
      });
    }
  
    if (!options.product) {
      inputs.push({
        type: 'input',
        message: g.f('Product title'),
        name: 'producttitle',
        default: function(answers) {
          return g.f('%s Product', answers.title);
        },
        when: function(answers) {
          return answers.createproduct && answers.createproduct.toLowerCase() === 'true';
        },
      });
    }
  
    if (!options.product) {
      inputs.push({
        type: 'input',
        message: g.f('Product name'),
        name: 'productname',
        default: function(answers) {
          return util.generateSlugFromText(answers.producttitle);
        },
        when: function(answers) {
          return answers.createproduct && answers.createproduct.toLowerCase() === 'true';
        },
      });
    }
  
    if (!options.product) {
      inputs.push({
        type: 'input',
        message: g.f('Product file'),
        name: 'productfile',
        default: function(answers) {
          return answers.productname + '.yaml';
        },
        when: function(answers) {
          return answers.createproduct && answers.createproduct.toLowerCase() === 'true';
        },
      });
    }
  
    return inputs;
  }
  
  function getProductInputs(options) {
    var inputs = [];
  
    if (!options.apis || options.apis.length === 0) {
      inputs.push({
        type: 'input',
        message: g.f('{{API}} Files'),
        name: 'apis',
      });
    }
  
    return inputs;
  }
  
  
  function prompt(options) {
    if (typeof options.name !== 'string') {
      options.name = null;
    }
  
    if (typeof options.version !== 'string') {
      options.version = null;
    }
  
    if (options.title) {
      var slugText = util.generateSlugFromText(options.title);
      if (!options.name) { options.name = slugText; };
      if (!options.filename) { options.filename = util.generateSlugFromText(options.name) + '.yaml'; };
      if (options.type === 'api') {
        if (!options.basepath) { options.basepath = '/' + util.generateSlugFromText(options.name); };
        if (!options.hostname) { options.hostname = '$(catalog.host)'; };
        if (!options.schemes) { options.schemes = null; };
        if (!options.target_url) { options.target_url = ''; };
  
        if (options.product) {
          options.producttitle = options.product;
          if (!options.productfile) {
            var filename = options.filename;
            var basename = path.basename(filename, path.extname(filename));
            var productname = basename + '-product';
            options.productfile = productname + '.yaml';
          }
        } else {
          options.product = false;
        };
      }
    } else {
      var inputs = getCommonInputs(options);
      if (options.type === 'api') {
        inputs = inputs.concat(getApiInputs(options));
      } else {
        inputs = inputs.concat(getProductInputs(options));
      }
    }
  
    return new Promise(function(resolve, reject) {
      if (inputs && inputs.length > 0) {
        return inquirer.prompt(inputs, function(answers) {
          var api = getApi(options, answers);
          var prod;
          try {
            prod = getProduct(options, answers, api);
          } catch (err) {
            reject(err);
          }
          if (answers.createproduct && answers.createproduct.toLowerCase() === 'false') {
            resolve({ api: api });
          } else {
            resolve({ api: api, product: prod });
          }
        });
      } else {
        var api = getApi(options, {});
        var prod;
        try {
          prod = getProduct(options, {}, api);
        } catch (err) {
          reject(err);
        }
        resolve({ api: api, product: prod });
      }
    });
  }
  module.exports = prompt;
  
  