text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  /**
   * Copyright(c) Microsoft Corporation.All rights reserved.
   * Licensed under the MIT License.
   */
  // tslint:disable:no-console
  // tslint:disable:no-object-literal-type-assertion
  const botframework_config_1 = require("botframework-config");
  const chalk = require("chalk");
  const program = require("commander");
  const fsx = require("fs-extra");
  const readline = require("readline-sync");
  const validurl = require("valid-url");
  const utils_1 = require("./utils");
  require('log-prefix')(() => utils_1.showMessage('%s'));
  program.option('--verbose', 'Add [msbot] prefix to all messages');
  program.Command.prototype.unknownOption = (flag) => {
      console.error(chalk.default.redBright(`Unknown arguments: ${flag}`));
      program.help();
  };
  program
      .name('msbot init')
      .option('-n, --name <name>', 'name of the bot')
      .option('-a, --appId  <appid>', 'Microsoft AppId used for auth with the endpoint')
      .option('-p, --appPassword <password>', 'Microsoft app password used for auth with the endpoint')
      .option('-e, --endpoint <endpoint>', 'local endpoint for the bot')
      .option('--secret', 'generate a secret and encrypt service keys with it')
      .option('-q, --quiet', 'do not prompt');
  const command = program.parse(process.argv);
  const args = {};
  Object.assign(args, command);
  if (!args.quiet) {
      let exists = fsx.existsSync(`${args.name}.bot`);
      while (((!args.hasOwnProperty('name') || args.name.length === 0)) || exists) {
          if (exists) {
              console.log(`${args.name}.bot already exists`);
          }
          args.name = readline.question(`What name would you like for your bot? `);
          exists = fsx.existsSync(`${args.name}.bot`);
      }
      while (!args.endpoint || args.endpoint.length === 0) {
          // tslint:disable-next-line:max-line-length
          args.endpoint = readline.question(`What localhost endpoint does your bot use for debugging? ` +
              `[Example: http://localhost:3978/api/messages] `, { defaultInput: ' ' });
      }
      if (validurl.isHttpUri(args.endpoint) || validurl.isHttpsUri(args.endpoint)) {
          if (!args.appId || args.appId.length === 0) {
              const answer = readline.question(`Do you have an appId for endpoint? [no] `, {
                  defaultInput: 'no'
              });
              if (answer === 'y' || answer === 'yes') {
                  args.appId = readline.question(`What is your appId for ${args.endpoint}? [none] `, {
                      defaultInput: ''
                  });
              }
          }
          while (args.appId && args.appId.length > 0 && (!args.appPassword || args.appPassword.length === 0)) {
              args.appPassword = readline.question(`What is your appPassword for ${args.endpoint}? `, {
                  defaultInput: ''
              });
          }
      }
      if (!args.secret) {
          // tslint:disable-next-line:max-line-length
          const answer = readline.question(`=== Your bot file contains service keys and we strongly recommend you encrypt them to keep them safe. ===\nWould you like to encrypt your keys with a secret? [yes] `, {
              defaultInput: 'yes'
          });
          args.secret = answer === 'y' || answer === 'yes';
      }
  }
  let secret;
  if (args.secret) {
      secret = botframework_config_1.BotConfiguration.generateKey();
  }
  if (!args.name) {
      console.error('missing --name argument');
  }
  else {
      const bot = new botframework_config_1.BotConfiguration();
      bot.name = args.name;
      bot.description = args.description;
      if (validurl.isHttpUri(args.endpoint) || validurl.isHttpsUri(args.endpoint)) {
          bot.connectService(new botframework_config_1.EndpointService({
              name: args.name,
              endpoint: args.endpoint,
              appId: args.appId || '',
              appPassword: args.appPassword || ''
          }));
      }
      const filename = `${bot.name}.bot`;
      bot.saveAs(filename, secret);
      const result = {
          status: 'OK',
          file: filename,
          secret: secret
      };
      if (args.quiet) {
          console.log(JSON.stringify(result, null, 2));
      }
      else {
          console.log(`created ${filename}`);
          if (secret) {
              // tslint:disable-next-line:max-line-length
              console.log(`Your bot is encrypted with secret:\n${secret}\n\nPlease save this secret in a secure place to keep your keys safe.`);
          }
      }
  }
  //# sourceMappingURL=msbot-init.js.map