"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var output_exports = {};
__export(output_exports, {
  debug: () => debug,
  debugCache: () => debugCache,
  debugCacheNs: () => debugCacheNs,
  debugDir: () => debugDir,
  debugDirNs: () => debugDirNs,
  debugLog: () => debugLog,
  debugLogNs: () => debugLogNs,
  debugNs: () => debugNs,
  debuglog: () => debuglog,
  debugtime: () => debugtime
});
module.exports = __toCommonJS(output_exports);
var import_debug = __toESM(require("../external/debug"));
var import_array = require("../primordials/array");
var import_date = require("../primordials/date");
var import_reflect = require("../primordials/reflect");
var import_registry = require("../spinner/registry");
var import_format = require("../strings/format");
var import_internal = require("./_internal");
var import_caller_info = require("./caller-info");
var import_namespace = require("./namespace");
var import_socket = require("../env/socket");
function debug(...args) {
  debugNs("*", ...args);
}
// @__NO_SIDE_EFFECTS__
function debugCache(operation, key, meta) {
  if (!(0, import_socket.getSocketDebug)()) {
    return;
  }
  const callerName = (0, import_caller_info.getCallerInfo)(3) || "cache";
  const pointingTriangle = (0, import_internal.getPointingTriangle)();
  const prefix = `[CACHE] ${callerName} ${pointingTriangle} ${operation}: ${key}`;
  const args = meta !== void 0 ? [prefix, meta] : [prefix];
  (0, import_reflect.ReflectApply)(import_internal.logger.info, import_internal.logger, args);
}
function debugCacheNs(namespacesOrOpts, operation, key, meta) {
  const options = (0, import_namespace.extractOptions)(namespacesOrOpts);
  const { namespaces } = options;
  if (!(0, import_namespace.isEnabled)(namespaces)) {
    return;
  }
  const callerName = (0, import_caller_info.getCallerInfo)(4) || "cache";
  const pointingTriangle = (0, import_internal.getPointingTriangle)();
  const prefix = `[CACHE] ${callerName} ${pointingTriangle} ${operation}: ${key}`;
  const logArgs = meta !== void 0 ? [prefix, meta] : [prefix];
  const spinnerInstance = options.spinner || (0, import_registry.getDefaultSpinner)();
  const wasSpinning = spinnerInstance?.isSpinning;
  spinnerInstance?.stop();
  (0, import_reflect.ReflectApply)(import_internal.logger.info, import_internal.logger, logArgs);
  if (wasSpinning) {
    spinnerInstance?.start();
  }
}
function debugDir(obj, inspectOpts) {
  debugDirNs("*", obj, inspectOpts);
}
function debugDirNs(namespacesOrOpts, obj, inspectOpts) {
  const options = (0, import_namespace.extractOptions)(namespacesOrOpts);
  const { namespaces } = options;
  if (!(0, import_namespace.isEnabled)(namespaces)) {
    return;
  }
  const callerName = (0, import_caller_info.getCallerInfo)(4) || "anonymous";
  const pointingTriangle = (0, import_internal.getPointingTriangle)();
  let opts = inspectOpts;
  if (opts === void 0) {
    const debugOpts = import_debug.default.inspectOpts;
    if (debugOpts) {
      opts = {
        ...debugOpts,
        showHidden: debugOpts.showHidden === null ? void 0 : debugOpts.showHidden,
        depth: debugOpts.depth === null || typeof debugOpts.depth === "boolean" ? void 0 : debugOpts.depth
      };
    }
  }
  const spinnerInstance = options.spinner || (0, import_registry.getDefaultSpinner)();
  const wasSpinning = spinnerInstance?.isSpinning;
  spinnerInstance?.stop();
  import_internal.logger.info(`[DEBUG] ${callerName} ${pointingTriangle} object inspection:`);
  import_internal.logger.dir(obj, inspectOpts);
  if (wasSpinning) {
    spinnerInstance?.start();
  }
}
function debugLog(...args) {
  debugLogNs("*", ...args);
}
function debugLogNs(namespacesOrOpts, ...args) {
  const options = (0, import_namespace.extractOptions)(namespacesOrOpts);
  const { namespaces } = options;
  if (!(0, import_namespace.isEnabled)(namespaces)) {
    return;
  }
  const callerName = (0, import_caller_info.getCallerInfo)(4) || "anonymous";
  const pointingTriangle = (0, import_internal.getPointingTriangle)();
  const text = (0, import_array.ArrayPrototypeAt)(args, 0);
  const logArgs = typeof text === "string" ? [
    (0, import_format.applyLinePrefix)(`${callerName} ${pointingTriangle} ${text}`, {
      prefix: "[DEBUG] "
    }),
    ...(0, import_array.ArrayPrototypeSlice)(args, 1)
  ] : [`[DEBUG] ${callerName} ${pointingTriangle}`, ...args];
  const spinnerInstance = options.spinner || (0, import_registry.getDefaultSpinner)();
  const wasSpinning = spinnerInstance?.isSpinning;
  spinnerInstance?.stop();
  (0, import_reflect.ReflectApply)(import_internal.logger.info, import_internal.logger, logArgs);
  if (wasSpinning) {
    spinnerInstance?.start();
  }
}
function debugNs(namespacesOrOpts, ...args) {
  const options = (0, import_namespace.extractOptions)(namespacesOrOpts);
  const { namespaces } = options;
  if (!(0, import_namespace.isEnabled)(namespaces)) {
    return;
  }
  const name = (0, import_caller_info.getCallerInfo)(4) || "anonymous";
  const pointingTriangle = (0, import_internal.getPointingTriangle)();
  const text = (0, import_array.ArrayPrototypeAt)(args, 0);
  const logArgs = typeof text === "string" ? [
    (0, import_format.applyLinePrefix)(`${name} ${pointingTriangle} ${text}`, {
      prefix: "[DEBUG] "
    }),
    ...(0, import_array.ArrayPrototypeSlice)(args, 1)
  ] : args;
  const spinnerInstance = options.spinner || (0, import_registry.getDefaultSpinner)();
  const wasSpinning = spinnerInstance?.isSpinning;
  spinnerInstance?.stop();
  (0, import_reflect.ReflectApply)(import_internal.logger.info, import_internal.logger, logArgs);
  if (wasSpinning) {
    spinnerInstance?.start();
  }
}
// @__NO_SIDE_EFFECTS__
function debuglog(section) {
  const util = (0, import_internal.getUtil)();
  return util.debuglog(section);
}
// @__NO_SIDE_EFFECTS__
function debugtime(label) {
  const util = (0, import_internal.getUtil)();
  let startTime;
  const impl = () => {
    if (startTime === void 0) {
      startTime = (0, import_date.DateNow)();
    } else {
      const duration = (0, import_date.DateNow)() - startTime;
      util.debuglog("time")(`${label}: ${duration}ms`);
      startTime = void 0;
    }
  };
  impl.start = () => {
    startTime = (0, import_date.DateNow)();
  };
  impl.end = () => {
    if (startTime !== void 0) {
      const duration = (0, import_date.DateNow)() - startTime;
      util.debuglog("time")(`${label}: ${duration}ms`);
      startTime = void 0;
    }
  };
  return impl;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  debug,
  debugCache,
  debugCacheNs,
  debugDir,
  debugDirNs,
  debugLog,
  debugLogNs,
  debugNs,
  debuglog,
  debugtime
});
