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

















var util = require("util");
var utils = require("../../../util/utils");
var insightsUtils = require("./insights.utils");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;
  var insightsAutoscaleSettingCommand = cli.category("insights").category("autoscale").category("setting").description($("Manages autoscale settings"));


  insightsAutoscaleSettingCommand.command("list <resourceGroup>").description($("List autoscale settings for a resource.")).usage("[options] <resourceGroup>").option("-g --resourceGroup <resourceGroup>", $("The resource group.")).option("-n --settingName <settingName>", $("The name of the setting.")).execute(function __1(resourceGroup, options, _) { var __frame = { name: "__1", line: 35 }; return __func(_, this, arguments, __1, 2, __frame, function __$__1() {





      return insightsAutoscaleSettingCommand._prepareAndExecute(resourceGroup, options, __cb(_, __frame, 1, 38, function __$__1() { _(); }, true)); }); });


  insightsAutoscaleSettingCommand.command("delete <resourceGroup> <settingName>").description($("Deletes an autoscale setting.")).usage("[options] <resourceGroup> <settingName>").option("-g --resourceGroup <resourceGroup>", $("The resource group.")).option("-n --settingName <settingName>", $("The name of the setting.")).execute(function __2(resourceGroup, settingName, options, _) { var __frame = { name: "__2", line: 44 }; return __func(_, this, arguments, __2, 3, __frame, function __$__2() {





      return insightsAutoscaleSettingCommand._prepareAndExecuteDelete(resourceGroup, settingName, options, __cb(_, __frame, 1, 38, function __$__2() { _(); }, true)); }); });


  insightsAutoscaleSettingCommand.command("set <settingName> <location> <resourceGroup>").description($("Create or set an autoscale setting.")).usage("[options] <settingName> <location> <resourceGroup>").option("-x --disable", $("Flag to disable the setting.")).option("-a --autoProfiles <autoProfiles>", $("A list of autoscale profiles in JSON format.")).option("-g --resourceGroup <resourceGroup>", $("The resource group.")).option("-n --settingName <settingName>", $("The name of the setting.")).option("-l --location <location>", $("The location.")).option("-i --targetResourceId <targetResourceId>", $("The resource Id.")).option("-p --settingSpec <settingSpec>", $("The setting spec in JSON format.")).option("-w --notifications <notifications>", $("The list object autoscale notifications in escaped-jsson format.")).execute(function __3(settingName, location, resourceGroup, options, _) { var __frame = { name: "__3", line: 68 }; return __func(_, this, arguments, __3, 4, __frame, function __$__3() {




















      return insightsAutoscaleSettingCommand._prepareAndExecuteSet(settingName, location, resourceGroup, options, __cb(_, __frame, 1, 38, function __$__3() { _(); }, true)); }); });


  insightsAutoscaleSettingCommand._prepareAndExecute = function insightsAutoscaleSettingCommand__prepareAndExecute__4(resourceGroup, options, _) { var client, __this = this; var __frame = { name: "insightsAutoscaleSettingCommand__prepareAndExecute__4", line: 72 }; return __func(_, this, arguments, insightsAutoscaleSettingCommand__prepareAndExecute__4, 2, __frame, function __$insightsAutoscaleSettingCommand__prepareAndExecute__4() {
      if (!__.isString(resourceGroup)) {
        cli.missingArgument("resourceGroup"); } ;


      client = insightsUtils.createInsightsManagementClient(log, options);

      return __this._executeCmd(client, resourceGroup, options, __cb(_, __frame, 7, 9, function __$insightsAutoscaleSettingCommand__prepareAndExecute__4() { _(); }, true)); }); };


  insightsAutoscaleSettingCommand._prepareAndExecuteDelete = function insightsAutoscaleSettingCommand__prepareAndExecuteDelete__5(resourceGroup, settingName, options, _) { var client, __this = this; var __frame = { name: "insightsAutoscaleSettingCommand__prepareAndExecuteDelete__5", line: 82 }; return __func(_, this, arguments, insightsAutoscaleSettingCommand__prepareAndExecuteDelete__5, 3, __frame, function __$insightsAutoscaleSettingCommand__prepareAndExecuteDelete__5() {
      if (!__.isString(resourceGroup)) {
        cli.missingArgument("resourceGroup"); } ;


      if (!__.isString(settingName)) {
        cli.missingArgument("settingName"); } ;


      client = insightsUtils.createInsightsManagementClient(log, options);

      return __this._executeDeleteCmd(client, resourceGroup, settingName, options, __cb(_, __frame, 11, 9, function __$insightsAutoscaleSettingCommand__prepareAndExecuteDelete__5() { _(); }, true)); }); };


  insightsAutoscaleSettingCommand._prepareAndExecuteSet = function insightsAutoscaleSettingCommand__prepareAndExecuteSet__6(settingName, location, resourceGroup, options, _) { var client, parameters, __this = this; var __frame = { name: "insightsAutoscaleSettingCommand__prepareAndExecuteSet__6", line: 96 }; return __func(_, this, arguments, insightsAutoscaleSettingCommand__prepareAndExecuteSet__6, 4, __frame, function __$insightsAutoscaleSettingCommand__prepareAndExecuteSet__6() {
      client = insightsUtils.createInsightsManagementClient(log, options);
      parameters = __this._createSdkCallParameters(settingName, location, resourceGroup, options.targetResourceId, options);
      log.silly(util.inspect(parameters));

      return __this._executeSetCmd(client, resourceGroup, settingName, parameters, options, __cb(_, __frame, 5, 9, function __$insightsAutoscaleSettingCommand__prepareAndExecuteSet__6() { _(); }, true)); }); };


  function processRule(rule) {
    if (rule.metricTrigger) {
      var trigger = rule.metricTrigger;
      trigger.timeGrain = insightsUtils.validateTimeSpan(trigger.timeGrain);
      if (trigger.timeWindow) {
        trigger.timeWindow = insightsUtils.validateTimeSpan(trigger.timeWindow); } ; }

     else {
      throw new Error($("Rule missing metricTrigger.")); } ;


    if (rule.scaleAction) {
      var scaleAction = rule.scaleAction;
      scaleAction.cooldown = insightsUtils.validateTimeSpan(scaleAction.cooldown); }
     else {
      throw new Error($("Rule missing scaleAction.")); } ; };



  function processProfile(profile) {
    var rules = profile.rules;
    if (__.isArray(rules)) {
      __.each(rules, processRule); }
     else {
      throw new Error($("Invalid rules parameters, array expected.")); } ; };



  insightsAutoscaleSettingCommand._processProfiles = function(autoProfiles) {
    var profilesArray = [];
    if (autoProfiles) {
      var profiles = JSON.parse(autoProfiles);

      log.silly(util.format("Parsed profiles: %s", util.inspect(profiles)));

      if (__.isArray(profiles)) {
        for (var i = 0; (i < profiles.length); i = (i + 1)) {
          processProfile(profiles[i]);

          log.silly(util.format("Processed profile #%s: %s", i, util.inspect(profiles[i])));

          profilesArray.push(profiles[i]); }; }

       else {
        throw new Error($("Invalid profiles argument: array expected.")); } ; } ;



    return profilesArray; };


  insightsAutoscaleSettingCommand._processNotifications = function(notifications) {
    var notificationsArray = [];
    if (notifications) {
      var internalNotifications = JSON.parse(notifications);

      log.silly(util.format("Parsed notifications: %s", util.inspect(internalNotifications)));

      if (__.isArray(internalNotifications)) {
        for (var i = 0; (i < internalNotifications.length); i = (i + 1)) {
          notificationsArray.push(internalNotifications[i]); }; }

       else {
        throw new Error($("Invalid notifications argument: array expected.")); } ; } ;



    return notificationsArray; };


  insightsAutoscaleSettingCommand._createSdkCallParameters = function(settingName, location, resourceGroup, targetResourceId, options) {
    log.silly(util.format("Options: %s", util.inspect(options)));

    if (!__.isString(settingName)) {
      return cli.missingArgument("settingName"); } ;


    if (!__.isString(location)) {
      return cli.missingArgument("location"); } ;


    if (!__.isString(resourceGroup)) {
      return cli.missingArgument("resourceGroup"); } ;


    var enableSetting = !options.disable;
    var locationInternal = location;
    var nameInternal = settingName;
    var targetResourceIdInternal = targetResourceId;



    var autoscaleProfilesInternal = this._processProfiles(options.autoProfiles);
    log.silly(util.format("Pre-processed profiles: %s", util.inspect(autoscaleProfilesInternal)));

    var notificationsInternal = this._processNotifications(options.notifications);
    log.silly(util.format("Pre-processed notifications: %s", util.inspect(notificationsInternal)));

    if (options.settingSpec) {

      var settingSpec = JSON.parse(options.settingSpec);
      var property = settingSpec.properties;

      if (!property) {
        throw new Error($("Properties in settingSpec cannot be null.")); } ;


      locationInternal = settingSpec.location;
      nameInternal = settingSpec.name;


      autoscaleProfilesInternal = (__.isArray(autoscaleProfilesInternal) ? autoscaleProfilesInternal : property.profiles);
      targetResourceIdInternal = property.targetResourceUri;

      enableSetting = (!options.disable && property.enabled);
      for (var i = 0; (property.notifications && (i < property.notifications.length)); i = (i + 1)) {
        notificationsInternal.push(property.notifications[i]); }; }

     else {

      if (!__.isString(settingName)) {
        return cli.missingArgument("settingName"); } ;


      if (!__.isString(location)) {
        return cli.missingArgument("location"); } ;


      if (!__.isString(targetResourceId)) {
        return cli.missingArgument("targetResourceId"); } ; } ;



    return {
      location: locationInternal,
      properties: {
        name: nameInternal,
        enabled: enableSetting,
        profiles: autoscaleProfilesInternal,
        targetResourceUri: targetResourceIdInternal,
        notifications: notificationsInternal },

      tags: (options.settingSpec ? options.settingSpec.tags : null) }; };



  insightsAutoscaleSettingCommand._executeCmd = function insightsAutoscaleSettingCommand__executeCmd__7(client, resourceGroup, options, _) { var progress, result, response; var __frame = { name: "insightsAutoscaleSettingCommand__executeCmd__7", line: 250 }; return __func(_, this, arguments, insightsAutoscaleSettingCommand__executeCmd__7, 3, __frame, function __$insightsAutoscaleSettingCommand__executeCmd__7() {
      progress = cli.interaction.progress($("Querying for autoscale settings"));
      result = []; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeCmd__7() { return (function __$insightsAutoscaleSettingCommand__executeCmd__7(__then) {


              if (((__.isNull(options.settingName) || __.isUndefined(options.settingName)) || ((__.isString(options.settingName) && (options.settingName === ""))))) {
                log.silly("Query by resourceGroup only");
                return client.autoscaleOperations.listSettings(resourceGroup, null, __cb(_, __frame, 7, 46, function ___(__0, __1) { response = __1;

                  log.silly((!response ? util.inspect(response) : "nothing in response"));
                  log.silly(((!response && response.autoscaleSettingResourceCollection) ? util.inspect(response.autoscaleSettingResourceCollection) : "nothing in autoscaleSettingResourceCollection"));


                  __.each(response.autoscaleSettingResourceCollection.value, function(element) { result.push(element); }); __then(); }, true)); } else {

                log.silly("Query by setting name");
                return client.autoscaleOperations.getSetting(resourceGroup, options.settingName, __cb(_, __frame, 16, 46, function ___(__0, __2) { response = __2;

                  log.silly((!response ? util.inspect(response) : "nothing in response"));


                  result.push(response); __then(); }, true)); } ; })(function __$insightsAutoscaleSettingCommand__executeCmd__7() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeCmd__7() {


              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeCmd__7() {


          insightsUtils.formatOutputList(cli, log, options, result); _(); }); }); }); };


  insightsAutoscaleSettingCommand._executeDeleteCmd = function insightsAutoscaleSettingCommand__executeDeleteCmd__8(client, resourceGroup, settingName, options, _) { var progress, response; var __frame = { name: "insightsAutoscaleSettingCommand__executeDeleteCmd__8", line: 280 }; return __func(_, this, arguments, insightsAutoscaleSettingCommand__executeDeleteCmd__8, 4, __frame, function __$insightsAutoscaleSettingCommand__executeDeleteCmd__8() {
      progress = cli.interaction.progress(utils.format($("Deleting autoscale setting \"%s\""), settingName));
      response = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeDeleteCmd__8() {

            return client.autoscaleOperations.deleteSetting(resourceGroup, settingName, __cb(_, __frame, 4, 44, function ___(__0, __1) { response = __1;

              log.silly((!response ? util.inspect(response) : "nothing in response")); _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeDeleteCmd__8() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeDeleteCmd__8() {


          insightsUtils.formatOutput(cli, log, options, response); _(); }); }); }); };


  insightsAutoscaleSettingCommand._executeSetCmd = function insightsAutoscaleSettingCommand__executeSetCmd__9(client, resourceGroup, settingName, parameters, options, _) { var progress, response; var __frame = { name: "insightsAutoscaleSettingCommand__executeSetCmd__9", line: 294 }; return __func(_, this, arguments, insightsAutoscaleSettingCommand__executeSetCmd__9, 5, __frame, function __$insightsAutoscaleSettingCommand__executeSetCmd__9() {
      progress = cli.interaction.progress(util.format($("Create or set the autoscale setting \"%s\"."), settingName));
      response = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeSetCmd__9() {

            return client.autoscaleOperations.createOrUpdateSetting(resourceGroup, settingName, parameters, __cb(_, __frame, 4, 44, function ___(__0, __1) { response = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeSetCmd__9() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$insightsAutoscaleSettingCommand__executeSetCmd__9() {


          insightsUtils.formatOutput(cli, log, options, response); _(); }); }); }); };};
