text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.dispose = exports.enable = void 0;
  var Contracts_1 = require("../../Declarations/Contracts");
  var Constants_1 = require("../../Declarations/Constants");
  var diagnostic_channel_1 = require("diagnostic-channel");
  var clients = [];
  // Mapping from bunyan levels defined at https://github.com/trentm/node-bunyan/blob/master/lib/bunyan.js#L256
  var bunyanToAILevelMap = {
      10: Contracts_1.SeverityLevel.Verbose,
      20: Contracts_1.SeverityLevel.Verbose,
      30: Contracts_1.SeverityLevel.Information,
      40: Contracts_1.SeverityLevel.Warning,
      50: Contracts_1.SeverityLevel.Error,
      60: Contracts_1.SeverityLevel.Critical,
  };
  var subscriber = function (event) {
      var message = event.data.result;
      clients.forEach(function (client) {
          var AIlevel = bunyanToAILevelMap[event.data.level];
          if (message instanceof Error) {
              client.trackException({ exception: (message) });
          }
          else {
              client.trackTrace({ message: message, severity: AIlevel });
          }
      });
  };
  function enable(enabled, client) {
      var statsbeat = client.getStatsbeat();
      if (enabled) {
          if (clients.length === 0) {
              diagnostic_channel_1.channel.subscribe("bunyan", subscriber);
              if (statsbeat) {
                  statsbeat.addInstrumentation(Constants_1.StatsbeatInstrumentation.BUNYAN);
              }
          }
          ;
          clients.push(client);
      }
      else {
          clients = clients.filter(function (c) { return c != client; });
          if (clients.length === 0) {
              diagnostic_channel_1.channel.unsubscribe("bunyan", subscriber);
              if (statsbeat) {
                  statsbeat.removeInstrumentation(Constants_1.StatsbeatInstrumentation.BUNYAN);
              }
          }
      }
  }
  exports.enable = enable;
  function dispose() {
      diagnostic_channel_1.channel.unsubscribe("bunyan", subscriber);
      clients = [];
  }
  exports.dispose = dispose;
  //# sourceMappingURL=bunyan.sub.js.map