/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb; var __ = require("underscore");
















var util = require("util");

var profile = require("../util/profile");
var utils = require("../util/utils");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;

  var account = cli.category("account");
  var environment = account.category("env").description($("Commands to manage your account environment"));


  environment.command("list").description($("List the environments")).execute(function(options, callback) {


    var environments = profile.current.environments;

    if (log.format().json) {


      log.json(JSON.parse(JSON.stringify(environments))); }
     else {
      log.table(Object.keys(environments), function(row, s) {
        row.cell("Name", s); }); } ;



    callback(); });


  environment.command("show [environment]").description($("Show an environment")).option("--environment <environment>", $("the environment name")).execute(function __1(environment, options, _) { var existingEnvironment, endpointsForDisplay, report; var __frame = { name: "__1", line: 53 }; return __func(_, this, arguments, __1, 2, __frame, function __$__1() {



      return cli.interaction.promptIfNotGiven("Environment name: ", environment, __cb(_, __frame, 1, 36, function ___(__0, __1) { environment = __1;

        existingEnvironment = profile.current.getEnvironment(environment);

        if (!existingEnvironment) {
          return _(new Error(util.format($("Unknown environment %s"), environment))); }
         else {
          if (log.format().json) {


            log.json(JSON.parse(JSON.stringify(existingEnvironment))); }
           else {
            fields = __.keys(existingEnvironment);
            endpointsForDisplay = __.object(fields.map(function(endpoint) {
              var value = "";
              try {
                value = existingEnvironment[endpoint];
              } catch (ex) {  };


              return [endpoint,value,]; }));




            report = fields.sort().filter(function(field) { return (field !== "name"); }).map(function(field) { return [field,field,]; });

            log.report([["Name","name",],].concat(report), endpointsForDisplay); return _(null); } ; } ; _(); }, true)); }); });









  function nameToOptionString(name) {
    var parts = name.split(/(?=[A-Z])/);
    return util.format("--%s <%s>", parts.map(function(s) {
      return s.toLowerCase(); }).join("-"), name); };







  function addEnvironmentOptions(cmd) {
    profile.Environment.parameters.sort(function(p) {
      return p.name;
    }).forEach(function(p) {
      cmd.option(nameToOptionString(p.name), p.description); });

    return cmd; };


  function createUpdateObject(options) {
    return __.pick(options, __.pluck(profile.Environment.parameters, "name")); };


  var addCommand = environment.command("add [environment]").description($("Add an environment")).option("--environment <environment>", $("the environment name"));



  addCommand = addEnvironmentOptions(addCommand);
  addCommand.execute(function __2(environment, options, _) { var existingEnvironment, update, newEnvironment; var __frame = { name: "__2", line: 121 }; return __func(_, this, arguments, __2, 2, __frame, function __$__2() {

      return cli.interaction.promptIfNotGiven("New Environment name: ", environment, __cb(_, __frame, 1, 36, function ___(__0, __1) { environment = __1;

        existingEnvironment = profile.current.getEnvironment(environment);

        if (existingEnvironment) {
          return _(new Error(util.format($("Duplicate environment %s"), existingEnvironment))); } ;


        update = createUpdateObject(options);
        if (!update.managementEndpointUrl) {
          return _(new Error($("management endpoint url must be given when creating an environment"))); } ;


        newEnvironment = new profile.Environment(update);
        newEnvironment.name = environment;
        profile.current.addEnvironment(newEnvironment);
        profile.current.save();
        log.info(util.format($("New environment %s created"), environment)); _(); }, true)); }); });


  var setCommand = environment.command("set [environment]").description($("Update an environment")).option("--environment <environment>", $("the environment name"));



  setCommand = addEnvironmentOptions(setCommand);

  setCommand.execute(function __3(environment, options, _) { var updates, existingEnvironment; var __frame = { name: "__3", line: 149 }; return __func(_, this, arguments, __3, 2, __frame, function __$__3() {

      return cli.interaction.promptIfNotGiven("New Environment name: ", environment, __cb(_, __frame, 1, 36, function ___(__0, __1) { environment = __1;

        updates = createUpdateObject(options);
        if ((__.keys(updates).length === 0)) {
          return _(new Error($("No URL to update was specified"))); } ;


        existingEnvironment = profile.current.getEnvironment(environment);

        if (!existingEnvironment) {
          return _(new Error(util.format($("Unknown environment %s"), environment))); }
         else {
          __.extend(existingEnvironment, updates);
          profile.current.save(); } ; _(); }, true)); }); });



  environment.command("delete [environment]").description($("Delete an environment")).option("--environment <environment>", $("the environment name")).execute(function __4(environment, options, _) { var existingEnvironment; var __frame = { name: "__4", line: 170 }; return __func(_, this, arguments, __4, 2, __frame, function __$__4() {



      return cli.interaction.promptIfNotGiven("New Environment name: ", environment, __cb(_, __frame, 1, 36, function ___(__0, __1) { environment = __1;

        existingEnvironment = profile.current.getEnvironment(environment);

        if (!existingEnvironment) {
          return _(new Error(util.format($("Unknown environment %s"), environment))); }
         else {
          profile.current.deleteEnvironment(existingEnvironment);
          profile.current.save(); } ; _(); }, true)); }); });};
