/*** 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 insightsAlertsCommand = cli.category("insights").category("alerts").description($("Manages alert rules")).command("list <resourceId>").description($("List alert logs for a resource.")).usage("[options] <resourceId>").option("-i --resourceId <resourceId>", $("The resource Id.")).option("-b --startTime <startTime>", $("The start time of the query.")).option("-e --endTime <endTime>", $("The end time of the query.")).option("--status <status>", $("The status.")).option("--caller <caller>", $("Caller to look for when querying.")).option("-d --detailedOutput", $("Shows the details of the events in the log.")).option("-s --subscription <subscription>", $("The subscription identifier.")).execute(function __1(resourceId, options, _) { var __frame = { name: "__1", line: 39 }; return __func(_, this, arguments, __1, 2, __frame, function __$__1() {












      return insightsAlertsCommand._prepareAndExecute(resourceId, options, __cb(_, __frame, 1, 30, function __$__1() { _(); }, true)); }); });


  insightsAlertsCommand._filterByResourceId = function(resourceId) {
    if (__.isString(resourceId)) {
      var resourceIdLowerCase = resourceId.toLowerCase();
      return function(record) { return (__.isString(record.resourceId) && (record.resourceId.toLowerCase() === resourceIdLowerCase)); }; }
     else {
      return insightsUtils.passAllFilter; } ; };



  insightsAlertsCommand._processGeneralParameters = function(startTime, endTime, status, caller) {
    var queryFilter = insightsUtils.validateDateTimeRangeAndAddDefaultsEvents(startTime, endTime);
    log.silly("s2");
    queryFilter = insightsUtils.addConditionIfPresent(queryFilter, "status", status);
    queryFilter = insightsUtils.addConditionIfPresent(queryFilter, "caller", caller);

    return queryFilter; };


  insightsAlertsCommand._prepareAndExecute = function insightsAlertsCommand__prepareAndExecute__2(resourceId, options, _) { var client, queryFilter, __this = this; var __frame = { name: "insightsAlertsCommand__prepareAndExecute__2", line: 61 }; return __func(_, this, arguments, insightsAlertsCommand__prepareAndExecute__2, 2, __frame, function __$insightsAlertsCommand__prepareAndExecute__2() {
      if (!__.isString(resourceId)) {
        cli.missingArgument("resourceId"); } ;


      client = insightsUtils.createInsightsClient(log, options);

      queryFilter = __this._processGeneralParameters(options.startTime, options.endTime, options.status, options.caller);
      log.silly("s3");
      queryFilter = insightsUtils.addConditionIfPresent(queryFilter, "eventSource", insightsUtils.alertsEventSourceName);

      return __this._executeEventsCmd(client, queryFilter, (!options.detailedOutput ? insightsUtils.selectedFields : null), __this._filterByResourceId(resourceId), options, __cb(_, __frame, 11, 9, function __$insightsAlertsCommand__prepareAndExecute__2() { _(); }, true)); }); };


  insightsAlertsCommand._executeEventsCmd = function insightsAlertsCommand__executeEventsCmd__3(client, queryFilter, selectedFields, keepTheRecord, options, _) { var progress, result, response, recordFilter, nextLink; var __frame = { name: "insightsAlertsCommand__executeEventsCmd__3", line: 75 }; return __func(_, this, arguments, insightsAlertsCommand__executeEventsCmd__3, 5, __frame, function __$insightsAlertsCommand__executeEventsCmd__3() {
      progress = cli.interaction.progress(util.format($("Querying \"%s\""), queryFilter));
      result = []; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$insightsAlertsCommand__executeEventsCmd__3() {

            return client.eventOperations.listEvents(queryFilter, selectedFields, __cb(_, __frame, 4, 44, function ___(__0, __1) { response = __1;


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

              recordFilter = function(element) { if (keepTheRecord(element)) { result.push(element); } ; };
              __.each(response.eventDataCollection.value, recordFilter);

              nextLink = response.eventDataCollection.nextLink; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$insightsAlertsCommand__executeEventsCmd__3() { __more = false;
                  var __4 = nextLink; if (__4) {
                    log.silly("Following nextLink");
                    return client.eventOperations.listEventsNext(nextLink, __cb(_, __frame, 16, 42, function ___(__0, __2) { response = __2;
                      __.each(response.eventDataCollection.value, recordFilter);
                      nextLink = response.eventDataCollection.nextLink; while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(function __$insightsAlertsCommand__executeEventsCmd__3() { _(null, null, true); }); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$insightsAlertsCommand__executeEventsCmd__3() {


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


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