text: "use strict";
  var _a, _b, _c, _d, _e, _f;
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.traceClientResponseMessage = exports.traceClientRequestMessage = exports.traceResponseMessage = exports.traceRequestMessage = exports.isGoodish = exports._dump_client_transaction_statistics = exports._dump_transaction_statistics = exports.dumpSecurityHeader = exports.doTraceStatistics = exports.doTraceClientResponseContent = exports.doTraceClientRequestContent = exports.doTraceClientMessage = exports.doPerfMonitoring = exports.doTraceResponse = exports.doTraceRequest = exports.doTraceServerMessage = void 0;
  /* eslint-disable max-statements */
  /* eslint-disable complexity */
  const chalk = require("chalk");
  const node_opcua_utils_1 = require("node-opcua-utils");
  const node_opcua_assert_1 = require("node-opcua-assert");
  const node_opcua_types_1 = require("node-opcua-types");
  const node_opcua_status_code_1 = require("node-opcua-status-code");
  const clientFlag = (((_b = (_a = process.env) === null || _a === void 0 ? void 0 : _a.NODEOPCUADEBUG) === null || _b === void 0 ? void 0 : _b.match(/CLIENT{([^}]*)}/)) || [])[1] || "";
  const serverFlag = (((_d = (_c = process.env) === null || _c === void 0 ? void 0 : _c.NODEOPCUADEBUG) === null || _d === void 0 ? void 0 : _d.match(/SERVER{([^}]*)}/)) || [])[1] || "";
  const filter = new RegExp((((_f = (_e = process.env) === null || _e === void 0 ? void 0 : _e.NODEOPCUADEBUG) === null || _f === void 0 ? void 0 : _f.match(/FILTER{([^}]*)}/)) || [])[1] || ".*");
  // console.log("serverFlag", serverFlag);
  // console.log("clientFlag", clientFlag);
  exports.doTraceServerMessage = serverFlag.match(/TRACE/);
  exports.doTraceRequest = serverFlag.match(/REQUEST/);
  exports.doTraceResponse = serverFlag.match(/RESPONSE/);
  exports.doPerfMonitoring = serverFlag.match(/PERF/);
  ///
  exports.doTraceClientMessage = clientFlag.match(/TRACE/);
  exports.doTraceClientRequestContent = clientFlag.match(/REQUEST/);
  exports.doTraceClientResponseContent = clientFlag.match(/RESPONSE/);
  exports.doTraceStatistics = process.env.NODEOPCUADEBUG && !!process.env.NODEOPCUADEBUG.match("STATS");
  // const doPerfMonitoring = process.env.NODEOPCUADEBUG && process.env.NODEOPCUADEBUG.indexOf("PERF") >= 0;
  exports.dumpSecurityHeader = process.env.NODEOPCUADEBUG && !!process.env.NODEOPCUADEBUG.match("SECURITY");
  // istanbul ignore next
  function _dump_transaction_statistics(stats) {
      if (stats) {
          console.log("                Bytes Read : ", stats.bytesRead);
          console.log("             Bytes Written : ", stats.bytesWritten);
          if (exports.doPerfMonitoring) {
              console.log("   time to receive request : ", (stats.lap_reception / 1000).toFixed(3), " sec");
              console.log("   time to process request : ", (stats.lap_processing / 1000).toFixed(3), " sec");
              console.log("   time to send response   : ", (stats.lap_emission / 1000).toFixed(3), " sec");
          }
      }
  }
  exports._dump_transaction_statistics = _dump_transaction_statistics;
  function _dump_client_transaction_statistics(stats) {
      function w(str) {
          return ("                  " + str).substr(-12);
      }
      console.log(chalk.green.bold("--------------------------------------------------------------------->> Stats"));
      console.log("   request                   : ", chalk.yellow(stats.request.schema.name.toString()), " / ", chalk.yellow(stats.response.schema.name.toString()), " - ", stats.request.requestHeader.requestHandle, "/", stats.response.responseHeader.requestHandle, stats.response.responseHeader.serviceResult.toString());
      console.log("   Bytes Read                : ", w(stats.bytesRead), " bytes");
      console.log("   Bytes Written             : ", w(stats.bytesWritten), " bytes");
      if (exports.doPerfMonitoring) {
          console.log("   transaction duration      : ", w(stats.lap_transaction.toFixed(3)), " milliseconds");
          console.log("   time to send request      : ", w(stats.lap_sending_request.toFixed(3)), " milliseconds");
          console.log("   time waiting for response : ", w(stats.lap_waiting_response.toFixed(3)), " milliseconds");
          console.log("   time to receive response  : ", w(stats.lap_receiving_response.toFixed(3)), " milliseconds");
          console.log("   time processing response  : ", w(stats.lap_processing_response.toFixed(3)), " milliseconds");
      }
      console.log(chalk.green.bold("---------------------------------------------------------------------<< Stats"));
  }
  exports._dump_client_transaction_statistics = _dump_client_transaction_statistics;
  const nameLength = "TranslateBrowsePathsToNodeIdsResponse".length + 2;
  function __get_extraInfo(req) {
      var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
      if (req instanceof node_opcua_types_1.ReadRequest) {
          return " nodesToRead.length    =" + ((_a = req.nodesToRead) === null || _a === void 0 ? void 0 : _a.length);
      }
      if (req instanceof node_opcua_types_1.ReadResponse) {
          return " results.length        =" + ((_b = req.results) === null || _b === void 0 ? void 0 : _b.length);
      }
      if (req instanceof node_opcua_types_1.WriteRequest) {
          return " nodesToWrite.length   =" + ((_c = req.nodesToWrite) === null || _c === void 0 ? void 0 : _c.length);
      }
      if (req instanceof node_opcua_types_1.WriteResponse) {
          return " results.length        =" + ((_d = req.results) === null || _d === void 0 ? void 0 : _d.length);
      }
      if (req instanceof node_opcua_types_1.BrowseRequest) {
          return " nodesToBrowse.length  =" + ((_e = req.nodesToBrowse) === null || _e === void 0 ? void 0 : _e.length);
      }
      if (req instanceof node_opcua_types_1.BrowseResponse) {
          return " results.length        =" + ((_f = req.results) === null || _f === void 0 ? void 0 : _f.length);
      }
      if (req instanceof node_opcua_types_1.BrowseNextRequest) {
          return "                        "; // nodesToBrowse.length" + req.?.length;
      }
      if (req instanceof node_opcua_types_1.BrowseNextResponse) {
          return " results.length        =" + ((_g = req.results) === null || _g === void 0 ? void 0 : _g.length);
      }
      if (req instanceof node_opcua_types_1.CreateMonitoredItemsRequest) {
          return " itemsToCreate.length  =" + ((_h = req.itemsToCreate) === null || _h === void 0 ? void 0 : _h.length);
      }
      if (req instanceof node_opcua_types_1.CreateMonitoredItemsResponse) {
          return " results.length        =" + ((_j = req.results) === null || _j === void 0 ? void 0 : _j.length);
      }
      if (req instanceof node_opcua_types_1.TranslateBrowsePathsToNodeIdsRequest) {
          return " browsePaths.length    =" + ((_k = req.browsePaths) === null || _k === void 0 ? void 0 : _k.length);
      }
      if (req instanceof node_opcua_types_1.TranslateBrowsePathsToNodeIdsResponse) {
          return " results.length        =" + ((_l = req.results) === null || _l === void 0 ? void 0 : _l.length);
      }
      if (req instanceof node_opcua_types_1.RegisterNodesRequest) {
          return " nodesToRegister.length=" + ((_m = req.nodesToRegister) === null || _m === void 0 ? void 0 : _m.length);
      }
      if (req instanceof node_opcua_types_1.RegisterNodesResponse) {
          return " nodesToRegister.length=" + ((_o = req.registeredNodeIds) === null || _o === void 0 ? void 0 : _o.length);
      }
      if (req instanceof node_opcua_types_1.PublishResponse) {
          let t = "";
          if (req.notificationMessage.notificationData) {
              for (const n of req.notificationMessage.notificationData) {
                  t += ((_p = n === null || n === void 0 ? void 0 : n.constructor) === null || _p === void 0 ? void 0 : _p.name) + " ";
                  if (n instanceof node_opcua_types_1.DataChangeNotification) {
                      t += (_q = n.monitoredItems) === null || _q === void 0 ? void 0 : _q.length;
                  }
                  if (n instanceof node_opcua_types_1.EventNotificationList) {
                      t += (_r = n.events) === null || _r === void 0 ? void 0 : _r.length;
                  }
                  if (n instanceof node_opcua_types_1.StatusChangeNotification) {
                      t += n.status.toString();
                  }
                  t += " ";
                  t = t.replace(/Notification/g, "Nt°");
              }
          }
          return " " + t + " seq#=" + req.notificationMessage.sequenceNumber;
      }
      return "";
  }
  function _get_extraInfo(req) {
      return __get_extraInfo(req).padEnd(30);
  }
  function evaluateBinarySize(r) {
      const e = r;
      const size = e.binaryStoreSize();
      return "s=" + ("" + size).padStart(6) + " ";
  }
  function isGoodish(statusCode) {
      return statusCode.value < 0x40000000;
  }
  exports.isGoodish = isGoodish;
  function statusCodeToString(s) {
      if (s === node_opcua_status_code_1.StatusCodes.Good) {
          return chalk.green(s.toString());
      }
      else if (isGoodish(s)) {
          return chalk.yellow(s.toString());
      }
      else {
          return chalk.red(s.toString());
      }
  }
  // istanbul ignore next
  function traceRequestMessage(request, channelId, instance) {
      if (exports.doTraceServerMessage) {
          const extra = _get_extraInfo(request);
          const size = evaluateBinarySize(request);
          const requestId = request.requestHeader.requestHandle;
          console.log(chalk.green((0, node_opcua_utils_1.timestamp)(), "   >>>>> ------ S"), instance.toString().padStart(3), channelId.toString().padStart(3), requestId.toString().padStart(8), chalk.yellow(request.schema.name.padEnd(nameLength)), extra, size);
          if (exports.doTraceRequest && filter && request.constructor.name.match(filter)) {
              console.log(request.toString());
              console.log(chalk.cyan("   >>>>> ------ \n"));
          }
      }
  }
  exports.traceRequestMessage = traceRequestMessage;
  // istanbul ignore next
  function traceResponseMessage(response, channelId, instance) {
      (0, node_opcua_assert_1.assert)(response.responseHeader.requestHandle >= 0);
      if (exports.doTraceServerMessage) {
          const extra = _get_extraInfo(response);
          const size = evaluateBinarySize(response);
          const requestId = response.responseHeader.requestHandle;
          console.log(chalk.green.bold((0, node_opcua_utils_1.timestamp)(), "   <<<<< ------ S"), instance.toString().padStart(3), channelId.toString().padStart(3), requestId.toString().padStart(8), chalk.green.bold(response.schema.name.padEnd(nameLength)), extra, size, statusCodeToString(response.responseHeader.serviceResult));
          if (exports.doTraceResponse && filter && response.constructor.name.match(filter)) {
              console.log(response.toString());
              console.log(chalk.cyan.bold("       <<<<< ------n"));
          }
      }
  }
  exports.traceResponseMessage = traceResponseMessage;
  // istanbul ignore next
  // istanbul ignore next
  function traceClientRequestMessage(request, channelId, instance) {
      const extra = _get_extraInfo(request);
      const size = evaluateBinarySize(request);
      const requestId = request.requestHeader.requestHandle;
      console.log(chalk.cyan((0, node_opcua_utils_1.timestamp)(), "  >>>>>> ------ C"), instance.toString().padStart(3), channelId.toString().padStart(3), requestId.toString().padStart(8), request.schema.name.padEnd(nameLength), extra, size);
  }
  exports.traceClientRequestMessage = traceClientRequestMessage;
  function traceClientResponseMessage(response, channelId, instance) {
      const extra = _get_extraInfo(response);
      const size = evaluateBinarySize(response);
      const requestId = response.responseHeader.requestHandle;
      console.log(chalk.cyan.bold((0, node_opcua_utils_1.timestamp)(), "  <<<<<< ------ C"), instance.toString().padStart(3), channelId.toString().padStart(3), requestId.toString().padStart(8), response.schema.name.padEnd(nameLength), extra, size, statusCodeToString(response.responseHeader.serviceResult));
  }
  exports.traceClientResponseMessage = traceClientResponseMessage;
  //# sourceMappingURL=utils.js.map